Commit 8ec34053 by Nattana Chaiyamat

Merge branch 'DEV' of https://mygit.myhr.co.th/angular/myAppraisal into DEV

parents 44e63790 7f4e7e75
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