Customer forgot password API

This commit is contained in:
sayliraut
2024-05-24 16:48:30 +05:30
parent 554f9a9511
commit e60a264f24
8 changed files with 706 additions and 21 deletions

View File

@@ -7,10 +7,13 @@ use App\Services\APIs\CustomerAPIs\AuthServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use App\Models\IamPrincipalOtp;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\QueryException;
use App\Models\IamPrincipal;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
@@ -87,7 +90,7 @@ class AuthController extends Controller
if ($validator->fails()) {
$validationErrors = $validator->errors()->all();
Log::error("Registation validation error: " . implode(", ", $validationErrors));
Log::error("Customer validation error: " . implode(", ", $validationErrors));
return jsonResponseWithErrorMessageApi($validationErrors, 403);
}
return $this->AuthServices->register($request);
@@ -96,4 +99,157 @@ class AuthController extends Controller
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 403);
}
}
/**
* Created By : sayli Raut
* Created at : 24 May 2024
* Use : Customer login.
*/
public function login(Request $request)
{
try {
$validator = Validator::make($request->all(), [
'email_address' => 'required|string|email',
'password' => 'required|string|min:6',
]);
if ($validator->fails()) {
$validationErrors = $validator->errors()->all();
Log::error("Login validation error: " . implode(", ", $validationErrors));
return jsonResponseWithErrorMessageApi($validationErrors, 403);
}
return $this->AuthServices->login($request);
} catch (QueryException $e) {
Log::error('Customer Login Failed ' . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 403);
}
}
/**
* Created By : sayli Raut
* Created at : 24 May 2024
* Use : forgot password.
*/
public function forgotPassword(Request $request)
{
try {
$validator = Validator::make($request->all(), [
'email_address' => [
'required',
'string',
'email',
function ($attribute, $value, $fail) {
$existingUser = IamPrincipal::where('email_address', $value)->where('principal_type_xid', 3)->whereNull('deleted_at')->exists();
if (!$existingUser) {
$fail('The selected email address is invalid.');
}
},
],
]);
if ($validator->fails()) {
$validationErrors = $validator->errors()->all();
Log::error("Forgot password validation error: " . implode(", ", $validationErrors));
return jsonResponseWithErrorMessageApi($validationErrors, 403);
}
return $this->AuthServices->forgotPassword($request);
} catch (Exception $e) {
Log::error('Customer Forgot Password OTP function failed: ' . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
}
}
/**
* Created By : sayli Raut
* Created at : 24 May 2024
* Use : OTP verification.
*/
public function verifyOTPForgotPassword(Request $request)
{
try {
$validator = Validator::make($request->all(), [
'email_address' => [
'required',
'string',
'email',
function ($attribute, $value, $fail) {
$existingUser = IamPrincipal::where('email_address', $value)->where('principal_type_xid', 3)->whereNull('deleted_at')->exists();
if (!$existingUser) {
$fail('The selected email address is invalid.');
}
},
],
'otp' => 'required',
]);
if ($validator->fails()) {
$validationErrors = $validator->errors()->all();
Log::error("Forgot password validation error: " . implode(", ", $validationErrors));
return jsonResponseWithErrorMessageApi($validationErrors, 403);
}
return $this->AuthServices->verifyOTPForgotPassword($request);
} catch (Exception $e) {
DB::rollBack();
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
}
}
/**
* Created By : sayli Raut
* Created at : 24 May 2024
* Use : Change Password.
*/
public function changePassword(Request $request)
{
try {
$validator = Validator::make($request->all(), [
'iam_principal_xid' => 'required|exists:iam_principal,id',
'password' => 'required|confirmed',
]);
if ($validator->fails()) {
$validationErrors = $validator->errors()->all();
Log::error("Forgot password validation error: " . implode(", ", $validationErrors));
return jsonResponseWithErrorMessageApi($validationErrors, 403);
}
return $this->AuthServices->changePassword($request);
} catch (Exception $e) {
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage());
return response()->json(__('something_went_wrong'), 500);
}
}
/**
* Created By : sayli Raut
* Created at : 24 May 2024
* Use : Resend OTP .
*/
public function resendOtp(Request $request)
{
try {
$validator = Validator::make($request->all(), [
'iam_principal_xid' => 'required|exists:iam_principal,id',
'otp_purpose' => 'required'
]);
if ($validator->fails()) {
$validationErrors = $validator->errors()->all();
Log::error("Forgot password validation error: " . implode(", ", $validationErrors));
return jsonResponseWithErrorMessageApi($validationErrors, 403);
}
return $this->AuthServices->resendOtp($request);
} catch (Exception $e) {
DB::rollBack();
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage());
return response()->json(__('something_went_wrong'), 500);
}
}
}

