Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JWTAuth::getJWTProvider()->setSecret( ... dosent work #2258

Open
pcualmac opened this issue May 3, 2024 · 0 comments · May be fixed by #2262
Open

JWTAuth::getJWTProvider()->setSecret( ... dosent work #2258

pcualmac opened this issue May 3, 2024 · 0 comments · May be fixed by #2262

Comments

@pcualmac
Copy link

pcualmac commented May 3, 2024

JWTAuth::getJWTProvider()->setSecret(... dosent work.

Steps to reproduce

  1. Crate a larval project add "tymon/jwt-auth": "^2.1"
  2. generate the secret php artisan jwt:secret
  3. save as secret app1 ex 'ZNF9rMdaAq8AI3koJ1AfNfokm9o5kZ3dnts4qirwIl0QErgDkf05umHUVa88rqeo'
  4. test an api login
  5. php artisan jwt:secret
  6. test api login
  7. create a controller for login app1
  8. add _construct()
api for keys 
  public function __construct()
      {
          // Get the secret key for the application context from configuration
          $this->secretKey = 'ZNF9rMdaAq8AI3koJ1AfNfokm9o5kZ3dnts4qirwIl0QErgDkf05umHUVa88rqeo'
          // Set the secret key for JWT authentication
          JWTAuth::getJWTProvider()->setSecret($this->secretKey);
      }

    public function loginUser(Request $request)
    {

        $credentials = $request->only('email', 'password');
        try {
            $token = auth()->guard('appOne')->attempt($credentials, ['secret' => $this->secretKey]);
            if (!$token) {
                return response()->json(['success' => false, 'error' => 'Some Error Message'], 401);
            }
        } catch (JWTException $e) {
            return response()->json(['success' => false, 'error' => 'Failed to login, please try again.'], 500);
        }
        $user = Auth::guard('appOne')->user();
        $customClaims = $user->getJWTCustomClaims();
        $response =[
            'token' => $token,
            'customClaims' => $customClaims,
            'claims' => JWTAuth::claims($customClaims)->fromUser($user),
            'secretKey' => $this->secretKey,
            'getVerificationKey' =>JWTAuth::getJWTProvider()->getVerificationKey()
        ]; 
        return $this->finalResponse($response);
    }

ApiOne
public function verifyToken(Request $request)
    {
        try {
            $token = $request->bearerToken() ?: $request->query('token');
            JWTAuth::setToken($token);
            $user = Auth::guard('api')->user();
            $customClaims = $user->getJWTCustomClaims();
            $response =[
                'user' => $user,
                'customClaims' => $customClaims,
                'claims' => JWTAuth::claims($customClaims)->fromUser($user),
            ]; 
            return response()->json(['response' => $response], 200);
        } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
            // Token has expired
            return response()->json(['error' => 'Token expired'], 401);
        } catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
            // Token is invalid
            return response()->json(['error' => 'Token invalid'], 401);
        } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
            // Token is absent from the request
            return response()->json(['error' => 'Token absent'], 401);
        }
    }
  1. in the new project try app1 for verify user
  2. not working
  3. try secret from the first app works

Expected behaviour

I was Expected to use the app1 ex 'ZNF9rMdaAq8AI3koJ1AfNfokm9o5kZ3dnts4qirwIl0QErgDkf05umHUVa88rqeo' as JWT_SECRET i

Actual behaviour

not working with 'ZNF9rMdaAq8AI3koJ1AfNfokm9o5kZ3dnts4qirwIl0QErgDkf05umHUVa88rqeo' as JWT_SECRET working with main app secret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant