DEV Community

Maulana
Maulana

Posted on

Encrypted JWT and how to activate Auth::user()

I just had an experience that I found interesting

So the boss asked me to encrypt the JWT token sent to the frontend. However, the problem is that when the token is encrypted, the controller cannot use Auth()->user()

What I did was create middleware to decrypt the token sent from the frontend then set to user so that Auth()->user() can be used in all controllers in the middleware

$token = str_replace('Bearer ', '', $request->header('Authorization'));
$token = Crypt::decrypt($token);
JWTAuth::setToken($token);
JWTAuth::toUser();
Enter fullscreen mode Exit fullscreen mode

Maybe there are more suggestions for friends who are more experienced. Thanks 🌟

Top comments (0)