自定义登录

未匹配的标注

自定义登录

重写登录页面和登录逻辑

方式一,重写登录控制器方法:

默认的登录控制器用的是App\Admin\AuthController这个类,可以通过配置参数admin.auth.controller进行修改

<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Controllers\AuthController as BaseAuthController;

class AuthController extends BaseAuthController
{
    // 自定义登录view模板
    protected $view = 'admin.login';

    // 重写你的登录页面逻辑
    public function getLogin(Content $content)
    {
        ...
    }

    ...
}

方式二,覆写路由:

在路由文件app/Admin/routes.php中,覆盖掉登录页面和登录逻辑的路由,即可实现自定义的功能

Route::group([
    'prefix'        => config('admin.prefix'),
    'namespace'     => Admin::controllerNamespace(),
    'middleware'    => ['web', 'admin'],
], function (Router $router) {

    $router->get('auth/login', 'AuthController@getLogin');
    $router->post('auth/login', 'AuthController@postLogin');

});

在自定义的路由器AuthController中的getLoginpostLogin方法里分别实现自己的登录页面和登录逻辑。

重写laravel认证

如果不使用Dcat Admin内置的认证登录逻辑,可以参考下面的方式自定义登录认证逻辑

首先要先定义一个user provider,用来获取用户身份, 比如app/Providers/CustomUserProvider.php

<?php

namespace App\Providers;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;

class CustomUserProvider implements UserProvider
{
    public function retrieveById($identifier)
    {}

    public function retrieveByToken($identifier, $token)
    {}

    public function updateRememberToken(Authenticatable $user, $token)
    {}

    public function retrieveByCredentials(array $credentials)
    {
        // 用$credentials里面的用户名密码去获取用户信息,然后返回Illuminate\Contracts\Auth\Authenticatable对象
    }

    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        // 用$credentials里面的用户名密码校验用户,返回true或false
    }
}

在方法retrieveByCredentialsvalidateCredentials中, 传入的$credentials就是登录页面提交的用户名和密码数组,然后你可以使用$credentials去实现自己的登录逻辑

Interface Illuminate\Contracts\Auth\Authenticatable的定义如下:

<?php

namespace Illuminate\Contracts\Auth;

interface Authenticatable {

    public function getAuthIdentifierName();
    public function getAuthIdentifier();
    public function getAuthPassword();
    public function getRememberToken();
    public function setRememberToken($value);
    public function getRememberTokenName();

}

上面interface每个方法的解释参考adding-custom-user-providers

定义好了User provider之后,打开app/Providers/AuthServiceProvider.php注册它:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Auth::provider('custom', function ($app, array $config) {

            // Return an instance of Illuminate\Contracts\Auth\UserProvider...
            return new CustomUserProvider();
        });
    }
}

最后修改一下配置,打开config/admin.php,找到auth部分修改:

    'auth' => [
        'guards' => [
            'admin' => [
                'driver' => 'session',
                'provider' => 'admin',
            ]
        ],

        // 修改下面
        'providers' => [
            'admin' => [
                'driver' => 'custom',
            ]
        ],
    ],

这样就完成了自定义登录认证的逻辑,自定义登陆算是laravel中比较复杂的部分,需要开发者有耐心的一步步调试完成。

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
贡献者:1
讨论数量: 0
发起讨论 查看所有版本


暂无话题~