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');
}
相关文章
相关标签/搜索