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

我看到过大约how to authenticate without password篇帖子,但如果没有DB篇,还能进行身份验证吗?

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

  1. 请用户提交PIN
  2. Compare the PIN with a value within .env
  3. If the PIN is correct, authenticate the user

It seems to be possible to do authenticate without a traditional RDBMS by introducing a user provider.

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人应该是什么样子

  2. 如果有可能首先用env()来验证用户身份

Any advice will be appreciated.

推荐答案

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.
}

The route looks like,

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

The custom_auth will looke like,

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

    return redirect('/login');
}

希望这能帮助一些人.

Laravel相关问答推荐

在Vite list 中找不到文件:Resources/scss/hrms.scss.如何使用LARAVEL VITE解决这个问题?

如何使用 laravel 更新单个值

调用字符串上的成员函数

如何通过单击进入 vs 代码中的类

如何在 Laravel Eloquent Collection 数组之前添加一个键值对?

Laravel 5 将错误发送到Electron邮件

laravel 基本路径

Laravel Session 总是改变 Laravel 5.4 中的每个刷新/请求

Laravel - 超过 PHP 最大上传大小限制时验证文件大小

在 Laravel 中下载后如何重定向?

Laravel - 自定义时间戳列名称

在 laravel 分页中添加一些数据

laravel:Eloquent的例子将数据插入数据库

日期验证 - 如何本地化/翻译字符串今天和明天

Laravel 错误声明 App\Exceptions\Handler::report(Throwable $exception)

Laravel 中的单会话登录

如何在 Laravel 4 中组织不同版本的 REST API 控制器?

Laravel 不活动时间设置

Laravel 更新后用户模型错误(用户类包含 3 个抽象方法)

如何在 Laravel 5.3 中获取当前的语言环境