You should first log to see if your code to change the password is activated and then have a look in your db if it's changed.
Aug 17, 2022
2
Level 1
In Laravel, forgot API- password is not updating
I make a forgot password API and when i hit that it send me a mail, when i open that mail and click on that link one form shown, and get new password and confirm password. after that when i submit the form and check my new password through login it says incorrect credentials it's mean my password won't change. can anyone please help me , why password is not not updating.
HERE IS MY USER CONTROLLER WHERE I CREATE FUNCTION FOR FORGOT PASSWORD API:
public function forget_password( Request $request)
{
try{
$user= User::where('email',$request->email)->get();
if( count($user) > 0)
{
$token = str::random(40);
$domain = URL::to('/');
$url= $domain.'/reset-password?token='.$token;
$data['url']=$url;
$data['email']=$request->email;
$data['title']='Reset Password';
$data['body']='Please click on below link to reset your password.';
Mail::send('forgot_password',['data'=>$data], function($message) use($data){
$message->to($data['email'])->subject($data['title']);
}
);
$datetime = Carbon:: now()->format('Y-m-d H:i:s');
PasswordReset::updateOrCreate(
['email'=>$request->email],
[
'email'=>$request->email,
'token'=>$token,
'created_at'=>$datetime
]
);
return response()->json(
[
'success'=>true,
'msg'=>'Please check your mail to reset your password'
]);
}
else{
return response()->json([
'success'=>'false',
'msg'=>'user not found',
]);
}
}
catch(\Exception $e){
return response()->json([
'success'=>'false',
'msg'=> $e->getMessage()
]);
}
}
//reset password view load
public function reset_password_load(Request $request){
$resertdata= PasswordReset::where('token',$request->token)->get();
if(isset($request->token)&& count($resertdata)>0){
$user =User::where('email',$resertdata[0]['email'])->get();
return view('resetPassword', compact('user'));
}
else{
return view('404');
}
}
//password reset functionality
public function reset_password(Request $request){
$request->validate([
'password'=>'required|string|min:6|Confirmed'
]);
$user = User::find($request->id);
$user->password = \Hash::make($request->password);
$user->save(); //or $user->save();
PasswordReset::where('email' , $user->email)->delete();
return "<h1>Your password has been reset successfully </h1> ";
}
}
HERE IS THE ROUTES ;
ONE FOR API ROUTE :
Route::post('/forget_password', [UserController::class,'forget_password']);
TWO FOR WEB ROUTE:
Route::get('/reset-password', [UserController::class,'reset_password_load']);
Route::post('/reset-password', [UserController::class,'reset_password']);
HERE IS MY BLADE FILES:
This is for email sending
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>{{$data['title']}}</title>
</head>
<body>
<p>{{$data['body']}}</p>
<a href="{{$data['url']}}" >Click here to reset password</a>
<P>Thank You!</p>
</body>
</html>
This is for updating password:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
@if($errors->any())
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
@endif
<form method="POST" action="/reset-password">
@csrf
<input type="hidden" name="id" value="{{$user[0]['id']}}" ></input>
< br><br>
<input type="password" name="password" placeholder="New Password"></input>
<br><br>
<input type="password" name="confirm_password" placeholder="Confirm Password"></input>
<br><br>
<input type="submit"></input>
</form>
</body>
</html>
Please or to participate in this conversation.