1

Laravel 的登录认证

 3 years ago
source link: https://segmentfault.com/a/1190000040044936
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

认证相关路由

# 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。

守卫 Guard:对用户进行身份验证,默认支持"session" 和 "token",可以在守卫中设置使用的提供者。

提供者 Provider:使用何种方式在数据库中查找用户,默认支持"eloquent" 和 "database",可以在守卫中设置使用的模型类。

模型类:默认为 App\User::class

# 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 中间件添加到路由时,可以同时指定使用哪个看守器进行用户认证。

指定的看守器应该对应 auth.php 配置文件中 guards 数组中的的一个键:

public function __construct()
{
    $this->middleware('auth:api');
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK