Lumen Auth:attempt()不会持久登录

时间:2018-05-28 09:37:11

标签: laravel lumen

我已经安装了流明v5.6的新副本并编写了用户登录的新微服务。但是当我输入登录详细信息并使用

Auth::attempt($credential)

登录方法然后我随时都会记下以下错误

Method Illuminate\\Auth\\RequestGuard::attempt does not exist

以下是我的工作方法

public static function loginUser($credentials) {
   if (Auth::attempt($credentials)) {
       echo "<pre>";
       print_r('ok');
       exit;   
   }
   return [];      
}

以下是我的用户模型

<?php

namespace App;

use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class User extends BaseModel implements AuthenticatableContract, AuthorizableContract {

    use Authenticatable,
        Authorizable;

    /**
     * modal table
     * @var type
     */
    protected $table = 'users';

    /**
     * modal primary key
     * @var type
     */
    protected $primarykey = 'id';

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

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];


    public static function saveNewUser($inputs) {
        $user = new User($inputs);
        if ($user->save()) {
            return $user;
        }
        return [];
    }


    /**
     * login user
     * @param type $credentials
     * @return type
     */
    public static function loginUser($credentials) {
        if (Auth::attempt($credentials)) {
            echo "<pre>";
            print_r('ok');
            exit;   
        }
        return [];      
    }

}

我不知道这个问题是什么。我也实现了auth配置方法,但未能解决此问题。现在我被困在用户登录过程中。指导正确的方向,并提供任何有用的链接来解决这个问题。

1 个答案:

答案 0 :(得分:0)

将此中间件添加到您的路线中:

Route::get('/users/{user}', 'UserController@show')->middleware('auth:api');