Laravel 的登陸認證

認證相關路由

# AuthRouteMethods.php

# 登陸退出
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');

# 註冊
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');

# 重置密碼
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
$this->post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update');
$this->get('password/confirm', 'Auth\ConfirmPasswordController@showConfirmForm')->name('password.confirm');
$this->post('password/confirm', 'Auth\ConfirmPasswordController@confirm');

# 郵箱驗證
$this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
$this->get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify');
$this->post('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');

相關概念

認證配置文件:config/auth.php。php

守衛 Guard:對用戶進行身份驗證,默認支持"session" 和 "token",能夠在守衛中設置使用的提供者。web

提供者 Provider:使用何種方式在數據庫中查找用戶,默認支持"eloquent" 和 "database",能夠在守衛中設置使用的模型類。數據庫

模型類:默認爲 App\User::classapi

配置文件

# config/auth.php

return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    'password_timeout' => 10800,
];

註冊邏輯

use Illuminate\Support\Facades\Auth;

$user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
$this->guard()->login($user);

登陸邏輯

$credentials = $request->only('email', 'password');
$credentials['active'] = 1;

if ($this->guard()->attempt($credentials, $request->filled('remember'))) {
    $request->session()->regenerate();
    return redirect()->intended('dashboard');
}

退出邏輯

$this->guard()->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();

自定義看守器

use Illuminate\Support\Facades\Auth;

protected function guard()
{
    // 默認:Auth::guard();
    // 看守器名稱須要與 auth.php 配置文件中的配置項之一相匹配
    return Auth::guard('guard-name');
}

獲取認證用戶

use Illuminate\Support\Facades\Auth;

// 獲取當前經過認證的用戶...
$user = Auth::user();

// 獲取當前經過認證的用戶 ID...
$id = Auth::id();

// 返回一個認證用戶實例...
use Illuminate\Http\Request;
$request->user();

檢查用戶是否已認證

use Illuminate\Support\Facades\Auth;

if (Auth::check()) {
    // 用戶已經登陸了...
}

其餘登陸方法

// 登陸
Auth::login($user);

// 登陸並記住給定用戶...
Auth::login($user, true);

// 指定看守器實例登陸
Auth::guard('admin')->login($user);

// 經過 ID 將用戶登陸到應用
Auth::loginUsingId(1);

//登陸並記住給定用戶...
Auth::loginUsingId(1, true);

// 僅驗證一次用戶身份,不使用 session 或 cookies
Auth::once($credentials)

保護路由

Laravel 自帶了一個 auth 中間件,定義在 Illuminate\Auth\Middleware\Authenticate 中。因爲這個中間件已經在 HTTP 內核中註冊,只需把這個中間件附加到路由定義中便可:數組

Route::get('profile', function () {
    // 只有認證過的用戶能夠進入...
})->middleware('auth');

# 或者
public function __construct()
{
    $this->middleware('auth');
}

auth 中間件添加到路由時,能夠同時指定使用哪一個看守器進行用戶認證。cookie

指定的看守器應該對應 auth.php 配置文件中 guards 數組中的的一個鍵:session

public function __construct()
{
    $this->middleware('auth:api');
}
相關文章
相關標籤/搜索