43
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravel: パスワードリセット by Auth

Last updated at Posted at 2020-02-09

参考

【laravel】メール(gmail)がエラーで送信出来ない問題
Laravelパスワードリセットメールの文面・デザインを変更する
Laravel5.6でバリデーションなどのエラーメッセージを日本語化する方法
laravel-resources-lang-ja
Laravel 5.6 パスワードリセット
php - Laravel 58でカスタムPasswordBroker sendResetLink()メソッドを拡張または作成する方法は?

.env
//メール送信設定
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME={address_id}@gmail.com
MAIL_PASSWORD={mail_pass}
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS={email_address}
MAIL_FROM_NAME={name}

ここまでで下記画面からのパスワードリセットメールが送信できるようになります。
WS000007.JPG

パスワードリセットメールのテンプレートビュー

resource/views/mail/passwordreset.blade.php
//参考サイト様より
<!DOCTYPE html>
<html lang="ja">
<style>
  body { 
    background-color: #fffacd;
  }
  h1 {
    font-size: 16px;
    color: #ff6666;
  }
  #button {
    width: 200px;
    text-align: center;
  }
  #button a {
    padding: 10px 20px;
    display: block;
    border: 1px solid #2a88bd;
    background-color: #FFFFFF;
    color: #2a88bd;
    text-decoration: none;
    box-shadow: 2px 2px 3px #f5deb3;
  }
  #button a:hover {
    background-color: #2a88bd;
    color: #FFFFFF;
  }
</style>
<body>
<h1>
  パスワードリセット
</h1>
<p>
  以下のボタンを押下しパスワードリセットの手続きを行ってください
</p>
<p id="button">
  <a href="{{$reset_url}}">パスワードリセット</a>
</p>
</body>
</html>

通知クラス(Notification)を継承するパスワードリセットクラスの作成

$ php artisan make:notification PasswordResetNotification
app/Notification/PasswordResetNotification.php
//追加
public $token;
protected $title = 'パスワードリセット 通知';

//変更
public function __construct($token)
{
    $this->token = $token;
}

public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject($this->title)
        ->view('mail.passwordreset',
            [
                'reset_url' => url('password/reset', $this->token),
            ]);
}

作成したPasswordResetNotificationクラスを利用するために。

app/User.php
//追加
use App\Notifications\PasswordResetNotification;

//メソッドのオーバーライド
public function sendPasswordResetNotification($token)
{
    $this->notify(new PasswordResetNotification($token));
}

これでパスワードリセットできるようになります。

カスタマイズ

期限の変更設定

config/auth.php
//expireオプションで設定
'passwords' => [                     
    'users' => [                     
        'provider' => 'users',       
        'table' => 'password_resets',
        'expire' => 30, // <- ココ
    ],                               
],                                   

パスワードリセット後のリダイレクト先

app/Http/Controller/Auth/ResetPasswordController.php
//プロパティを変更
protected $redirectTo = '/home';

フラッシュメッセージの日本語化

resource/lang/ja/password.php
<?php
    return [
        'password' => 'パスワードは6文字以上にして、確認用入力欄と一致させてください。',
        'reset' => 'パスワードは再設定されました!',
        'sent' => 'パスワード再設定用のURLをメールで送りました。',
        'token' => 'パスワード再設定用のトークンが不正です。',
        'user' => "メールアドレスに一致するユーザーが存在しません。",
    ];

メールアドレスが見つからなくても同じメッセージを出力

app/Http/Controller/Auth/ForgetPasswordController.php
//追加
use Illuminate\Http\Request;
//オーバーライド
public function sendResetLinkEmail(Request $request)
{
    $this->validateEmail($request);
    $response = $this->broker()->sendResetLink(
        $request->only('email')
    );
    return back()->with('status', 'パスワード再設定用のURLをメールで送りました。');
}

LGTMお願いします!
ストックのついでにお願いします!
モチベーションがあがります!

43
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?