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