Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] Fix Forget Password Endpoint#87 #88

Merged
merged 6 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

Expand Down Expand Up @@ -55,8 +56,12 @@ public function render($request, Throwable $exception)
return response()->json(['error' => 'Resource not found'], Response::HTTP_NOT_FOUND);
}

if ($exception instanceof ValidationException) {
return response()->json(['error' => $exception->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}

if ($exception instanceof HttpException) {
return response()->json(['error' => 'Somethings wrong with the server'], Response::HTTP_INTERNAL_SERVER_ERROR);
return response()->json(['error' => 'Somethings wrong with the server: '.$exception->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}

return parent::render($request, $exception);
Expand Down
72 changes: 0 additions & 72 deletions app/Http/Controllers/api/ForgetController.php

This file was deleted.

59 changes: 27 additions & 32 deletions app/Http/Controllers/api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use App\Models\Code;
use App\Http\Requests\ForgetRequest;
use App\Http\Requests\ResetRequest;
use App\Mail\ForgetMail;
use App\Mail\ForgetPasswordMail;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Exception;
Expand Down Expand Up @@ -121,8 +121,8 @@ private function is_usedUpdated($code, $userId)
* @OA\Post(
* path="/forget-password",
* tags={"User"},
* summary="send email to recovery password",
* description="This endpoint is used send an email to a register user to reset the password.",
* summary="send email to reset password",
* description="This endpoint is used to send an email to a registered user to reset the password.",
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
Expand All @@ -138,11 +138,11 @@ private function is_usedUpdated($code, $userId)
* ),
* @OA\Response(
* response="200",
* description="check your email"
* description="Password reset email sent out. Check your email"
* ),
* @OA\Response(
* response="404",
* description="The email don\'t exist"
* description="The email does not exist"
* )
* )
*/
Expand All @@ -151,37 +151,32 @@ public function forgetPassword(ForgetRequest $request){

$email = $request->email;

$user= User::where('email',$email)->doesntExist();

$token= Str::random(10);

$existingMail = DB::table('password_reset_tokens')->where('email', $email)->first();


try{
// check if user with such email exists
$user= User::where('email',$email)->first();

if($user){
return response()->json(['error' => 'The email don\'t exist'],404);

}else if($existingMail){
if(!$user){
return response()->json(['error' => 'The email does not exist'],404);
}

DB::table('password_reset_tokens')->where('email', $email)->update([
'token' => $token,
]);
// Generate password reset token
$token= Str::random(10);

// Assign password reset token to user's email in 'password_reset_token' table
if(DB::table('password_reset_tokens')->where('email', $email)->first()) {
DB::table('password_reset_tokens')->where('email', $email)->update([ 'token' => $token, ]);
} else {

DB::table('password_reset_tokens')->insert([
'email' => $email,
'token' => $token
]);
}
]);
};

//send email
//send password reset email
Mail::to($email)->send(new ForgetPasswordMail($user->name, $token));

Mail::to($email)->send(new ForgetMail($token));

return response()->json(['message'=>'check your email'],200);
// send confirmation response
return response()->json(['message'=>'Password reset email sent out. Check your email'],200);


}catch(Exception $exception){
Expand All @@ -196,8 +191,8 @@ public function forgetPassword(ForgetRequest $request){
* @OA\Post(
* path="/reset-password/{token}",
* tags={"User"},
* summary="User recovery password",
* description="This endpoint is used to update the password of the user.",
* summary="Reset user password",
* description="This endpoint is used to reset the user password.",
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
Expand All @@ -214,7 +209,7 @@ public function forgetPassword(ForgetRequest $request){
* example="password"
* ),
* @OA\Property(
* property="password_confirm",
* property="password_confirmation",
* type="string",
* example="password"
* ),
Expand All @@ -223,7 +218,7 @@ public function forgetPassword(ForgetRequest $request){
* ),
* @OA\Response(
* response="200",
* description="success"
* description="User password reset successfully"
* ),
* @OA\Response(
* response="400",
Expand All @@ -249,10 +244,10 @@ public function resetPassword(ResetRequest $request){
$user= User::where('email',$passwordResets->email)->first();
$user->password = Hash::make($request->password);
$user->save();
DB::table('password_reset_tokens')->where('email', $passwordResets->email)->update(['token' => null]);
DB::table('password_reset_tokens')->where('email', $passwordResets->email)->delete();

return response()->json([
'message' => 'success'
'message' => 'User password reset successfully'
],200);

}
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Requests/ResetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function authorize(): bool
public function rules(): array
{
return [
'password' =>'required',
'password_confirm' => 'required|same:password'
'password' => 'required|string|min:8|confirmed'
];
}
}
15 changes: 8 additions & 7 deletions app/Mail/ForgetMail.php → app/Mail/ForgetPasswordMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@



class ForgetMail extends Mailable
class ForgetPasswordMail extends Mailable
{
use Queueable, SerializesModels;

public $name;
public $token;


/**
* Create a new message instance.
*/
public function __construct($token)
public function __construct($name, $token)
{
$this->data = $token;
$this->name = $name;
$this->token = $token;
}

/**
Expand All @@ -31,7 +34,7 @@ public function __construct($token)
public function envelope(): Envelope
{
return new Envelope(
subject: 'Password Reset',
subject: 'Please reset your password',
);
}

Expand All @@ -41,10 +44,8 @@ public function envelope(): Envelope
public function content(): Content
{

$data = $this->data;

return new Content(
view: 'mail.forget',
view: 'mail.forgetPassword',
);
}

Expand Down
Loading