2024-03-28 14:52:40 +05:30
< ? php
namespace App\Http\Controllers\Frontend ;
use App\Http\Controllers\Controller ;
use Illuminate\Http\Request ;
use App\Models\User ;
use Carbon\Carbon ;
use Validator ;
use Mail ;
use App\Mail\OtpMail ;
use Illuminate\Support\Facades\Hash ;
use Illuminate\Support\Facades\Auth ;
use Illuminate\Support\Facades\Session ;
class LoginController extends Controller
{
public function index ()
{
return view ( 'Frontend.Pages.login' );
}
public function register ()
{
return view ( 'Frontend.Pages.register' );
}
public function registerInvestor ()
{
return view ( 'Frontend.Pages.register-investor' );
}
public function registerAssetManagers ()
{
return view ( 'Frontend.Pages.register-asset-managers' );
}
public function registerIntermediaries ()
{
return view ( 'Frontend.Pages.register-intermediaries' );
}
public function registerOtp ()
{
return view ( 'Frontend.Pages.register_otp' );
}
2024-04-03 15:33:08 +05:30
public function registerMobileOtp ()
{
return view ( 'Frontend.Pages.register_mobile_otp' );
}
2024-03-28 14:52:40 +05:30
public function forgetPassword (){
return view ( 'Frontend.Pages.forget_password_request' );
}
public function resetPassword (){
return view ( 'Frontend.Pages.forget_password' );
}
// login method with mobile
public function login_by_mobile ( Request $request )
{
// validating data
$validator = Validator :: make ( $request -> all (), [
'contact_number' => 'required|exists:users,contact_number|min:10|max:10' ,
'g-recaptcha-response' => 'sometimes|required'
], [
'required' => 'The :attribute field is required' ,
'exists' => 'Contact Number is not registered!' ,
'min' => 'Contact Number should be 10 digits!' ,
'max' => 'Contact Number should be 10 digits!' ,
'g-recaptcha-response.required' => 'Please verify captcha!'
]);
$validationMessage = $this -> validationError ( $validator );
if ( $validationMessage ) {
return response () -> json ([ 'status' => 400 , 'message' => $validationMessage ], 400 );
}
// $user = User::where('contact_number', $request->contact_number)->first();
$user = User :: where ([ 'contact_number' => $request -> contact_number , 'role' => 0 ]) -> first ();
if ( ! $user )
{
return response () -> json ([ 'status' => 201 , 'error' => " Contact number not exist " ]);
}
$contactNumber = [
'contact_number' => $request -> contact_number ,
];
Session :: forget ( 'phone_for_otp' );
Session :: put ( 'phone_for_otp' , $contactNumber );
$otp = rand ( 1000 , 9999 );
$sendOtp = $this -> thirdPartyOTP ( $user -> contact_number , $otp );
if ( $sendOtp ) {
$user -> otp = $otp ;
$user -> expire_at = Carbon :: now ( 'Asia/colombo' ) -> addMinutes ( 2 );
$user -> update ();
return response () -> json ([ 'message' => 'OTP has been sent to phone' , 'status' => 200 ]);
} else {
return response () -> json ([ 'message' => 'Error Sending OTP' , 'status' => 400 ], 400 );
}
}
// otp verification in login by mobile
public function verify_mobile_otp ( Request $request )
{
// validating data
$validator = Validator :: make ( $request -> all (), [
// 'user_id' => 'required',
'otp' => 'required'
]);
// validation fail handling
if ( $validator -> fails ()) {
return response () -> json ([ 'error' => $validator -> errors (), 'status' => 204 ], 204 );
}
$otp = $request -> otp ;
$contactNumber = Session :: get ( 'phone_for_otp' );
// validating otp
$user = User :: where ( 'contact_number' , $contactNumber [ 'contact_number' ]) -> first ();
// otp verification failed handling
$current_time = Carbon :: now ( 'Asia/colombo' );
if ( $otp !== $user -> otp ) {
return response () -> json ([ 'error' => 'Your OTP is not correct' , 'status' => 400 ], 400 );
} elseif ( $current_time && $current_time -> isAfter ( $user -> expire_at )) {
return response () -> json ([ 'error' => 'Your OTP has been expired' , 'status' => 400 ], 400 );
}
// Session::forget('phone_for_otp');
// $otp->otp = null;
// $otp->expire_at = null;
// $otp->update();
\Auth :: guard ( 'users' ) -> login ( $user );
return response () -> json ([
'status' => 200 ,
'user_id' => $user -> id ,
'message' => 'logged in successfully' ,
'token' => $user -> createToken ( " API TOKEN " ) -> plainTextToken
], 200 );
}
// otp verification for password change
public function verify_reset_password_otp ( Request $request )
{
// dd($request->all());
// validating data
$validator = Validator :: make ( $request -> all (), [
// 'user_id' => 'required|exists:users,id',
'otp' => 'required' ,
]);
// validation fail handling
if ( $validator -> fails ()) {
return response () -> json ([ 'error' => 'Invalid request' , 'status' => 204 ]);
}
// validating otp
$otp = User :: where ( 'id' , $request -> user_id ) -> first ();
// otp verification failed handling
$current_time = Carbon :: now ( 'Asia/colombo' );
if ( $otp -> otp != $request -> otp ) {
return response () -> json ([ 'error' => 'Your OTP is not correct' , 'status' => 400 ]);
} elseif ( $otp && $current_time -> isAfter ( $otp -> expire_at )) {
return response () -> json ([ 'error' => 'Your OTP has been expired' , 'status' => 400 ]);
}
// $otp->otp = null;
// $otp->expire_at = null;
// $otp->update();
return response () -> json ([ 'message' => 'Otp verified successfully' , 'status' => 200 ]);
}
// login with email and password
public function loginByEmail ( Request $request )
{
// validating data
$validator = Validator :: make ( $request -> all (), [
'email' => 'required|exists:users,email' ,
'password' => 'required|min:8' ,
'g-recaptcha-response' => 'sometimes|required'
], [
'required' => 'The :attribute field is required' ,
'unique' => 'The :attribute field should be unique' ,
'exists' => 'Email is not registered!' ,
'g-recaptcha-response.required' => 'Please verify captcha!'
]);
$validationMessage = $this -> validationError ( $validator );
if ( $validationMessage ) {
return response () -> json ([ 'status' => 400 , 'message' => $validationMessage ]);
}
// handling validation error
// if ($validator->fails())
// {
// return response()->json(['error'=>'Email is not registered', 'status' => 204]);
// }
if ( ! Auth :: guard ( 'users' ) -> attempt ( $request -> only ([ 'email' , 'password' ]))) {
return response () -> json ([
'status' => 400 ,
'message' => 'Credentials does not match!' ,
]);
}
$user = User :: where ([ 'email' => $request -> email , 'role' => 0 ]) -> first ();
if ( ! $user )
{
return response () -> json ([ 'status' => 400 , 'message' => " Credentials does not match! " ]);
}
return response () -> json ([
'status' => 200 ,
'user_id' => $user -> id ,
'message' => 'Logged in successfully' ,
'token' => $user -> createToken ( " API TOKEN " ) -> plainTextToken
], 200 );
}
// forget password by email or phone
public function forget_password ( Request $request )
{
// checking input is email or phone
$loginWith = filter_var ( $request -> emailorphone , FILTER_VALIDATE_EMAIL ) ? 'email' : 'contact_number' ;
// validating data
$validator = Validator :: make (
$request -> all (),
[
'emailorphone' => " required|exists:users, $loginWith " ,
]
);
// handling validation error
if ( $validator -> fails ()) {
// $errorName = $loginWith == 'email' ? 'email' : 'contact number';
// $errorName = is_int($request->emailorphone) ? 'contact number' : 'email';
// return response()->json(['error' => $errorName . " is not registered", 'status' => 204]);
return response () -> json ([ 'error' => " Enter valid credential " , 'status' => 204 ]);
}
// checking user exits or not
if ( $loginWith == 'contact_number' ) {
try {
$otp = $this -> otpGenerate ( $request -> email );
$sendOTP = $this -> thirdPartyOTP ( $request -> emailorphone , $otp );
$user = User :: where ( 'contact_number' , $request -> emailorphone ) -> first ();
// dd($user->id);
Session :: forget ( 'phone_email_forgot' );
Session :: put ( 'phone_email_forgot' , $request -> emailorphone );
$user -> otp = $otp ;
$user -> expire_at = Carbon :: now ( 'Asia/colombo' ) -> addMinutes ( 2 );
$user -> update ();
// Session::forget('user_id');
// Session::put('user_id', $user->id);
return response () -> json ([ 'user_id' => $user -> id , 'status' => 200 ]);
} catch ( \Exception $e ) {
return response () -> json ([ 'error' => 'Netwrok Error! Please try again after sometime.' , 'status' => 500 ]);
}
} else {
try {
$otp = $this -> otpGenerate ( $request -> emailorphone );
$mailData = [
2024-04-02 17:32:45 +05:30
'title' => 'Mail from Jerichoalternatives.in' ,
2024-03-28 14:52:40 +05:30
'body' => 'This is for testing email using smtp.'
];
// $otp = $this->otpGenerate($request->email);
Mail :: to ( $request -> emailorphone ) -> send ( new OtpMail ( $mailData , $otp ));
$user = User :: where ( 'email' , $request -> emailorphone ) -> first ();
// dd($user);
Session :: forget ( 'phone_email_forgot' );
Session :: put ( 'phone_email_forgot' , $request -> emailorphone );
$user -> otp = $otp ;
$user -> expire_at = Carbon :: now ( 'Asia/colombo' ) -> addMinutes ( 2 );
$user -> update ();
// Session::forget('user_id');
// Session::put('user_id', $user->id);
return response () -> json ([ 'user_id' => $user -> id , 'status' => 200 ]);
} catch ( \Exception $e ) {
2024-04-02 17:32:45 +05:30
return response () -> json ([ 'error' => 'Network Error! Please try again after sometime.' , 'status' => 500 ]);
2024-03-28 14:52:40 +05:30
}
}
}
// update password with otp
public function update_password ( Request $request )
{
// validating data
$validator = Validator :: make ( $request -> all (), [
// 'user_id' => 'required|exists:users,id',
'password' => 'required|confirmed|min:8' ,
'otp' => 'required'
]);
// handling validation error
if ( $validator -> fails ()) {
// dd($validator->errors());
return response () -> json ([ 'error' => $validator -> errors (), 'status' => 204 ]);
}
$phoneOrEmail = Session :: get ( 'phone_email_forgot' );
// $checkOtp = User::where(['email'=>$phoneOrEmail,'otp'=>$request->otp])->orWhere(['email'=>$phoneOrEmail,'otp'=>$request->otp])
// $user = User::where('contact_number', $phoneOrEmail)->orWhere('email',$phoneOrEmail)->first();
// $user = User::where(['contact_number'=>$phoneOrEmail,'otp'=>$request->otp])->orWhere(['email'=>$phoneOrEmail,'otp'=>$request->otp])->first();
// $user = User::where('id', $request->user_id)->where('otp', $request->otp)->first();
$user = User :: where ( 'otp' , '=' , $request -> otp )
-> where ( function ( $query ) use ( $phoneOrEmail ){
return $query
-> where ( 'contact_number' , '=' , $phoneOrEmail )
-> orWhere ( 'email' , '=' , $phoneOrEmail );
})
-> first ();
$current_time = Carbon :: now ( 'Asia/colombo' );
if ( ! $user ) {
return response () -> json ([ 'error' => 'Your OTP is not correct' , 'status' => 400 ]);
}
elseif ( $user && $current_time -> isAfter ( $user -> expire_at )) {
return response () -> json ([ 'error' => 'Your OTP has been expired' , 'status' => 400 ]);
} else {
try {
$user -> password = Hash :: make ( $request -> password );
$user -> update ();
return response () -> json ([ 'message' => 'Password has been updated successfully' , 'status' => 200 ]);
} catch ( \Exception $e ) {
return response () -> json ([ 'error' => 'Netwrok Error! Please try again after sometime.' , 'status' => 500 ]);
}
}
}
2024-04-15 19:18:22 +05:30
// resend otp for forgot password
public function resendOtp ()
{
// dd('hello');
$email_mobile = Session :: get ( 'phone_email_forgot' );
$loginWith = filter_var ( $email_mobile , FILTER_VALIDATE_EMAIL ) ? 'email' : 'contact_number' ;
if ( $loginWith == 'contact_number' )
{
// $otp = rand(0000,9999);
$otp = $this -> otpGenerate ( $email_mobile );
$this -> thirdPartyOTP ( $email_mobile , $otp );
User :: where ( 'contact_number' , $email_mobile ) -> update ([
'otp' => $otp ,
'expire_at' => Carbon :: now ( 'Asia/colombo' ) -> addMinutes ( 2 )
]);
} else {
$otp = $this -> otpGenerate ( $email_mobile );
$mailData = [
'title' => 'Mail from Jerichoalternatives.in' ,
'body' => 'This is for testing email using smtp.'
];
// $otp = $this->otpGenerate($request->email);
Mail :: to ( $email_mobile ) -> send ( new OtpMail ( $mailData , $otp ));
User :: where ( 'email' , $email_mobile ) -> update ([
'otp' => $otp ,
'expire_at' => Carbon :: now ( 'Asia/colombo' ) -> addMinutes ( 2 )
]);
}
return response () -> json ([ 'status' => 200 , 'message' => 'OTP has been send to your email or phone' ]);
}
2024-03-28 14:52:40 +05:30
// resend otp while login
public function request_otp ( Request $request )
{
$validator = Validator :: make (
$request -> all (),
[
// 'user_id' => 'required|exists:users,id',
]
);
if ( $validator -> fails ()) {
return response () -> json ([ 'error' => $validator -> errors ()], 204 );
}
$contactNumber = Session :: get ( 'phone_for_otp' );
$user = User :: where ( 'contact_number' , $contactNumber [ 'contact_number' ]) -> first ();
$otp = rand ( 1000 , 9999 );
$sendOTP = $this -> thirdPartyOTP ( $user -> contact_number , $otp );
try {
if ( $sendOTP ) {
$user -> otp = $otp ;
$user -> expire_at = Carbon :: now ( 'Asia/colombo' ) -> addMinutes ( 2 );
$user -> update ();
return response () -> json ([ 'message' => 'otp send successfully on your registered phone number - ' . $user -> contact_number . ' and email - ' . $user -> email , 'status' => 200 ]);
}
} catch ( \Exception $e ) {
return response () -> json ([ 'error' => 'Netwrok Error! Please try again after sometime.' , 'status' => 500 ]);
}
}
public function signOut ()
{
\Session :: flush ();
\Auth :: guard ( 'users' ) -> logout ();
// return redirect()->route('admin.login');
return redirect () -> back ();
}
public function validationError ( $validator )
{
if ( $validator -> fails ()) {
$errors = $validator -> errors ();
$messages = '' ;
foreach ( $errors -> all () as $message ) {
$messages .= $message . '</br>' ;
}
return $messages ;
}
}
public function otpGenerate ( $email )
{
$otp = rand ( 1000 , 9999 );
return $otp ;
}
public function thirdPartyOTP ( $number , $otp )
{
$curl = curl_init ();
curl_setopt_array ( $curl , array (
CURLOPT_URL => 'https://restapi.smscountry.com/v0.1/Accounts/4F7T5SbGyV7HBrEHxmX4/SMSes/' ,
CURLOPT_RETURNTRANSFER => true ,
CURLOPT_ENCODING => '' ,
CURLOPT_MAXREDIRS => 10 ,
CURLOPT_TIMEOUT => 0 ,
CURLOPT_FOLLOWLOCATION => true ,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
CURLOPT_CUSTOMREQUEST => 'POST' ,
CURLOPT_POSTFIELDS => ' {
" Text " : " Dear Investor, Your login OTP is ' . $otp . '. - Blue Feather Ventures Pvt Ltd. " ,
" Number " : " 91' . $number . ' " ,
" SenderId " : " BLUFVL " ,
" DRNotifyUrl " : " https://www.domainname.com/notifyurl " ,
" DRNotifyHttpMethod " : " POST " ,
" Tool " : " API "
} ' ,
CURLOPT_HTTPHEADER => array (
'Content-Type: application/json' ,
'Authorization: Basic NEY3VDVTYkd5VjdIQnJFSHhtWDQ6emFXdFEyTlV3ZlZROHB6dGRvVlRZUFdibG01Y1AxRldsbWl2WlVrbg=='
),
));
$response = curl_exec ( $curl );
curl_close ( $curl );
return $response ;
// return true;
}
public function searchEmail ( Request $request )
{
$email = $request -> email ;
// dd($email);
$user = User :: where ( 'email' , $email ) -> exists ();
if ( ! $user )
{
return response () -> json (
[
" status " => 201 ,
" message " => " Please enter valid email " ,
]
);
}
else
{
return response () -> json (
[
" status " => 200 ,
// "message"=>"Email is invalid !, Please enter valid email",
]
);
}
}
}