Tuesday, April 19, 2022

Vue Js Reset Password With Laravel API

 In this article, we will discuss “How to make Vue Js Reset Password With Laravel API”. In our previous article on this series, you will learn Laravel JWT Authentication and Vue Js application setup with Vue Auth and Laravel JWT Auth. I will recommend you to read out both of the previous parts for better understanding. You can skip those if you are looking only for the specific User Reset Password functionality using Laravel API’s.

Illuminate\Foundation\Auth\SendsPasswordResetEmails;
Illuminate\Foundation\Auth\ResetsPasswords;
/**
* Send password reset link.
*/
public function sendPasswordResetLink(Request $request)

return $this->sendResetLinkEmail($request);
/**
* Get the response for a successful password reset link.
*
* @param \Illuminate\Http\Request $request
* @param string $response
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
protected function sendResetLinkResponse(Request $request, $response)

return response()->json([
'message' => 'Password reset email sent.',
'data' => $response
]);


/**
* Get the response for a failed password reset link.
*
* @param \Illuminate\Http\Request $request
* @param string $response
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
protected function sendResetLinkFailedResponse(Request $request, $response)

return response()->json(['message' => 'Email could not be sent to this email address.']);
php artisan make:notification MailResetPasswordNotification
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

use Illuminate\Auth\Notifications\ResetPassword;

class MailResetPasswordNotification extends ResetPassword

use Queueable;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($token)

parent::__construct($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)

$link = url( "/reset-password/".$this->token );
return ( new MailMessage )
->subject( 'Reset Password Notification' )
->line( "Hello! You are receiving this email because we received a password reset request for your account." )
->action( 'Reset Password', $link )
->line( "This password reset link will expire in ".config('auth.passwords.users.expire')." minutes" )
->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 [
//
];
/**
* Override the mail body for reset password notification mail.
*/
public function sendPasswordResetNotification($token)

$this->notify(new \App\Notifications\MailResetPasswordNotification($token));
Route::prefix('v1')->group(function () 
Route::prefix('auth')->group(function ()
...
// Send reset password mail
Route::post('reset-password', 'AuthController@sendPasswordResetLink');

// handle reset password form process
Route::post('reset/password', 'AuthController@callResetPassword');
...
);
);
/**
* Handle reset password
*/
public function callResetPassword(Request $request)

return $this->reset($request);
/**
* Reset the given user's password.
*
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
* @param string $password
* @return void
*/
protected function resetPassword($user, $password)

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

event(new PasswordReset($user));
use Hash;
use Illuminate\Auth\Events\PasswordReset;
/**
* Get the response for a successful password reset.
*
* @param \Illuminate\Http\Request $request
* @param string $response
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
protected function sendResetResponse(Request $request, $response)

return response()->json(['message' => 'Password reset successfully.']);


/**
* Get the response for a failed password reset.
*
* @param \Illuminate\Http\Request $request
* @param string $response
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
protected function sendResetFailedResponse(Request $request, $response)

return response()->json(['message' => 'Failed, Invalid Token.']);
const routes = [
...

path: '/reset-password',
name: 'reset-password',
component: ForgotPassword,
meta:
auth:false

,

path: '/reset-password/:token',
name: 'reset-password-form',
component: ResetPasswordForm,
meta:
auth:false


...
]
<template>
<div class="container">
<div class="row justify-content-center">
<div class="col-6">
<div class="card card-default">
<div class="card-header">Reset Password</div>
<div class="card-body">
<form autocomplete="off" @submit.prevent="requestResetPassword" method="post">
<div class="form-group">
<label for="email">E-mail</label>
<input type="email" id="email" class="form-control" placeholder="user@example.com" v-model="email" required>
</div>
<button type="submit" class="btn btn-primary">Send Password Reset Link</button>
</form>
</div>
</div>
</div>
</div>
</div>
</template>

<script>
export default
data()
return
email: null,
has_error: false

,
methods:
requestResetPassword()
this.$http.post("/auth/reset-password", email: this.email).then(result =>
this.response = result.data;
console.log(result.data);
, error =>
console.error(error);
);



</script>
Vue Js Reset Password With Laravel API
Vue Js Reset Password With Laravel API
<template>
<div class="container">
<div class="row justify-content-center">
<div class="col-6">
<div class="card card-default">
<div class="card-header">New Password</div>
<div class="card-body">
<!-- <ul v-if="errors">
<li v-for="error in errors" v-bind:key="error"> msg </li>
</ul> -->
<form autocomplete="off" @submit.prevent="resetPassword" method="post">
<div class="form-group">
<label for="email">E-mail</label>
<input type="email" id="email" class="form-control" placeholder="user@example.com" v-model="email" required>
</div>
<div class="form-group">
<label for="email">Password</label>
<input type="password" id="password" class="form-control" placeholder="" v-model="password" required>
</div>
<div class="form-group">
<label for="email">Confirm Password</label>
<input type="password" id="password_confirmation" class="form-control" placeholder="" v-model="password_confirmation" required>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
</div>
</div>
</div>
</template>

<script>
export default
data()
return
token: null,
email: null,
password: null,
password_confirmation: null,
has_error: false

,
methods:
resetPassword()
this.$http.post("/auth/reset/password/",
token: this.$route.params.token,
email: this.email,
password: this.password,
password_confirmation: this.password_confirmation
)
.then(result =>
// console.log(result.data);
this.$router.push(name: 'login')
, error =>
console.error(error);
);



</script>
Vue Js Reset Password With Laravel API

Monday, April 11, 2022

NVM Cheat Sheet

 Instructions for installing the Node Version Manager on Mac or Ubuntu. The nvm tool can be used to choose a specific version of NodeJs/npm.

Install nvm

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

See https://github.com/creationix/nvm#install-script.

Tips And Tricks

$ nvm ls-remote                 # lists all of the available versions of NodeJs & iojs
$ nvm ls                        # list locally installed version
$ nvm install 0.12.3            # install the version 0.12.3 (see ls-remote for available options)
$ nvm use 0.12.3                # switch to and use the installed 0.12.3 version
$ nvm which 0.12.2              # the path to the installed node version
$ nvm current                   # what is the current installed nvm version
$ nvm alias default 0.10.32     # set the default node to the installed 0.10.32 version
$ nvm --help                    # the help documents