本文介绍了Laravel 5.2需要一个实现默认的认证驱动程序/&QUOT一个例子;多验证&QUOT ;.它现在是需要大量的作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

认证驱动程序/多验证

以释放laravel 5.2之前中指出,多AUTH suppots开箱。但目前还没有任何例子codeS展示如何使用不同的驱动程序与途径进行身份验证。所以,我需要使用默认laravel 5.2帮助建立多AUTH


解决方案

 设置了两个新模式
应用程序\\管理员
应用\\用户更新配置/ auth.php
    返回[
    '缺省'=> [
        '后卫'=> '用户',
        '密码'=> '用户',
    ]
    '卫士'=> [
        '用户'=> [
            '司机'=> 会话,
            '商'=> '用户',
        ]
        '管理员'= GT; [
            '司机'=> 会话,
            '商'=> '管理员',
        ]
    ]
    '供应商'=> [
        '用户'=> [
            '司机'=> '雄辩',
            模式= GT; 应用程序\\用户,
        ]
        '管理员'= GT; [
            '司机'=> '雄辩',
            模式= GT; 应用程序\\管理,
        ]
    ]
    '密码'=> [
        '用户'=> [
            '商'=> '用户',
            电子邮件=> auth.emails.password',
            '表'=> password_resets',
            '过期'=> 60,
        ]
        '管理员'= GT; [
            '商'=> '管理员',
            电子邮件=> auth.emails.password',
            '表'=> password_resets',
            '过期'=> 60,
        ]
    ]
];在kernel.php
    保护$中间件= [
        \\照亮\\基金会\\ HTTP \\中间件\\ CheckForMaintenanceMode ::类,
        \\照亮\\会议\\中间件\\ StartSession ::类,
        \\照亮\\查看\\中间件\\ ShareErrorsFromSession ::类
    ];    / **
     *应用程序的路线中间件组。
     *
     * @var阵列
     * /
    保护$ middlewareGroups = [
        '网络'=> [
            \\ APP \\ HTTP \\中间件\\ EncryptCookies ::类,
            \\照亮\\饼干\\中间件\\ AddQueuedCookiesToResponse ::类,            // \\ APP \\ HTTP \\中间件\\ VerifyCsrfToken ::类,
        ]        API=> [
            '节流:60,1',
        ]
    ];
在Route.php设置为低于code和测试    路线::得到('/登录',函数(){
        $ AUTH = AUTH() - GT;后卫('管理员');        $凭证= [
            电子邮件=> [email protected]',
            '密码'=> '密码',
        ];        如果($ auth->尝试($凭证)){
            返回重定向('/ profile文件');
        }
    });    路线::得到('/ profile文件',函数(){
            如果(AUTH() - GT;后卫(管理员) - GT;检查()){
                 的print_r(AUTH() - GT;后卫(管理员) - GT;用户() - GT;的toArray());
            }            如果(AUTH() - GT;后卫(用户) - GT;检查()){
                的print_r(AUTH() - GT;后卫(用户) - GT;用户() - GT;的toArray());
            }
        });请享用 :)

Authentication Drivers / "Multi-Auth"

as prior to release of laravel 5.2 it is stated that multi auth suppots out of the box. but there is no any example codes showing how to authenticate using different drivers with routes. So I need help setting up the multi-auth using default laravel 5.2

解决方案
Set two new model
App\Admin
App\User

Update config/auth.php


    return [
    'defaults' => [
        'guard' => 'user',
        'passwords' => 'user',
    ],
    'guards' => [
        'user' => [
            'driver' => 'session',
            'provider' => 'user',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admin',
        ],
    ],
    'providers' => [
        'user' => [
            'driver' => 'eloquent',
            'model' => 'App\User',
        ],
        'admin' => [
            'driver' => 'eloquent',
            'model' => 'App\Admin',
        ],
    ],
    'passwords' => [
        'user' => [
            'provider' => 'user',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ]
    ]
];



In kernel.php


    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

            //\App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];




and in Route.php set below code and test

    Route::get('/login', function() {
        $auth = auth()->guard('admin');

        $credentials = [
            'email' =>  '[email protected]',
            'password' =>  'password',
        ];

        if ($auth->attempt($credentials)) {
            return redirect('/profile');
        }
    });



    Route::get('/profile', function() {
            if(auth()->guard('admin')->check()){
                 print_r(auth()->guard('admin')->user()->toArray());
            }

            if(auth()->guard('user')->check()){
                print_r(auth()->guard('user')->user()->toArray());
            }
        });



Enjoy :)

这篇关于Laravel 5.2需要一个实现默认的认证驱动程序/&QUOT一个例子;多验证&QUOT ;.它现在是需要大量的作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:53