Skip to content

Commit ba28783

Browse files
committed
fix authenticated
1 parent fe9cc3a commit ba28783

File tree

5 files changed

+13
-18
lines changed

5 files changed

+13
-18
lines changed

src/app/features/auth/pages/error-404/error-404.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="error-container">
22
<!-- <div class="error-icon">😕</div> -->
33
<!-- <h1>404</h1> -->
4-
<ng-lottie [options]="lottieOptions" style="width: 500px; height: 500px">
4+
<ng-lottie [options]="lottieOptions" style="width: 700px; height: 400px">
55
</ng-lottie>
66
<h2>Không tìm thấy trang</h2>
77
<p>

src/app/features/auth/pages/error-404/error-404.component.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
justify-content: center;
66
min-height: 80vh;
77
text-align: center;
8+
margin-top: 3rem;
89
width: 100vw;
910
}
1011

src/app/shared/components/my-shared/header/header.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Component } from '@angular/core';
33
import { Router } from '@angular/router';
44
import { Store } from '@ngrx/store';
55
import { ProfileMenuComponent } from './profile-menu.component';
6-
import { decodeJWT } from '../../../utils/stringProcess';
76
import { ToggleSwitch } from '../../fxdonad-shared/toggle-switch/toggle-switch';
87
import { ThemeService } from '../../../../styles/theme-service/theme.service';
98
import { ProfileService } from '../../../../core/services/api-service/profile.service';
@@ -16,6 +15,7 @@ import { NotificationModalComponent } from './notification-modal/notification-mo
1615
import { NotificationSocketService } from '../../../../core/services/socket-service/notification-socket.service';
1716
import { NotificationListService } from '../../../../core/services/api-service/notification-list.service';
1817
import { sendNotification } from '../../../utils/notification';
18+
import { checkAuthenticated } from '../../../utils/authenRoleActions';
1919

2020
@Component({
2121
selector: 'app-header',
@@ -37,7 +37,7 @@ export class HeaderComponent {
3737
isLoggedIn: boolean = false;
3838
showProfileMenu = false;
3939
isMenuVisible = false;
40-
timeExpiresAt: string = '';
40+
timeExpiresAt: Date = new Date();
4141
avatarUrl: string = '';
4242
avatarDefault = avatarUrlDefault;
4343
setPassword = false;
@@ -89,11 +89,8 @@ export class HeaderComponent {
8989

9090
this.getCountNotice();
9191

92-
this.timeExpiresAt =
93-
decodeJWT(localStorage.getItem('token') ?? '')?.expiresAt || '';
94-
const expiresAt = new Date(this.timeExpiresAt).getTime();
95-
// this.isLoggedIn = !isNaN(expiresAt) && Date.now() < expiresAt;
96-
this.isLoggedIn = !!localStorage.getItem('token');
92+
//kiểm tra trạng thái đăng nhập có hợp lệ không
93+
this.isLoggedIn = checkAuthenticated();
9794
}
9895

9996
organizations = [

src/app/shared/utils/authenRoleActions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ export function activeForMyContent(
2727

2828
export function checkAuthenticated(): boolean {
2929
const decoded = decodeJWT(localStorage.getItem('token') ?? '');
30+
console.log('expiresAt:', decoded.expiresAt);
31+
console.log('now:', new Date());
3032

3133
if (decoded?.expiresAt) {
32-
const expiresAt = new Date(decoded.expiresAt); // nếu expiresAt là string
33-
return expiresAt > new Date();
34+
return decoded.expiresAt > new Date();
3435
} else {
3536
return false;
3637
}

src/app/shared/utils/stringProcess.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ export function removeSpecialCharacters(str: string): string {
121121
export function decodeJWT(token: string | null): {
122122
header: any;
123123
payload: DecodedJwtPayload;
124-
issuedAt?: string;
125-
expiresAt?: string;
124+
issuedAt?: Date;
125+
expiresAt?: Date;
126126
} {
127127
try {
128128
if (!token) {
@@ -165,13 +165,9 @@ export function decodeJWT(token: string | null): {
165165
const header = base64UrlDecode(header64);
166166
const payload = base64UrlDecode(payload64) as DecodedJwtPayload;
167167

168-
const issuedAt = payload.iat
169-
? new Date(payload.iat * 1000).toLocaleString()
170-
: undefined;
168+
const issuedAt = payload.iat ? new Date(payload.iat * 1000) : undefined;
171169

172-
const expiresAt = payload.exp
173-
? new Date(payload.exp * 1000).toLocaleString()
174-
: undefined;
170+
const expiresAt = payload.exp ? new Date(payload.exp * 1000) : undefined;
175171

176172
return {
177173
header,

0 commit comments

Comments
 (0)