Laravel:如何在没有数据库的情况下对用户进行身份验证 [英] Laravel: How to authenticate users without DB

查看:161
本文介绍了Laravel:如何在没有数据库的情况下对用户进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,是否可以在没有DB的情况下进行身份验证(而是使用用户提供程序)?

As the title suggests, is it possible to authenticate without DB (instead, using User Provider)?

我看过有关如何在没有密码的情况下进行身份验证的帖子,但是如果没有 DB 可以进行身份​​验证吗?

I've seen posts about how to authenticate without password, but would it be ever possible to authenticate without DB?

这就是我一直在努力实现的目标...

Here is what I've been trying to achieve...

  1. 要求用户提交PIN码
  2. 将PIN与.env内的值进行比较
  3. 如果PIN码正确,则对用户进行身份验证

通过引入用户提供商.

但是,该文档似乎并未描述用户提供者的外观.

However, the doc doesn't seem to describe how the user provider should look like.

以下是我的代码段(嗯,我只是模仿了文档)...

Here are snippets of my code (well, I simply mimicked the doc)...

class AuthServiceProvider extends ServiceProvider {

    public function boot()
    {
        $this->registerPolicies();

        Auth::provider('myUser', function ($app, array $config) {
            // Return an instance of Illuminate\Contracts\Auth\UserProvider...

            return new MyUserProvider($app->make('myUser'));
        });
    }
}

auth.php ...

'providers' => [
    'users' => [
        'driver' => 'myUser',
    ],
],

现在,我不知道如何进行.

Now, I have no clue as to how to proceed.

所以,我想知道...

So, I'd like to know...

  1. user provider的外观

是否有可能首先使用env()对用户进行身份验证

if it's possible to authenticate users with env() in the first place

任何建议将不胜感激.

推荐答案

谢谢大家,但是有一个更简单的解决方案...使用session().

Thank you all, but there was a much simpler solution... which is to use session().

在控制器中,

public function login(Request $request)
{
    if ($request->input('pin') === env('PIN')) {
        $request->session()->put('authenticated', time());
        return redirect()->intended('success');
    }

    return view('auth.login', [
        'message' => 'Provided PIN is invalid. ',
    ]);
    //Or, you can throw an exception here.
}

路线看起来像

Route::group(['middleware' => ['web', 'custom_auth']], function () {
    Route::get('/success', 'Controller@success')->name('success');
});

custom_auth看起来像

public function handle($request, Closure $next)
{
    if (!empty(session('authenticated'))) {
        $request->session()->put('authenticated', time());
        return $next($request);
    }

    return redirect('/login');
}

希望这会对某人有所帮助.

Hope this will help someone.

这篇关于Laravel:如何在没有数据库的情况下对用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