Commit 7f4e7e75 by Nakarin Luankla

decodeJWT

parent 07f2bb09
......@@ -106,7 +106,6 @@
"postcss": "^8.4.24",
"tailwind-clip-path": "^1.0.0",
"tailwindcss": "^3.3.2",
"typescript": "~5.1.3",
"jwt-decode": "^4.0.0"
"typescript": "~5.1.3"
}
}
import { Injectable } from '@angular/core';
import { jwtDecode } from "jwt-decode";
const TOKEN_KEY = 'accessToken';
@Injectable({
......@@ -9,13 +8,23 @@ export class TokenService {
constructor() {
}
public getUser(): any {
const user = jwtDecode(window.sessionStorage.getItem(TOKEN_KEY)!)
const user = this.decodeJWT(window.sessionStorage.getItem(TOKEN_KEY)!)
if (user) {
return user;
}
return ;
}
decodeJWT(token: string) {
let base64Url = token.split('.')[1]; // ดึงส่วนที่เป็น Payload
let base64 = base64Url.replace('-', '+').replace('_', '/'); // แก้ไข base64 ให้ถูกต้อง
let jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment