【问题标题】:Laravel 6.2 Auth-scaffold - not working but doesn't throw / log errorLaravel 6.2 Auth-scaffold - 不工作但不抛出/记录错误
【发布时间】:2020-03-24 13:31:46
【问题描述】:

问题

我已将 Auth 脚手架添加到现有项目中。

    php artisan ui vue --auth

据我了解,身份验证应该开箱即用,无需任何先决条件。
视图/网址工作正常,但注册过程和登录过程都不起作用。
如果未通过身份验证,my-domain.com/home 会将我重定向到 my-domain.com/login

其他问题的通知、警告和异常通常会按预期抛出。
但我没有收到有关此特定问题(注册/登录)的错误。
所以我在这里有点迷路,很高兴得到任何帮助。

TIA

用例

我将填写表格并被重定向到同一页面。
(例如 my-domain.com/register 会将我重定向到 my-domain.com/register)。
此外,不会将新用户插入到用户表中,并且在登录过程中也会读取失败。

到目前为止我尝试了什么

权限似乎不是问题,因为脚本可以像这样创建用户:

    $user = new \App\User;

    $user->name = 'Simon the Sorcrerer';
    $user->email = 'sorcerersimon@gme.co';
    $user->password = Hash::make('expelliarmus');
    $user->role_id = 'user';

    $user->save();

在我看来,这些链接在方法方面已经过时了。 但是 - 值得一提的是,我没有更改控制器或与默认值相关的任何其他内容。

2017 - Auth should work out of the box .
2013 - Laravel - Auth not working. super weird

我试过了

     Auth::routes();
     Route::get('/home', 'HomeController@index')->name('home');

     Auth::routes(['register' => true]);
     Route::get('/home', 'HomeController@index')->name('home');

     Auth::routes(['register' => true, 'verify' =>false]);
     Route::get('/home', 'HomeController@index')->name('home');

两者都没有改变当前行为。

【问题讨论】:

    标签: vue.js authentication routes laravel-6.2


    【解决方案1】:

    TL;DR:负载均衡器/CDN 后面的混合内容和查询中缺少的 role_id

    如果您在负载平衡的环境中设置 Laravel,https 会话在访问 Web 服务器之前终止,因此 Laravel 正在为 http 请求提供服务,从而产生混合内容问题。 因此,实际的脚本不会被导入,应用程序本身也不会抛出任何错误。

    因此,您需要确保通过安全连接 (https) 提供导入的脚本和端点。

    (通过在浏览器控制台中检查应用程序,您将看到混合内容错误。)

    解决不安全端点的路由问题

    编辑您的路由:

    app/Providers/AppServiceProvider.php
    

    来自

    public function boot()
    {
        ...
    } 
    

    public function boot()
    {
       // if (!\App::environment('local')) {
       \URL::forceScheme('https');
       // } 
    } 
    

    如果您想区分服务器上的本地环境和生产环境,注释部分反映了Mladen Janjetovic's approach。 但是,请考虑尽可能模拟您的生产环境进行本地测试。

    可选:解决脚本导入的混合内容(CSS 和 JS)

    您希望确保从安全来源 (https) 调用脚本。
    编辑您的资产调用:

    resources/views/layouts/app.blade.php
    

    来自

    asset( ... )
    

    secure_asset( ... )
    

    解决创建用户时缺少默认值的问题

    解决混合内容后,您可能会看到 User::create 查询失败。如果发生这种情况,您可能需要进行以下更改。

    在以下位置编辑您的用户模型:

    app/User.php
    

    来自

    protected $fillable = [
        'name', 'email', 'password',
    ];
    

    protected $fillable = [
        'name', 'email', 'password', 'role_id'
    ];
    

    编辑您的查询:

    app/Http/Controllers/Auth/RegisterController.php
    

    来自

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
    

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'role_id' => $data['role_id'] ?? 'user' 
        ]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-05
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 2017-03-10
      相关资源
      最近更新 更多