DEV Community

Maulana
Maulana

Posted on

1

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)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay