Fix #107 #108

Merged
amigan merged 3 commits from authSignals into shareUI 2025-02-09 09:51:20 -05:00
Showing only changes of commit 88522f4e24 - Show all commits

View file

@ -1,4 +1,11 @@
import { Injectable, signal, computed, effect, inject, DestroyRef } from '@angular/core'; import {
Injectable,
signal,
computed,
effect,
inject,
DestroyRef,
} from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http'; import { HttpClient, HttpResponse } from '@angular/common/http';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { Observable, Subject } from 'rxjs'; import { Observable, Subject } from 'rxjs';
@ -68,25 +75,17 @@ export class AuthService {
} }
_clearState() { _clearState() {
this._state.update((state) => { this._state.set(<AuthState>{});
state.is_auth = false;
state.token = null;
return state;
});
} }
logout() { logout() {
console.log("logout!"); this.http.get('/api/logout', { withCredentials: true }).subscribe({
this.http
.get('/api/logout', { withCredentials: true})
.subscribe({
next: (event) => { next: (event) => {
this._clearState(); this._clearState();
}, },
error: (err) => { error: (err) => {
this._clearState(); this._clearState();
} },
}); });
this._router.navigateByUrl('/login'); this._router.navigateByUrl('/login');
} }
@ -97,12 +96,14 @@ this._state.update((state) => {
.pipe( .pipe(
tap((event) => { tap((event) => {
if (event.status == 200) { if (event.status == 200) {
let ost = this._state();
let tok = event.body?.jwt.toString(); let tok = event.body?.jwt.toString();
this._state.update((state) => { let state = <AuthState>{
state.is_auth = true; user: ost.user,
state.token = tok ? tok : null; token: tok ? tok : null,
return state; is_auth: true,
}); };
this._state.set(state);
} }
}), }),
); );