merged shortener page into admin page, refactored into modal
This commit is contained in:
1 parent
d1d72022da
commit
6110248668
14 files changed
+363
-337
No files matched your search
@@ -18,10 +18,10 @@ export class AuthService {
|
||||
return localStorage.getItem('token');
|
||||
}
|
||||
|
||||
login(username: string, password: string): Observable<any> {
|
||||
return this.http.post(this.apiUrl, { username, password }).pipe(
|
||||
tap((response: any) => {
|
||||
if (response?.token) localStorage.setItem('token', response.token);
|
||||
login(username: string, password: string): Observable<{ token: string }> {
|
||||
return this.http.post<{ token: string }>(this.apiUrl, { username, password }).pipe(
|
||||
tap(response => {
|
||||
localStorage.setItem('token', response.token);
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -29,60 +29,47 @@ export class AuthService {
|
||||
logout(): void {
|
||||
if (this.redirectingToLogin) return;
|
||||
|
||||
localStorage.removeItem('token');
|
||||
|
||||
if (this.router.url.startsWith('/login')) {
|
||||
localStorage.removeItem('token');
|
||||
return;
|
||||
}
|
||||
|
||||
this.redirectingToLogin = true;
|
||||
|
||||
localStorage.removeItem('token');
|
||||
|
||||
const tree = this.router.parseUrl(this.router.url);
|
||||
delete tree.queryParams?.['redirectUrl'];
|
||||
const cleanRedirectUrl = tree.toString();
|
||||
|
||||
void this.router.navigate(['/login'], {
|
||||
queryParams: { redirectUrl: cleanRedirectUrl },
|
||||
replaceUrl: true
|
||||
}).finally(() => {
|
||||
void this.router.navigate(['/login'], { replaceUrl: true }).finally(() => {
|
||||
this.redirectingToLogin = false;
|
||||
});
|
||||
}
|
||||
|
||||
private isJwtNotExpired(token: string | null): boolean {
|
||||
if (!token) return false;
|
||||
|
||||
try {
|
||||
const payload = JSON.parse(atob(token.split('.')[1]));
|
||||
const expMs = payload.exp * 1000;
|
||||
return expMs > Date.now();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
isTokenValid(): Observable<boolean> {
|
||||
validateToken(): Observable<boolean> {
|
||||
const token = this.token;
|
||||
|
||||
if (!this.isJwtNotExpired(token)) {
|
||||
if (!token) {
|
||||
this.logout();
|
||||
return of(false);
|
||||
}
|
||||
|
||||
return this.http.get('/api/users', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
try {
|
||||
const payload = JSON.parse(atob(token.split('.')[1]));
|
||||
const isValid = payload.exp * 1000;
|
||||
|
||||
if (!isValid) {
|
||||
this.logout();
|
||||
return of(false);
|
||||
}
|
||||
} catch {
|
||||
this.logout();
|
||||
return of(false);
|
||||
}
|
||||
|
||||
return this.http.get('api/users', {
|
||||
headers: {Authorization: `Bearer ${token}`}
|
||||
}).pipe(
|
||||
map(() => true),
|
||||
catchError(() => of(false))
|
||||
);
|
||||
}
|
||||
|
||||
validateToken(): Observable<boolean> {
|
||||
return this.isTokenValid().pipe(
|
||||
tap(valid => {
|
||||
if (!valid) {
|
||||
this.logout();
|
||||
}
|
||||
catchError(() => {
|
||||
this.logout();
|
||||
return of(false);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user