Laravel Passport不生成令牌

时间:2018-10-20 06:07:33

标签: php laravel laravel-5 oauth-2.0 laravel-passport

我将Laravel用作离子应用程序的后端,并使用Laravel Passport进行身份验证。当我尝试呼叫#include <stdio.h> int main() { char x = -4; printf("0x%x -> %i\n", x, x); x <<= 1; //multiply by 2^1 = 2 printf("0x%x -> %i\n", x, x); x >>= 2; //divide by 2^2 =4 printf("0x%x -> %i\n", x, x); return 0; } 中的0xfffffffc -> -4 0xfffffff8 -> -8 0xfffffffe -> -2 时,显示为createToken()

Login.ts

Auth

UserController.php

Call to undefined method

错误日志: 当我使用Postman进行测试时,会引发以下错误

login(event){ let dataJSON; let formData = new FormData(); formData.append('email',event.target[0].value) formData.append('password',event.target[1].value) this.http.post(ENV.BASE_URL+'/auth', formData) .subscribe((data) => { console.log(data); dataJSON = data; }, (error) => { console.log("Error!", error); }); return false; }

auth.php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\User;

class UserController extends Controller{
    public function auth(Request $request){
        $params = $request->only('email', 'password');
        $username = $params['email'];
        $password = $params['password'];
        if(\Auth::attempt(['email' => $username, 'password' => $password])){
            $token = \Auth::user()->createToken('Token'); //Throws Error here
            return response()->json(['token'=>$token]);
        }
        return response()->json(['error' => 'Invalid username or Password']);
    }
}

user.php

Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Illuminate\Auth\GenericUser::createToken() in file
D:\PsResources\DroidWorkspace\ZiPay\ZipServer\app\Http\Controllers\UserController.php on line 16
Stack trace:
  1. Symfony\Component\Debug\Exception\FatalThrowableError-&gt;() D:\PsResources\DroidWorkspace\ZiPay\ZipServer\app\Http\Controllers\UserController.php:16

3 个答案:

答案 0 :(得分:1)

将这些添加到您的用户模型


use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasApiTokens

答案 1 :(得分:0)

尝试一下

\Auth::user()->createToken('Token')->accessToken;

更新

检查以下内容:

  1. 您是否在app.php provider数组中添加了此“ Laravel \ Passport \ PassportServiceProvider :: class”?
  2. 您是否在guards数组的auth.php中将api-> driver更改为“ passport”?

Config/App.php

Config/Auth.php

答案 2 :(得分:0)

这很奇怪,我删除了整个项目并重新开始了一个项目,一切正常。