View File

@@ -165,4 +165,20 @@ if (!function_exists('readRestHeaderToken')) {
return false;
}
}
if (!function_exists('generateOTP')) {
function generateOTP()
{
// Define the length of the OTP
$otpLength = 4;
// Generate a random OTP with $otpLength digits
$otp = '';
for ($i = 0; $i < $otpLength; $i++) {
$otp .= rand(0, 9);
}
return $otp;
}
}
}

View File

@@ -14,7 +14,7 @@ use App\Models\admin\ManageModule;
use App\Models\OrderedPassport;
class IamPrincipal extends Model
class IamPrincipal extends Authenticatable implements JWTSubject
{
use SoftDeletes;
use HasApiTokens, HasFactory, Notifiable;

View File

@@ -1,10 +1,14 @@
<?php
namespace App\Services\APIs\CustomerAPIs;
use App\Models\IamAppAction;
use App\Models\IamPrincipal;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
use App\Models\IamPrincipalOtp;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\QueryException;
@@ -19,7 +23,7 @@ class AuthServices
public function checkAge($request)
{
try {
$age = $request->input('age');
$age = $request->input('age');
if ($age == 'yes') {
return jsonResponseWithSuccessMessage(__('auth.legally_21'), 200);
} else {
@@ -33,7 +37,6 @@ class AuthServices
public function register($request)
{
dd($request);
try {
DB::beginTransaction();
$user = IamPrincipal::create([
@@ -56,7 +59,7 @@ class AuthServices
'access_token' => $token,
'token_type' => 'bearer',
];
return jsonResponseWithSuccessMessage(__('auth.Rest_user_created'), $response, 200);
return jsonResponseWithSuccessMessage(__('auth.Customer_user_created'), $response, 200);
} catch (QueryException $e) {
DB::rollBack();
Log::error('Restaurant Registration Failed ' . $e->getMessage());
@@ -64,4 +67,222 @@ class AuthServices
}
}
public function login($request)
{
try {
$credentials = [
'email_address' => $request->email_address,
'password' => $request->password,
];
$isDelete = IamPrincipal::where('email_address', $request->email_address)->where('principal_type_xid', 3)->where('deleted_by_admin', 1)->onlyTrashed()->first();
if ($isDelete) {
return jsonResponseWithErrorMessageApi(__('auth.deleted_user_by_admin'), 403);
}
$isExistEmail = IamPrincipal::where('email_address', $request->email_address)->where('principal_type_xid', 3)->whereNull('deleted_at')->first();
if ($isExistEmail == null) {
return jsonResponseWithErrorMessageApi(__('auth.incorrect_email_passport'), 403);
}
if ($isExistEmail && !(Hash::check($request->password, $isExistEmail->password))) {
Log::error('Entered Password is wrong.');
return jsonResponseWithErrorMessageApi(__('auth.incorrect_email_passport'), 403);
}
if (!$token = auth()->login($isExistEmail)) {
Log::error('Customer Login Failed');
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 403);
}
$isExistEmail->one_signal_player_id = $request->one_signal_player_id;
$isExistEmail->save();
$response = [
'userId' => $isExistEmail->id,
'access_token' => $token,
];
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $response, 200);
} catch (QueryException $e) {
Log::error('Customer Login Failed ' . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 403);
}
}
public function forgotPassword($request)
{
try {
DB::beginTransaction();
$user = IamPrincipal::where('email_address', $request->email_address)->where('principal_type_xid', 3)->whereNull('deleted_at')->first();
if ($user == null) {
Log::error('Email not exist');
return jsonResponseWithErrorMessageApi(__('auth.incorrect_email'), 403);
}
$otp = generateOTP();
IamPrincipalOTP::updateOrCreate(
['principal_xid' => $user->id],
[
'otp_code' => $otp,
'otp_purpose' => 'forgot password',
'valid_till' => Carbon::now()->addMinutes(2),
'is_used' => 0,
]
);
$mail = Mail::send(
'frontend.Mail.customer_forgot_password_mail',
[
'user' => $user,
'otp_code' => $otp,
'valid_till' => Carbon::now()->addMinutes(2)
],
function ($message) use ($user) {
$message->to($user->email_address);
$message->subject('Forgot Password Mail Page');
}
);
DB::commit();
Log::info('Customer Forgot Password otp sent successfully');
$response = [
'iam_principal_xid' => $user->id,
];
return jsonResponseWithSuccessMessageApi(__('auth.otp_sent_successfully'), $response, 200);
} catch (QueryException $e) {
DB::rollBack();
Log::error('Customer Forgot password Failed ' . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 403);
}
}
public function verifyOTPForgotPassword($request)
{
try {
DB::beginTransaction();
$User = IamPrincipal::where('email_address', $request->email_address)->where('principal_type_xid', 3)->whereNull('deleted_at')->first();
$iamPrincipal = IamPrincipalOTP::where('principal_xid', $User->id)->first();
if (!$iamPrincipal) {
Log::error('User not exist');
return jsonResponseWithErrorMessageApi(__('auth.failed_to_verify_otp'), 403);
}
if ($iamPrincipal->otp_code !== $request->otp) {
Log::error('Customer entered invalid otp');
return jsonResponseWithErrorMessageApi(__('auth.invalid_otp'), 403);
}
if (Carbon::now()->gt($iamPrincipal->valid_till)) {
Log::error('Customer otp Exipred');
return jsonResponseWithErrorMessageApi(__('auth.otp_expired'), 403);
}
if ($iamPrincipal->is_used === 1) {
Log::error('Customer otp Already used');
return jsonResponseWithErrorMessageApi(__('auth.otp_already_used'), 403);
}
$iamPrincipal->is_used = 1;
$iamPrincipal->save();
DB::commit();
$response = [
'iam_principal_xid' => $User->id
];
Log::info('Customer OTP verified successfully');
return jsonResponseWithSuccessMessageApi(__('auth.otp_verified'), $response, 200);
} catch (QueryException $e) {
DB::rollBack();
Log::error('Customer verify otp Failed ' . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 403);
}
}
public function changePassword($request)
{
try {
DB::beginTransaction();
$User = IamPrincipal::find($request->iam_principal_xid);
$User->password = Hash::make($request->password);
$User->save();
DB::commit();
return jsonResponseWithSuccessMessageApi(__('auth.password_updated_successfully'));
} catch (QueryException $e) {
DB::rollBack();
Log::error('Customer change password Failed ' . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 403);
}
}
public function resendOtp($request)
{
try {
DB::beginTransaction();
$iamPrincipal = IamPrincipalOTP::where('principal_xid', $request->iam_principal_xid)->first();
$user = IamPrincipal::where('id', $request->iam_principal_xid)->first();
if (!$iamPrincipal) {
return response()->json('OTP not found for this user.', 203);
}
$allowedResendInterval = Carbon::now()->subMinutes(2);
if ($iamPrincipal->updated_at >= $allowedResendInterval) {
return jsonResponseWithErrorMessageApi(__('auth.try_resend_otp'), 429);
}
$otp = generateOTP();
$iamPrincipal->principal_xid = $request->iam_principal_xid;
$iamPrincipal->otp_code = $otp;
$iamPrincipal->otp_purpose = $request->otp_purpose;
$iamPrincipal->valid_till = Carbon::now()->addMinutes(2);
$iamPrincipal->is_used = 0;
$iamPrincipal->save();
$mail = Mail::send(
'frontend.Mail.customer_forgot_password_mail',
[
'user' => $user,
'otp_code' => $otp,
'valid_till' => Carbon::now()->addMinutes(2)
],
function ($message) use ($user) {
$message->to($user->email_address);
$message->subject('Forgot Password Mail Page');
}
);
DB::commit();
$response = [
'iam_principal_xid' => $iamPrincipal->principal_xid,
'email_address' => $user->email_address
];
return jsonResponseWithSuccessMessageApi(__('auth.otp_resend_sent_successfully'), $response, 200);
} catch (QueryException $e) {
DB::rollBack();
Log::error('Resend otp Failed ' . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 403);
}
}
}

View File

@@ -1,5 +1,9 @@
<?php
use Carbon\Laravel\ServiceProvider;
use Illuminate\Support\Facades\Facade;
return [
/*
@@ -53,6 +57,8 @@ return [
*/
'url' => env('APP_URL', 'http://localhost'),
'asset_url' => env('ASSET_URL'),
/*
|--------------------------------------------------------------------------
@@ -78,34 +84,42 @@ return [
|
*/
'locale' => env('APP_LOCALE', 'en'),
'locale' => 'en',
'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),
'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'),
'fallback_locale' => 'en',
/*
|--------------------------------------------------------------------------
| Faker Locale
|--------------------------------------------------------------------------
|
| This locale will be used by the Faker PHP library when generating fake
| data for your database seeds. For example, this will be used to get
| localized telephone numbers, street address information and more.
|
*/
'faker_locale' => 'en_US',
/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is utilized by Laravel's encryption services and should be set
| to a random, 32 character string to ensure that all encrypted values
| are secure. You should do this prior to deploying the application.
| This key is used by the Illuminate encrypter service and should be set
| to a random, 32 character string, otherwise these encrypted strings
| will not be safe. Please do this before deploying an application!
|
*/
'cipher' => 'AES-256-CBC',
'key' => env('APP_KEY'),
'previous_keys' => [
...array_filter(
explode(',', env('APP_PREVIOUS_KEYS', ''))
),
],
'cipher' => 'AES-256-CBC',
/*
/*
|--------------------------------------------------------------------------
| Maintenance Mode Driver
|--------------------------------------------------------------------------
@@ -119,8 +133,61 @@ return [
*/
'maintenance' => [
'driver' => env('APP_MAINTENANCE_DRIVER', 'file'),
'store' => env('APP_MAINTENANCE_STORE', 'database'),
'driver' => 'file',
// 'store' => 'redis',
],
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/
'providers' => ServiceProvider::defaultProviders()->merge([
/*
* Package Service Providers...
*/
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
// App\Providers\AuthServiceProvider::class,
// Barryvdh\DomPDF\ServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
// App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
// Ladumor\OneSignal\OneSignalServiceProvider::class,
// Maatwebsite\Excel\ExcelServiceProvider::class,
])->toArray(),
/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/
'aliases' => Facade::defaultAliases()->merge([
// 'Example' => App\Facades\Example::class,
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
// 'OneSignal' => \Ladumor\OneSignal\OneSignal::class,
// 'PDF' => Barryvdh\DomPDF\Facade::class,
// 'Excel' => Maatwebsite\Excel\Facades\Excel::class,
])->toArray(),
];

View File

@@ -87,6 +87,7 @@ return [
'passport_search' => 'Passport Search successfully',
'not_found_otp' => 'OTP not found for this user',
'Rest_user_created' => 'Restaurant user created successfully',
'Customer_user_created' => 'Customer user created successfully',
'User_details_fetch' => 'User details fetch successfully',
'Voucher_not_found' => 'Voucher not found',
'delete_user' => 'Customer deleted successfully',

View File

@@ -0,0 +1,217 @@
<html>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" />
<head>
<style>
@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@100;200;300;400;500;600;700;800;900&display=swap");
@media only screen and (max-width: 767px) {
.main {
width: 320px !important;
}
.top-image {
width: 100% !important;
}
.inside-footer {
width: 320px !important;
}
table[class="contenttable"] {
width: 320px !important;
text-align: left !important;
}
td[class="force-col"] {
display: block !important;
}
td[class="rm-col"] {
display: none !important;
}
.mt {
margin-top: 15px !important;
}
*[class].width300 {
width: 255px !important;
}
*[class].block {
display: block !important;
}
*[class].blockcol {
display: none !important;
}
.emailButton {
width: 100% !important;
}
.emailButton a {
display: block !important;
font-size: 18px !important;
}
.side p {
width: 100%;
}
td.border {
width: auto !important;
}
tfoot td {
font-size: 10px;
}
.mktEditable p {
width: 100% !important;
}
}
@media only screen and (max-width: 600px) {
.main {
width: 320px !important;
}
.top-image {
width: 100% !important;
}
.inside-footer {
width: 320px !important;
}
table[class="contenttable"] {
width: 320px !important;
text-align: left !important;
}
td[class="force-col"] {
display: block !important;
}
td[class="rm-col"] {
display: none !important;
}
.mt {
margin-top: 15px !important;
}
*[class].width300 {
width: 255px !important;
}
*[class].block {
display: block !important;
}
*[class].blockcol {
display: none !important;
}
.emailButton {
width: 100% !important;
}
.emailButton a {
display: block !important;
font-size: 18px !important;
}
.side p {
width: 100%;
}
td.border {
width: auto !important;
}
tfoot td {
font-size: 10px;
}
.mktEditable p {
width: 100% !important;
}
}
</style>
</head>
<body>
<table class="main contenttable"
style="
display: flex;
align-items: center;
justify-content: center;
height: 100%;
">
<tbody>
<tr>
<td class="border"
style="
display: flex;
border: 1px solid #9d9a9a !important;
width: 535px;
">
<table>
<tbody>
<tr>
<td valign="top" class="side title">
<table>
<tbody>
<tr>
<td class="head-title"
style="display: flex;
justify-content: center;">
<!--background-color: #fcf6e4;-->
<div class="mktEditable" id="main_title">
{{-- <img src="{{ asset('src/assets/img/logo.png') }}" alt="user" /> --}}
</div>
</td>
</tr>
<tr>
<td class="grey-block"
style="
font-family: 'Montserrat', sans-serif;
padding: 20px 20px 0 20px;
color: #000;
font-size: 15px;
">
<div class="mktEditable" id="cta">
<p style="font-weight: 500">
One-Time Password (OTP) for verification
</p>
<p style="font-weight: 500">{{ 'Dear ' . $user->first_name }}
</p>
<p style="line-height: 20px">Your verification code is
{{ $otp_code }}.
</p>
<p>The otp is valid for 2 minutes</p>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
<!--<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js"></script>-->
</html>

View File

@@ -14,6 +14,10 @@ Route::post('/v1/register', [AuthController::class, 'register']);
Route::post('/v1/login', [AuthController::class, 'login']);
Route::post('/v1/forgot-password', [AuthController::class, 'forgotPassword']);
Route::post('/v1/password/verify-otp', [AuthController::class, 'verifyOtpForgotPassword']);
Route::post('/v1/change-password', [AuthController::class, 'changePassword']);
Route::post('/v1/resend-otp', [AuthController::class, 'resendOtp']);
// Route::group(['middleware' => ['customer.jwt.verify']], function () {
//*******************************************************CMS********************************************************
@@ -28,3 +32,6 @@ Route::get('/v1/list-of-news-articles', [CMSApiController::class, 'getNewsArticl
// });
// });