added correct logout to frontend
This commit is contained in:
@ -5,7 +5,7 @@
|
|||||||
<h1 class="text-2xl">My Blog</h1>
|
<h1 class="text-2xl">My Blog</h1>
|
||||||
</a>
|
</a>
|
||||||
<nav class="flex gap-5">
|
<nav class="flex gap-5">
|
||||||
<a routerLink="/admin" *ngIf="loggedIn()">admin</a>
|
<a routerLink="/dashboard" *ngIf="loggedIn()">admin</a>
|
||||||
<button *ngIf="!loggedIn()" (click)="toggleLogin()">login</button>
|
<button *ngIf="!loggedIn()" (click)="toggleLogin()">login</button>
|
||||||
<button *ngIf="loggedIn()" (click)="logOut()">logout</button>
|
<button *ngIf="loggedIn()" (click)="logOut()">logout</button>
|
||||||
</nav>
|
</nav>
|
||||||
|
@ -27,6 +27,6 @@ export class AppComponent {
|
|||||||
this.loginOpen = !this.loginOpen;
|
this.loginOpen = !this.loginOpen;
|
||||||
}
|
}
|
||||||
logOut() {
|
logOut() {
|
||||||
this.auth.jwt.set(null);
|
this.auth.logout();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import {
|
|||||||
signal,
|
signal,
|
||||||
WritableSignal,
|
WritableSignal,
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import { LoginResponse, User, Token } from '../interfaces/auth';
|
import { LoginResponse, User, Claims } from '../interfaces/auth';
|
||||||
import { environment } from '../../../environments/environment';
|
import { environment } from '../../../environments/environment';
|
||||||
|
|
||||||
const JWT_KEY = 'token';
|
const JWT_KEY = 'token';
|
||||||
@ -17,6 +17,7 @@ const JWT_KEY = 'token';
|
|||||||
export class AuthService {
|
export class AuthService {
|
||||||
private http = inject(HttpClient);
|
private http = inject(HttpClient);
|
||||||
jwt: WritableSignal<string | null> = signal(null);
|
jwt: WritableSignal<string | null> = signal(null);
|
||||||
|
claims: WritableSignal<Claims | null> = signal(null);
|
||||||
timeout: any | null = null;
|
timeout: any | null = null;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -26,6 +27,16 @@ export class AuthService {
|
|||||||
this.jwt.set(token);
|
this.jwt.set(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update claims
|
||||||
|
effect(() => {
|
||||||
|
const token = this.jwt();
|
||||||
|
if (!token) {
|
||||||
|
this.claims.set(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.claims.set(parseJwt(token));
|
||||||
|
});
|
||||||
|
|
||||||
// set localStorage when this.jwt changes
|
// set localStorage when this.jwt changes
|
||||||
effect(() => {
|
effect(() => {
|
||||||
const token = this.jwt();
|
const token = this.jwt();
|
||||||
@ -65,6 +76,11 @@ export class AuthService {
|
|||||||
.post<LoginResponse>(`${environment.apiRoot}/login`, user)
|
.post<LoginResponse>(`${environment.apiRoot}/login`, user)
|
||||||
.subscribe((res) => this.jwt.set(res.token));
|
.subscribe((res) => this.jwt.set(res.token));
|
||||||
}
|
}
|
||||||
|
logout() {
|
||||||
|
this.http.delete(`${environment.apiRoot}/logout`).subscribe(() => {
|
||||||
|
this.jwt.set(null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private clearTimeout() {
|
private clearTimeout() {
|
||||||
if (this.timeout) {
|
if (this.timeout) {
|
||||||
@ -75,7 +91,7 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/38552003/how-to-decode-jwt-token-in-javascript-without-using-a-library
|
// https://stackoverflow.com/questions/38552003/how-to-decode-jwt-token-in-javascript-without-using-a-library
|
||||||
function parseJwt(token: string): Token {
|
function parseJwt(token: string): Claims {
|
||||||
var base64Url = token.split('.')[1];
|
var base64Url = token.split('.')[1];
|
||||||
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
||||||
var jsonPayload = decodeURIComponent(
|
var jsonPayload = decodeURIComponent(
|
||||||
|
Reference in New Issue
Block a user