LoginSignup
0
3

More than 1 year has passed since last update.

【Laravel】ログイン時の追加処理

Posted at

ログイン時に追加処理をするためのメモ。

前提

バージョン:Laravel6

LoginControlerの中身

LoginController.php
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

デフォルトの状態ではログインコントローラーは上のようになっている。
今回重要なのはuse AuthenticatesUsers;。ログイン時の処理内容も本体はこのAuthenticatesUsersトレイトの中にある。
AuthenticatesUsersvendor/laravel/framework/Illuminate/Foundation/Authの中にある。

AuthenticatesUsers.phpから抜粋
public function login(Request $request)
{
    /* 割愛 */

    if ($this->attemptLogin($request)) {
        return $this->sendLoginResponse($request);
    }

    /* 割愛 */
}

protected function sendLoginResponse(Request $request)
{
    /* 割愛 */

    return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->redirectPath());
}

protected function authenticated(Request $request, $user)
{
    //
}

AuthenticatesUsersの中を見るとloginではログインをする(attemptLogin)ときにsendloginResponseが呼び出されている。
さらにsendloginResponseではauthenticatedredirect()...が三項演算子?:で比較されている。
つまり、authenticatedを実行してその返り値が存在しない、またはfalseの場合はredirect()...でログイン先のページへと移動する。
このauthenticatedは中身が空なのでLoginController側で上書きすればログイン実行時の処理を追加できる。
なお、$requestはログインフォームの入力値、$userはログインしたユーザーが入る。
返り値があればredirect()...は実行されないので、ユーザーごとにログイン先を変えるといった処理も可能。
注意点として、use Illuminate\Http\Request;も追記しないとLoginController側でRequestが呼び出だせずエラーになってしまう。

サンプル ログイン時の時間を記録する

usersテーブルにlogin_atカラムを追加してユーザーごとにログインした時間を記録する。

①マイグレーション

add_login_at_to_users_table.php
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dateTime('login_at')->nullable();
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('login_at');
    });
}

②コントローラー

LoginController.php
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request; // 追記

    /* 割愛 */

protected function authenticated(Request $request, $user)
{
  $user->login_at = now();
  $user->save();
}
0
3
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
0
3