begin auth cookie

This commit is contained in:
eneller
2026-03-08 19:02:08 +01:00
parent f5ae9ac9e6
commit cd568f0a63
6 changed files with 56 additions and 10 deletions

View File

@@ -36,7 +36,7 @@ export class ScreenLogin {
this.api.login(this.loginForm.value.username, this.loginForm.value.password).subscribe({
next: () => {
//this.router.navigate(['']);
this.router.navigate(['']);
},
error: (err) => {
this.error = err.error?.message || 'Login failed. Please try again.';

View File

@@ -1,6 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { BehaviorSubject, catchError, map, Observable, of, tap } from 'rxjs';
import Transaction from '@model/transaction'
@Injectable({
@@ -8,6 +8,8 @@ import Transaction from '@model/transaction'
})
export class APIService {
private apiUrl = 'http://localhost:3000/api'
private isAuthenticatedSubject = new BehaviorSubject<boolean>(false);
isAuthenticated$ = this.isAuthenticatedSubject.asObservable();
constructor(private http: HttpClient){}
@@ -20,4 +22,14 @@ export class APIService {
logout(): Observable<any>{
return this.http.post(this.apiUrl + '/auth/logout', {});
}
checkAuthStatus(): Observable<boolean> {
return this.http.get(`${this.apiUrl}/auth/status`, { withCredentials: true }).pipe(
map(() => true),
catchError(() => of(false)),
tap({
next: () => this.isAuthenticatedSubject.next(true),
error: () => this.isAuthenticatedSubject.next(false),
})
);
}
}