tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.3k stars 1.54k forks source link

Lumen 7.0 token always return false #2042

Open Hothi-Jimit opened 4 years ago

Hothi-Jimit commented 4 years ago

Subject of the issue

Lumen ^7.0 token always return false

Your environment

Q A
Bug? no
New Feature? no
Framework Lumen
Framework version 7.x
Package version ^1.0
PHP version 7.x.y

In my project "laravel/lumen-framework": "^7.0" and "tymon/jwt-auth": "^1.0". When I creating token that time always returns false.

app/User.php

`<?php

namespace App;

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; use Illuminate\Support\Facades\Hash; use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Model implements JWTSubject, AuthenticatableContract, AuthorizableContract { use Authenticatable, Authorizable; public $table= "USER_MASTER"; protected $primaryKey = 'USER_ID'; public $incrementing = false; const CREATED_AT = 'CREATED_DATE'; const UPDATED_AT = 'UPDATED_DATE';

protected $fillable = [
    'USER_NAME', 'PASSWORD',
];

protected $hidden = [
    'PASSWORD',
];

public function getAuthIdentifierName()
{
    return 'USER_NAME';
}

public function getAuthIdentifier()
{
    return $this->getId();
}

public function getId()
{
    return $this->USER_NAME;
}
public function getPassword()
{
    return $this->PASSWORD;
}

public function getAuthPassword()
{
    return $this->getPassword();
}

public function getJWTIdentifier()
{
    return $this->getId();
}

public function getJWTCustomClaims()
{
    return [];
}

public function setPasswordAttribute($value) {
    $this->attributes['PASSWORD'] = Hash::make($value);
}

}`

app/Http/Controllers/AuthController.php

`<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request; use App\User; use Tymon\JWTAuth\Contracts\Providers\Auth; use Tymon\JWTAuth\Facades\JWTAuth;

class AuthController extends Controller { public function __construct() { //$this->middleware('auth:api', ['except' => ['login']]); }

public function login(Request $request) {
    $user = new User();
    $user->USER_ID ='c2aea979-dfdd-4d2d-8457-ff722b2654fe';
    $user->USER_NAME = 'test@gmail.com';
    $user->PASSWORD = 'Test@123';
    $user->DOMAIN_ID ='c2aea979-dfdd-4d2d-8457-ff722b2654fe';
    $user->save();

    if (! $token = JWTAuth::attempt(['USER_NAME'=>$request->username,'PASSWORD'=>$request->password])) {
        return "error";
    }
    return $this->respondWithToken($token);
}

private function respondWithToken($token) {
    return $this->respond([
        'token' => $token,
        'access_type' => 'bearer',
        'expires_in' => auth()->factory()->getTTL() * 60
    ], "Login Successful");
}

public function logout() {
    auth()->logout();
    return $this->respondWithMessage('User successfully logged out');
}

public function refresh() {
    return $this->respondWithToken(auth()->refresh());
}

public function me() {
    return $this->respond(auth()->user());
}

} `

User insert working perfect but when the same detail hit with postman that return always an error.

Please help me.

Thank you.

stale[bot] commented 3 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.