首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Lumen Auth:attempt()不会持久化登录

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

Stack Overflow用户
提问于 2018-05-28 17:37:11
回答 1查看 768关注 0票数 2

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

代码语言:javascript
复制
Auth::attempt($credential)

方法进行登录,然后一直收到以下错误

代码语言:javascript
复制
Method Illuminate\\Auth\\RequestGuard::attempt does not exist

下面是我的工作方法

代码语言:javascript
复制
public static function loginUser($credentials) {
   if (Auth::attempt($credentials)) {
       echo "<pre>";
       print_r('ok');
       exit;   
   }
   return [];      
}

下面是我的用户模型

代码语言:javascript
复制
<?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配置方法,但是没能解决这个问题。现在我被困在用户登录过程中。指导正确的方向,并提供任何有用的链接来解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2018-05-28 18:35:44

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

代码语言:javascript
复制
Route::get('/users/{user}', 'UserController@show')->middleware('auth:api');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50563305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档