Commit 61ef530f by sawit

access token

parent e9de2e4f
......@@ -280,7 +280,7 @@ export class HomeComponent implements OnInit {
isVisible: true,
category: 'applications',
permissions: { view: true, create: true, edit: true, delete: true, export: true, import: true },
appUrl: environment.url
appUrl: environment.baseUrl
},
{
id: 'MYSKILL-X',
......@@ -378,11 +378,20 @@ export class HomeComponent implements OnInit {
confirmNavigation() {
if (this.selectedApp) {
window.sessionStorage.setItem('environment', this.selectedEnvironment);
this.tokenService.saveAppToken(this.selectedApp.name);
this.router.navigate([this.selectedApp.path]);
window.sessionStorage.setItem("module", this.selectedApp.name);
const appToNavigate = this.selectedApp;
this.tokenService.saveAppToken(appToNavigate.name).subscribe({
next: () => {
this.router.navigate([appToNavigate.path]);
this.cancelNavigation(); // Close modal on success
},
error: (err) => {
console.error('Failed to save token and navigate', err);
this.cancelNavigation(); // Close modal on error
}
});
} else {
this.cancelNavigation(); // Fallback to close modal
}
this.cancelNavigation(); // Close modal and reset state
}
cancelNavigation() {
......
......@@ -256,8 +256,16 @@ export class HeaderComponent {
}
checkAppToken(appmodule:string){
this.tokenService.saveAppToken(appmodule)
this.router.navigate(['/portal-manage/'+appmodule])
this.tokenService.saveAppToken(appmodule).subscribe({
next: () => {
this.router.navigate(['/portal-manage/'+appmodule]);
},
error: (err) => {
console.error(`Failed to save token for ${appmodule} and navigate`, err);
// Fallback navigation even on error
this.router.navigate(['/portal-manage/'+appmodule]);
}
});
}
routerApp(appmodule:string){
......
......@@ -4,6 +4,7 @@ import { HttpClient } from '@angular/common/http';
import { LoginModel, ProfileModel, UserModel } from '../user-auth.model';
import { environment } from '../../../environments/environment';
import { Observable, tap } from 'rxjs';
import { TokenService } from './token.service';
@Injectable({
providedIn: 'root',
})
......@@ -13,6 +14,7 @@ export class AuthService {
public generatedTokenData: any;
constructor(
private http: HttpClient,
private tokenService: TokenService,
) {
}
......@@ -46,12 +48,13 @@ export class AuthService {
return this.http.get<any>(environment.url + "/auth/generate-token",).pipe(tap(
response => {
this.generatedTokenData = response;
this.tokenService.saveModulesData(response);
}
))
}
postPortalRefreshToken(appUrl: string){
return this.http.post<any>(appUrl + "/refresh-token", {
return this.http.post<any>(appUrl + "/auth/portal/refresh-token", {
"refreshToken": this.generatedTokenData.refreshToken
});
}
......
import { Injectable } from "@angular/core";
import { Router } from "@angular/router";
import { HttpClient } from "@angular/common/http";
import { Observable, of } from "rxjs";
import { tap } from "rxjs/operators";
import { jwtDecode } from "jwt-decode";
import { ProfileModel, UserModel } from "../user-auth.model";
......@@ -29,7 +32,7 @@ export class TokenService {
"ZEESERVER": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBdXRoIiwidWlkIjoiOGFhNDUzMzAtMzE0Yy0xMWU3LWJhZjMtMmQ3ZDA5ODc3NzQ0Iiwicm9sZXMiOlsiYWRtaW4iXSwiaXNzIjoiQ29tcHV0ZXIgU2NpZW5jZSBDb3Jwb3JhdGlvbiBMaW1pdGVkIiwidXNlcm5hbWUiOiJhZG1pbiJ9.TzU5PKZ4jf8YeCEXA03IxLVRVmCh3AC2oTgT_KvBt6g",
"MYFACE": "",
"MYLEARN": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBdXRoIiwidWlkIjoiOGFhNDUzMzAtMzE0Yy0xMWU3LWJhZjMtMmQ3ZDA5ODc3NzQ0Iiwicm9sZXMiOlsiYWRtaW4iXSwiaXNzIjoiQ29tcHV0ZXIgU2NpZW5jZSBDb3Jwb3JhdGlvbiBMaW1pdGVkIiwidXNlcm5hbWUiOiJhZG1pbiJ9.Um39_CTM01n1g4XpFyuAa3VPxOozNPnJ0mu7kxU6KUs",
"MYJOB": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBdXRoIiwidWlkIjoiOGFhNDUzMzAtMzE0Yy0xMWU3LWJhZjMtMmQ3ZDA5ODc3NzQ0Iiwicm9sZXMiOlsiYWRtaW4iXSwiaXNzIjoiQ29tcHV0ZXIgU2NpZW5jZSBDb3Jwb3JhdGlvbiBMaW1pdGVkIiwidXNlcm5hbWUiOiJhZG1pbiJ9.Um39_CTM01n1g4XpFyuAa3VPxOozNPnJ0mu7kxU6KUs",
"MYJOB": "",
"MYSKILL-X": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBdXRoIiwidWlkIjoiOGFhNDUzMzAtMzE0Yy0xMWU3LWJhZjMtMmQ3ZDA5ODc3NzQ0Iiwicm9sZXMiOlsiYWRtaW4iXSwiaXNzIjoiQ29tcHV0ZXIgU2NpZW5jZSBDb3Jwb3JhdGlvbiBMaW1pdGVkIiwidXNlcm5hbWUiOiJhZG1pbiJ9.Um39_CTM01n1g4XpFyuAa3VPxOozNPnJ0mu7kxU6KUs",
}
......@@ -113,7 +116,7 @@ export class TokenService {
},
}
constructor(private router: Router) { }
constructor(private router: Router, private http: HttpClient) { }
signOut(): void {
window.localStorage.clear();
......@@ -198,17 +201,55 @@ export class TokenService {
return new CompanyModel();
}
public saveAppToken(module: string): void {
let tokenkey = this.apptoken[module]
public saveAppToken(module: string, token?: string): Observable<any> {
if (module === 'MYJOB') {
const modulesData = this.getModulesData() as any;
const myjobModule = modulesData?.modules?.find((m: { app: string; }) => m.app === 'MYJOB');
const refreshToken = myjobModule ? myjobModule.refreshToken : null;
if (!refreshToken) {
console.error('MYJOB Refresh Token not found in modules data! Falling back to legacy method.');
this.executeLegacySave(module, token);
return of(null);
}
return this.http.post<any>(environment.baseUrl + "/auth/portal/refresh-token", {
"refreshToken": refreshToken
}).pipe(
tap(response => {
if (response && response.accessToken) {
this.saveTokenToStorage(module, response.accessToken);
} else {
console.error('accessToken not found in postPortalRefreshToken response for MYJOB. Falling back to legacy method.');
this.executeLegacySave(module, token);
}
})
);
} else {
this.executeLegacySave(module, token);
return of(null);
}
}
private executeLegacySave(module: string, token?: string): void {
const tokenkey = token || this.apptoken[module];
this.saveTokenToStorage(module, tokenkey);
}
private saveTokenToStorage(module: string, tokenkey: string | null): void {
if (tokenkey && tokenkey.startsWith('Bearer ')) {
tokenkey = tokenkey.substring(7);
}
window.sessionStorage.setItem("module", module);
// window.localStorage.removeItem(APP_TOKEN_KEY);
// window.localStorage.setItem(APP_TOKEN_KEY, tokenkey);
window.sessionStorage.removeItem(APP_TOKEN_KEY);
window.sessionStorage.setItem(APP_TOKEN_KEY, tokenkey);
let db = this.dbApp[module]
window.localStorage.removeItem(COMPANY);
window.localStorage.setItem(COMPANY, JSON.stringify(new CompanyModel(db)));
if (tokenkey) {
window.sessionStorage.setItem(APP_TOKEN_KEY, tokenkey);
}
const db = this.dbApp[module];
if (db) {
window.localStorage.removeItem(COMPANY);
window.localStorage.setItem(COMPANY, JSON.stringify(new CompanyModel(db)));
}
}
public getAppToken(): string | null {
......@@ -221,4 +262,31 @@ export class TokenService {
return window.sessionStorage.getItem("module");
}
public saveModulesData(modules: any[]): void {
window.sessionStorage.removeItem(MODULE_KEY);
window.sessionStorage.setItem(MODULE_KEY, JSON.stringify(modules));
}
public getModulesData(): any[] | null {
const data = window.sessionStorage.getItem(MODULE_KEY);
if (data) {
return JSON.parse(data);
}
return null;
}
public saveAppAccessToken(module: string, accessToken: string): void {
if (!module || !accessToken) return;
let token = accessToken;
if (token.startsWith('Bearer ')) {
token = token.substring(7);
}
window.sessionStorage.setItem(`accessToken-${module}`, token);
}
public getAppAccessToken(module: string): string | null {
if (!module) return null;
return window.sessionStorage.getItem(`accessToken-${module}`);
}
}
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