admin管理员组文章数量:1425780
I am trying to generate a JWT token which I will do in my Laravel project like this solution I found: .php
So I have 2 questions I am trying to get wrap my head around:
In my Next.js React project, i would set the JWT in a cookie to remember it. Is that right? And then can pass it with every request to the server to verify the user.
How could I decode it then in Next.js? So that I could get some basic info out of it like a username.
I am trying to generate a JWT token which I will do in my Laravel project like this solution I found: https://github./luciferous/jwt/blob/master/JWT.php
So I have 2 questions I am trying to get wrap my head around:
In my Next.js React project, i would set the JWT in a cookie to remember it. Is that right? And then can pass it with every request to the server to verify the user.
How could I decode it then in Next.js? So that I could get some basic info out of it like a username.
Share Improve this question asked Dec 19, 2017 at 8:45 strangeQuirksstrangeQuirks 6,01010 gold badges48 silver badges86 bronze badges 4- You encode your JWT using a key, so you would need that key to be able to decode your JWT (This would be done on the server side, as I don't think it would be safe to pass it along to the client). – ZombieTfk Commented Dec 19, 2017 at 8:51
- ah okay, so i should never decode it in my React Application? Then I would just have to always pass the information I need from the laravel api? Would be nice to be able to access some data in the react application by decoding the jwt – strangeQuirks Commented Dec 19, 2017 at 8:54
- It's certainly possible to decode on the client, but it would depend on how much you're willing to trust them. – ZombieTfk Commented Dec 19, 2017 at 8:56
- Try stackoverflow./questions/38552003/… – Nigel Ren Commented Dec 19, 2017 at 8:56
2 Answers
Reset to default 3For JWT Decoding in Javascript you could use Auth0's JWT decode library(https://github./auth0/jwt-decode) which makes it simple to decode(no verification) encoded JWT tokens.
You would just read the JWT token from the set cookie and decode it like this:
var token = 'eyJ0eXAiO.../// jwt token';
var decoded = jwt_decode(token);
You can also use a simple function to decode it which would look like this:
function parseJwt (token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse(window.atob(base64));
};
There is no problem with decoding JWT tokens directly in the React application, just make sure that you always verify the encoded token on the server side so it can't be a modified JWT token by the user.
You can also try this.
Use poser to manage your dependencies and download PHP-JWT:
poser require firebase/php-jwt.
For encoding and Decoding JWT payload in laravel.
//import this file.
use Firebase\JWT\JWT;
//create a payload.
$payload =
[
"email" => $user->email,
"username" => $user->username
];
//for encoding payload
$token = JWT::encode($payload, 'secret', 'HS256');
//for decoding $token
$data = JWT::decode($token, new Key('secret', 'HS256'));
本文标签: phpJWT that can be encoded in Laravel and decoded in JavaScriptStack Overflow
版权声明:本文标题:php - JWT that can be encoded in Laravel and decoded in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745350812a2654754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论