PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 6, 2022

[FIXED] Laravel Passport doesn't generate tokens

 January 06, 2022     laravel, laravel-5, laravel-passport, oauth-2.0, php     No comments   

Issue

I'm using Laravel as back-end for my ionic application, and Laravel Passport for authentication. When I try to call createToken() of Auth it shows, Call to undefined method

Login.ts:

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;
    }

UserController.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']);
    }
}

Error Log: When I tested using Postman, it throws following error

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->() D:\PsResources\DroidWorkspace\ZiPay\ZipServer\app\Http\Controllers\UserController.php:16

auth.php

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
        'table' => 'users',
    ],
    'users' => [
        'driver' => 'database',
        'table' => 'users',
     ],
],

user.php

namespace App;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable{
    use HasApiTokens, Notifiable;
    ....
}

Solution

This is weird,I deleted whole project and started fresh one, everything works fine.



Answered By - Suroor Ahmmad
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing