Commit 7f4e7e75 by Nakarin Luankla

decodeJWT

parent 07f2bb09
...@@ -106,7 +106,6 @@ ...@@ -106,7 +106,6 @@
"postcss": "^8.4.24", "postcss": "^8.4.24",
"tailwind-clip-path": "^1.0.0", "tailwind-clip-path": "^1.0.0",
"tailwindcss": "^3.3.2", "tailwindcss": "^3.3.2",
"typescript": "~5.1.3", "typescript": "~5.1.3"
"jwt-decode": "^4.0.0"
} }
} }
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { jwtDecode } from "jwt-decode";
const TOKEN_KEY = 'accessToken'; const TOKEN_KEY = 'accessToken';
@Injectable({ @Injectable({
...@@ -10,12 +9,22 @@ export class TokenService { ...@@ -10,12 +9,22 @@ export class TokenService {
constructor() { constructor() {
} }
public getUser(): any { public getUser(): any {
const user = jwtDecode(window.sessionStorage.getItem(TOKEN_KEY)!) const user = this.decodeJWT(window.sessionStorage.getItem(TOKEN_KEY)!)
if (user) { if (user) {
return user; return user;
} }
return ; 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