πŸš€ DevOps Certified Professional
πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 | 🀝 +1 (469) 756-6329 πŸ” Contact@DevOpsSchool.com

How do I manually send a password reset request in Laravel?

DevOps

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

This is like this for your controller:

<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Password;
class YourController extends Controller
{
public function sendEmail()
{
$credentials = ['email' => $email_address];
$response = Password::sendResetLink($credentials, function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response));
case Password::INVALID_USER:
return redirect()->back()->withErrors(['email' => trans($response)]);
}
}
}

 I only added a token to his answer. this works just fine:

$user = User::where('email', request()->input('email'))->first();
$token = Password::getRepository()->create($user);
$user->sendPasswordResetNotification($token);

Complete control for Laravel 5.5:

$user = User::where('email', request()->input('email'))->first();
$token = Password::getRepository()->create($user);
Mail::send(['text' => 'emails.password'], ['token' => $token], function (Message $message) use ($user) {
$message->subject(config('app.name') . ' Password Reset Link');
$message->to($user->email);
});

The easiest way:

$token = Str::random(60);
$user = User::where('email', request()->input('email'))->first();
$user->sendPasswordResetNotification($token);

And if you want to edit your e-mail manually:

 php artisan vendor:publish

Select β€œ11” gives you:

/resources/views/vendor/notifications/email.blade.php

Use this code to send the password reset link. Here I am API Route for this.

$this->validate($request, [
'user_id' => 'required|int',
]);
$user = User::find($request->user_id);
if( $user )
{
$credentials = ['email' => $user->email];
$response = Password::sendResetLink($credentials, function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
return response()->json([
'status' => 'success',
'message' => 'Password reset link send into mail.',
'data' =>''], 201);
case Password::INVALID_USER:
return response()->json([
'status' => 'failed',
'message' => 'Unable to send password reset link.'
], 401);
}
}
return response()->json([
'status' => 'failed',
'message' => 'user detail not found!'
], 401);

for Web View :

$response = Password::sendResetLink($credentials, function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response));
case Password::INVALID_USER:
return redirect()->back()->withErrors(['email' => trans($response)]);
}
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x