In first step, we require to update the migration file database/migrations/xxx_create_password_resets_table.php like bellow code:
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->index();
$table->string('token');
$table->timestamps();
});
}
Step 2. Create PasswordReset model
Open your terminal or command prompt and run bellow command:
This command will create app/PasswordReset.php file, in this file set fillable inputs.
Step 3. Create Notifications
We create two notifications PaswordResetRequest and PasswordResetSuccess, in your terminal or command prompt run bellow commands:
This command will create app/Notifications/PasswordResetRequest.php and app/Notifications/PasswordResetSuccess.php files.
In the PasswordResetRequest.php file add the next code:
In the PasswordResetSuccess.php file add the next code:
Step 4. Create API Routes
We will create api routes. Laravel provide routes/api.php file for write web services route. So, let’s add new route on that file.
Step 5: Install Dependencies
We use Carbon package to help with dates, in your terminal run bellow command:
Step 6: Create Controller
In this step we have to create new controller and three api method. So let’s create PasswordResetController and put bellow code:
Now we are ready to run our example so run bellow command to quick run:
Tests
Now, we can simple test by rest client tools (Postman), So I test it and you can see below screenshots.
In this api you have to set two header as listed below:
Request Password Reset
When request password reset will created token in data base
When request password reset will receive a email with the link to Reset Password.
Find Password Reset
If the token is active response with token data
If the token is not active the response is a 404 error
Reset Password
If the token is active response with User data
If the token is not active the response is a 404 error
Thanks for reading! I’m Alfredo Barrón, Feel free to connect with me via Twitter.
<?phpnamespace App\Notifications;use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;class PasswordResetRequest extends Notification implements ShouldQueue
{
use Queueable; protected $token; /**
* Create a new notification instance.
*
* @return void
*/
public function __construct($token)
{
$this->token = $token;
} /**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
} /**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$url = url('/api/password/find/'.$this->token); return (new MailMessage)
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', url($url))
->line('If you did not request a password reset, no further action is required.');
} /**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
<?phpnamespace App\Notifications;use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;class PasswordResetSuccess extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
} /**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
} /**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('You are changed your password successful.')
->line('If you did change password, no further action is required.')
->line('If you did not change password, protect your account.');
}/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}