Last active
April 27, 2019 06:58
-
-
Save oxycoder/7fe6ac9708a0e1ec90c0f624d76fb8a1 to your computer and use it in GitHub Desktop.
Laravel 5.4 Customize Password Reset with DingoApi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Password; | |
use Illuminate\Contracts\Hashing\Hasher as HasherContract; | |
class AuthController extends BaseController | |
{ | |
protected $hasher; | |
public function __construct(HasherContract $hasher) | |
{ | |
$this->hasher = $hasher; | |
} | |
/* | |
* POST | |
*/ | |
public function sendResetLinkEmail(Request $request) | |
{ | |
$broker = $this->getBroker(); | |
$response = Password::broker($broker)->sendResetLink($request->only('email'), function (Message $message) { | |
$message->subject('Your password reset link'); | |
}); | |
switch ($response) { | |
case Password::RESET_LINK_SENT: | |
return response()->json([ | |
'result' => 'success' | |
]); | |
case Password::INVALID_USER: | |
default: | |
$this->response->error('Invalid email or email does not exist', 422); | |
} | |
} | |
/* | |
* POST | |
*/ | |
public function resetpw(PasswordResetRequest $request) | |
{ | |
$token = $request->get('token'); | |
$email = $request->get('email'); | |
$password = $request->get('password'); | |
$reset = PasswordResets::where('email', $email)->first(); | |
if(count($reset) > 0) { | |
if($this->hasher->check($token, $reset->token)) { | |
$user = User::where('email', '=', $email)->first(); | |
$user->password = YOUR_PASSWORD_HASH_FUNCTION. Laravel use Bcrypt by default | |
$user->save(); | |
$reset->delete(); | |
return response()->json([ | |
'result' => 'success' | |
]); | |
} | |
} | |
$this->response->error('Invalid token or email or expired code', 422); | |
} | |
function getBroker() | |
{ | |
return property_exists($this, 'broker') ? $this->broker : null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Notifications; | |
use Illuminate\Bus\Queueable; | |
use Illuminate\Notifications\Notification; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use Illuminate\Notifications\Messages\MailMessage; | |
class ResetPasswordNotification extends Notification | |
{ | |
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) | |
{ | |
return (new MailMessage) | |
->line('Click the link below to reset your password..') | |
->action('Reset Password', 'https://abc.com/reset?token=' . $this->token); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
class PasswordResets extends Model { | |
/** | |
* Define primary key for delete() function works. | |
*/ | |
protected $primaryKey = 'email'; | |
protected $table = 'password_resets'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use App\Notifications\ResetPasswordNotification; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Database\Eloquent\Model; | |
class User extends Authenticatable { | |
use Notifiable; | |
/* | |
* Override default password notification | |
*/ | |
public function sendPasswordResetNotification($token) | |
{ | |
$this->notify(new ResetPasswordNotification($token)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment