-เพิ่ม guard jwt token

-เพิ่ม  jwt-decoded
-เพิ่ม  ระบบ first setup ดึงข้อมูล  accounting
This commit is contained in:
2025-11-13 11:45:03 +07:00
parent fe028d274b
commit f25488370a
3 changed files with 60 additions and 19 deletions

View File

@@ -61,6 +61,7 @@
"bootstrap": "^5.3.8", "bootstrap": "^5.3.8",
"chart.js": "^4.5.1", "chart.js": "^4.5.1",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"jwt-decode": "^4.0.0",
"ng2-charts": "^6.0.1", "ng2-charts": "^6.0.1",
"postcss": "^8.5.6", "postcss": "^8.5.6",
"rxjs": "~7.8.0", "rxjs": "~7.8.0",
@@ -73,6 +74,7 @@
"@angular/cli": "^20.3.9", "@angular/cli": "^20.3.9",
"@angular/compiler-cli": "^20.3.10", "@angular/compiler-cli": "^20.3.10",
"@capacitor/cli": "latest", "@capacitor/cli": "latest",
"@types/jasmine": "~5.1.0",
"cross-env": "^10.1.0", "cross-env": "^10.1.0",
"electron": "^39.0.0", "electron": "^39.0.0",
"electron-builder": "^26.0.12", "electron-builder": "^26.0.12",
@@ -84,7 +86,6 @@
"karma-jasmine-html-reporter": "~2.1.0", "karma-jasmine-html-reporter": "~2.1.0",
"ngx-toastr": "^19.1.0", "ngx-toastr": "^19.1.0",
"postcss": "^8.5.3", "postcss": "^8.5.3",
"typescript": "~5.9.3", "typescript": "~5.9.3"
"@types/jasmine": "~5.1.0"
} }
} }

View File

@@ -47,31 +47,53 @@ export class MainDashboardContentComponent implements OnInit {
constructor(private generalService: GeneralService) {} constructor(private generalService: GeneralService) {}
ngOnInit(): void { ngOnInit(): void {
this.fetchChartData(); let token = localStorage.getItem('access_token')
this.OnSearchAct(token, true);
} }
fetchChartData(): void { OnSearchAct(value: any, setupFirst: boolean): void {
// NOTE: Using a placeholder endpoint as the actual one was not provided. const uri = '/api/web/accountingSearch';
const uri = '/api/dashboard/summary-last-6-months'; let request = {
token: value
this.generalService.getRequest(uri).subscribe({ }
this.generalService.postRequest(uri, request).subscribe({
next: (result: any) => { next: (result: any) => {
if (result.code === '200' && result.data) { if (result.code === '200') {
this.processChartData(result.data); this.generalService.trowApi(result);
} else { console.log(`✅ OTP ส่งไปที่ ${value.email}`);
console.warn('Could not fetch chart data:', result.message_th);
// Optionally, display placeholder data or an error message
this.setupPlaceholderData();
} }
}, },
error: (error: any) => { error: (error: any) => {
console.error('Error fetching chart data:', error);
// Display placeholder data on error to show the graph structure },
this.setupPlaceholderData(); complete: () => {
} }
}); });
} }
// fetchChartData(): void {
// // NOTE: Using a placeholder endpoint as the actual one was not provided.
// const uri = '/api/dashboard/summary-last-6-months';
// this.generalService.getRequest(uri).subscribe({
// next: (result: any) => {
// if (result.code === '200' && result.data) {
// this.processChartData(result.data);
// } else {
// console.warn('Could not fetch chart data:', result.message_th);
// // Optionally, display placeholder data or an error message
// this.setupPlaceholderData();
// }
// },
// error: (error: any) => {
// console.error('Error fetching chart data:', error);
// // Display placeholder data on error to show the graph structure
// this.setupPlaceholderData();
// }
// });
// }
processChartData(data: any[]): void { processChartData(data: any[]): void {
const labels = data.map(item => item.month); const labels = data.map(item => item.month);
const revenues = data.map(item => item.revenue); const revenues = data.map(item => item.revenue);

View File

@@ -1,12 +1,30 @@
import { inject } from '@angular/core'; import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router'; import { CanActivateFn, Router } from '@angular/router';
import { jwtDecode } from 'jwt-decode';
export const authGuard: CanActivateFn = (route, state) => { export const authGuard: CanActivateFn = (route, state) => {
const router = inject(Router); const router = inject(Router);
const accessToken = localStorage.getItem('access_token'); const accessToken = localStorage.getItem('access_token');
if (accessToken) { if (accessToken) {
return true; try {
const decodedToken: any = jwtDecode(accessToken);
const currentTime = Date.now() / 1000;
if (decodedToken.exp < currentTime) {
// Token expired
localStorage.removeItem('access_token');
router.navigate(['/login']);
return false;
}
return true;
} catch (error) {
// Error decoding token
localStorage.removeItem('access_token');
router.navigate(['/login']);
return false;
}
} else { } else {
router.navigate(['/login']); router.navigate(['/login']);
return false; return false;