added resend otp while creating account and solving freedcamp issue

This commit is contained in:
Ritikesh yadav
2024-04-19 15:16:12 +05:30
parent 156ae18649
commit 468e936310
12 changed files with 318 additions and 47 deletions

View File

@@ -18,7 +18,7 @@ use Laravel\Sanctum\PersonalAccessToken;
use App\Services\AuthService;
use Carbon\Carbon;
use App\Http\Requests\StoreAssetManagerRegistrationRequest;
use App\Models\RegistrationOtp;
class AuthController extends Controller
{
@@ -450,6 +450,50 @@ class AuthController extends Controller
}
}
public function resendOtpForMobile()
{
// dd(Session::get('user-registration'));
if(!Session::has('user-registration')) {
return response()->json(['status' => 401, 'message' => 'Something error!, Please fill the form again']);
}
else{
$mailData = [
'title' => 'Mail from ItSolutionStuff.com',
'body' => 'This is for testing email using smtp.'
];
$otp = rand(1000,9999);
// Mail::to(Session::get('user-registration')['email'])->send(new OtpMail($mailData, $otp));
$this->thirdPartyOTP(Session::get('user-registration')['contact_number'], $otp);
$oldSession = Session::get('user-registration');
$oldSession['mobile_otp'] = $otp;
Session::put('user-registration',$oldSession);
RegistrationOtp::where('contact_number',Session::get('user-registration')['contact_number'])->update(['contact_otp'=>$otp,'contact_expire_at'=>Carbon::now()->addMinutes('2')]);
return response()->json(['status' => 200, 'message' => 'OTP has been resend to your mobile number']);
}
}
public function resendOtpForEmail()
{
if(!Session::has('user-registration')) {
return response()->json(['status' => 401, 'message' => 'Something error!, Please fill the form again']);
}
else{
$mailData = [
'title' => 'Mail from ItSolutionStuff.com',
'body' => 'This is for testing email using smtp.'
];
$otp = rand(1000,9999);
Mail::to(Session::get('user-registration')['email'])->send(new OtpMail($mailData, $otp));
$oldSession = Session::get('user-registration');
$oldSession['otp'] = $otp;
Session::put('user-registration',$oldSession);
RegistrationOtp::where('email',Session::get('user-registration')['email'])->update(['email_otp'=>$otp,'email_expire_at'=>Carbon::now()->addMinutes('2')]);
return response()->json(['status' => 200, 'message' => 'OTP has been resend to your email']);
}
}
public function enteredEmailOTP(Request $request)
{
// dd((int)$request->otp, Session::get('user-registration')['otp']);
@@ -463,8 +507,9 @@ class AuthController extends Controller
'max' => 'The :attribute field must be 4 digits',
]);
$validationMessage = $this->validationError($validator);
// dd($validationMessage);
if ($validationMessage) {
return response()->json(['status' => 400, 'message' => $validationMessage], 400);
return response()->json(['status' => 401, 'message' => $validationMessage]);
}
if (strlen($request->otp) < 4) {
return response()->json(['status' => 401, 'message' => 'Please enter 4 digit OTP']);
@@ -472,7 +517,17 @@ class AuthController extends Controller
$otp = (int) $request->otp;
if (Session::has('user-registration')) {
// $user = Session::get('user-registration');
if (Session::get('user-registration')['otp'] == $otp) {
$registrationOtpData = RegistrationOtp::where('email', Session::get('user-registration')['email'])->first();
if (now() > $registrationOtpData->email_expire_at) {
//email otp has expired
return response()->json(['status' => 401, 'message' => 'Your Email OTP has been Expired']);
}
if ($registrationOtpData->email_otp == $otp) {
// if (Session::get('user-registration')['otp'] == $otp) {
$mobile_otp = $this->otpGenerate(Session::get('user-registration')['contact_number']);
$this->thirdPartyOTP(Session::get('user-registration')['contact_number'], $mobile_otp);
@@ -484,6 +539,15 @@ class AuthController extends Controller
// Put the updated session data back into the session
Session::put('user-registration', $userRegistration);
//here we have to update the entry of Registration_otps
RegistrationOtp::where('email',Session::get('user-registration')['email'])->update([
'contact_number'=>(int)Session::get('user-registration')['contact_number'],
'contact_otp'=>$mobile_otp,
'contact_expire_at'=>Carbon::now()->addMinutes('2'),
]);
// $userCreated = Session::put('user-registration'['mobile_otp'], $mobile_otp);
//here i have to send mobile OTP in this session
//updated by hritik on 17-04-2024
@@ -562,7 +626,7 @@ class AuthController extends Controller
$insertOtp = UserOtp::create([
'contact_number' => $request->contact_number,
'email' => $request->email,//emai
'email' => $request->email, //emai
'expire_at' => Carbon::now()->addMinutes('5'),
'otp' => $otp, //email otp
'contact_otp' => $mobile_otp //contact OTP
@@ -596,6 +660,7 @@ class AuthController extends Controller
'title' => 'Mail from ItSolutionStuff.com',
'body' => 'This is for testing email using smtp.'
];
RegistrationOtp::updateOrCreate(['email' => $request->email],['email' => $request->email, 'email_otp' => $otp, 'email_expire_at' => Carbon::now()->addMinutes('2')]);
Mail::to($request->email)->send(new OtpMail($mailData, $otp));
// $this->thirdPartyOTP($request->contact_number, $mobile_otp);
@@ -650,7 +715,7 @@ class AuthController extends Controller
$this->thirdPartyOTP($request->contact_number2, $mobile_otp);
$insertOtp = UserOtp::create([
'contact_number' => $request->contact_number2,
'email' => $request->email2,//emai
'email' => $request->email2, //emai
'expire_at' => Carbon::now()->addMinutes('5'),
'otp' => $otp, //email otp
'contact_otp' => $mobile_otp //contact OTP
@@ -678,7 +743,7 @@ class AuthController extends Controller
];
Mail::to($validated['email2'])->send(new OtpMail($mailData, $otp));
// $this->thirdPartyOTP($validated['contact_number2'], $mobile_otp);
// Mail::to('yadavritikesh29@gmail.com')->send(new OtpMail($mailData, $otp));
return response()->json(['status' => 200, 'message' => 'OTP has been sent to your email']);
@@ -756,8 +821,6 @@ class AuthController extends Controller
return response()->json(['status' => 200, 'message' => 'User Created', 'token' => $userToken, 'data' => $user]);
}
}
} catch (\Exception $e) {
return response()->json(['status' => 400, 'message' => 'Error While Registation Details!'], 400);
}
@@ -780,7 +843,7 @@ class AuthController extends Controller
$emailToAdd = $request->email2;
$userEmailOtpData = UserOtp::where('email', $emailToAdd)->where('otp', $emailOtp)->first(); // checking user email otp data
// dd($userEmailOtpData);
// dd($userEmailOtpData);
if (!$userEmailOtpData) {
return response()->json(['status' => 400, 'message' => 'Email OTP Did Not Matched!'], 400);
}
@@ -820,7 +883,6 @@ class AuthController extends Controller
return response()->json(['status' => 200, 'message' => 'User Created', 'token' => $userToken, 'data' => $user]);
}
}
} catch (\Exception $e) {
return response()->json(['status' => 400, 'message' => 'Error While Registation Details!' . $e->getMessage()], 400);
}
@@ -851,10 +913,10 @@ class AuthController extends Controller
$validator = Validator::make($request->all(), [
'user' => 'required',
'password' => [
'required',
// 'min:8',
// 'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\x])(?=.*[!$#%@]).*$/'
],
'required',
// 'min:8',
// 'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\x])(?=.*[!$#%@]).*$/'
],
]);
// if ($validator->fails()) {
@@ -875,8 +937,7 @@ class AuthController extends Controller
}
if (!\Hash::check($password, $user->password)) {
return response()->json(['status' => 400, 'message' => 'Invalid Credentials!'], 400);
}
;
};
// $hashedTooken = $request->bearerToken();
$userToken = $user->createToken('apiToken')->plainTextToken;
$token = PersonalAccessToken::findToken($userToken);
@@ -933,7 +994,7 @@ class AuthController extends Controller
]);
$validationMessage = $this->validationError($validator);
if ($validationMessage) {
return response()->json(['status' => 400, 'message' => $validationMessage], 400);
return response()->json(['status' => 401, 'message' => $validationMessage]);
}
if (strlen($request->otp) < 4) {
return response()->json(['status' => 401, 'message' => 'Please enter 4 digit OTP']);
@@ -941,6 +1002,13 @@ class AuthController extends Controller
$otp = (int) $request->otp;
if (Session::has('user-registration')) {
$userDetails = Session::get('user-registration');
$registrationOtp = RegistrationOtp::where('contact_number',$userDetails['contact_number'])->first();
// dd($userDetails);
if(now() > $registrationOtp->contact_expire_at)
{
return response()->json(['status' => 401, 'message' => 'Mobile OTP has been expired!']);
// return
}
if ($userDetails['mobile_otp'] !== $otp) {
return response()->json(['status' => 400, 'message' => 'OTP Did Not Matched!']);
}