Reset password for admin or user manually on Laravel

43 0 0 0

Last Updated : 2020-10-24 09:50:37

Here is a brief overview on how to implement a reset password manually for users in Laravel application.

    public function forgetPassword(Request $request) {
if($request->method() == 'GET') {
//Show reset password form
return view("auth/passwords/adminEmail") ;

} else {

//--------------------------------------
$user = Admin::where ('email', $request->email)->first();
if ( !$user ) return redirect()->back()->withErrors(['error' => '404']);

//create a new token to be sent to the user.

DB::delete("delete from password_resets where email = '{$request->email}'");
DB::table('password_resets')->insert([
'email' => $request->email,
'token' => str_random(60), //change 60 to any length you want
'created_at' => Carbon::now()
]);

$tokenData = DB::table('password_resets')
->where('email', $request->email)->first();

$token = $tokenData->token;
$email = $request->email; // or $email = $tokenData->email;

/**
* Send email to the email above with a link to your password reset
* something like url('password-reset/' . $token)
* Sending email varies according to your Laravel version. Very easy to implement
*/
//--------------------------------------

//mail($request->email,"Reset your password","Here is your token : $token") ;
$to_name = 'Mohammed';
$to_email = $request->email;
$data = array('name'=>$to_name, 'token'=>$token, "body" => "Here is your token : $token");

Mail::send('emails.resetAdminpassword', $data, function($message) use ($to_name, $to_email) {
$message->to($to_email, $to_name)
->subject('Reset your password');
//$message->from('info@arablancers.website','Chamber Stream');
//$message->from('notifications@sahaly.info','Chamber Stream');
});

//return view("auth/passwords/adminEmail", )
return Redirect()->back()->with('status', 'An email has been sent to your email address, Click the reset passwork link there');
}
}

public function resetPassword(Request $request, $token) {
if($request->method() == 'GET') {
//Show reset password form
//ECHO 'Reset Get';

$tokenData = DB::table('password_resets')
->where('token', $token)->first();

if ( !$tokenData ) return redirect()->to('home'); //redirect them anywhere you want if the token does not exist.
return view('auth/passwords/adminReset', compact(['token']));
} else {
//ECHO "Reset post";

$this->validate($request, [
'token' => 'required',
'password' => 'required|confirmed|min:6',
]);

$password = $request->password;
$tokenData = DB::table('password_resets')
->where('token', $token)->first();

$user = Admin::where('email', $tokenData->email)->first();
if ( !$user ) return redirect()->to('home'); //or wherever you want

$user->password = Hash::make($password);
$user->update(); //or $user->save();

//do we log the user directly or let them login and try their password for the first time ? if yes
//Auth::login($user);

// If the user shouldn't reuse the token later, delete the token
DB::table('password_resets')->where('email', "$user->email")->delete();

//redirect where we want according to whether they are logged in or not.
return Redirect(url("admin/login"))->with('status', "Your password has been successfully updated, Please login");

}
}

Mohammed Anwar

Mohammed Anwar

Experienced technical lead PHP, MySQL and Laravel Developer for 15+ years, with proven ability to develop and create high-quality and optimized web applications. Great ability to build and optimize database design, schema and queries. Versed programing trainer and instructor delivering web courses and helping others to get into the field in a timely manner. Fast and eager learner for new technologies .