【问题标题】:Can't get user id in controller and Auth::check() is not working - Laravel 5.8无法在控制器中获取用户 ID 并且 Auth::check() 不起作用 - Laravel 5.8
【发布时间】:2019-08-30 16:37:57
【问题描述】:

我是 laravel 5.8 的新手,无法在 api 控制器中使用 Auth,以下是详细信息:

  1. 我已将默认用户表从 users 更改为 User

User.php

<?php

    namespace App;

    use Illuminate\Notifications\Notifiable;
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Foundation\Auth\User as Authenticatable;

    class User extends Authenticatable
    {
        use Notifiable;
        protected $table = 'User';

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'firstName', 'lastName', 'email', 'password',
        ];

        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];

        /**
         * The attributes that should be cast to native types.
         *
         * @var array
         */
        protected $casts = [
            'email_verified_at' => 'datetime',
        ];
    }
  1. 没有改变 auth.php

    中的任何内容
    <?php 
      return 
    
    ['defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    
    
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],
    
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    
        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],
    
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ], 
    ];
    

我的问题:如何使 Auth::check() 在此控制器中工作并获取用户 ID。这将返回Not logged

<?php

namespace App\Http\Controllers\api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Session;
use DB;
use Response;

use Auth;

class AccountsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
         //
        $userID = Auth::id();

        if (Auth::check()) {
            return "User logged , user_id : ".$userID ;
        }else{
            return "Not logged"; //It is returning this
        }
    }

}

我已经尝试了几个类似问题的答案(Q1Q2),但没有奏效。

【问题讨论】:

  • Auth 检查当前会话中的用户。你在使用 API 的函数吗?

标签: php laravel laravel-5 laravel-5.8


【解决方案1】:

如果使用 API 路由,将没有登录用户。 API 的身份验证是通过令牌完成的,您可以在 Laravel docs 中了解它。

除非您使用网络路由,否则不会调用会话中间件,因此不会启动会话,因此无法使用身份验证。

您试图实现什么,因为将 Auth 与 API 路由一起使用是不正常的。

【讨论】:

    【解决方案2】:

    从您的控制器看来,您正在尝试使用 API 路由访问会话。 API 路由无权访问会话。您应该从web.php 中定义的路由调用您的控制器,或者您应该使用令牌进行身份验证。请咨询Laravel docs 以探索 API 身份验证的可能性。

    【讨论】:

      【解决方案3】:

      使用可以尝试在会话中设置身份验证用户。这将在身份验证后设置用户并设置 Auth::user()。

      Auth::attempt(['email' =&gt; request('email'), 'password' =&gt; request('password')])

      【讨论】:

        猜你喜欢
        • 2015-10-13
        • 1970-01-01
        • 2022-12-05
        • 2016-04-07
        • 1970-01-01
        • 1970-01-01
        • 2017-08-01
        • 1970-01-01
        • 2016-05-31
        相关资源
        最近更新 更多