Immutable signals
This commit is contained in:
parent
bf413c2e09
commit
88522f4e24
1 changed files with 27 additions and 26 deletions
|
@ -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';
|
||||||
|
@ -52,11 +59,11 @@ export class AuthService {
|
||||||
.pipe(takeUntilDestroyed(this.destroyed))
|
.pipe(takeUntilDestroyed(this.destroyed))
|
||||||
.subscribe({
|
.subscribe({
|
||||||
next: (res) => {
|
next: (res) => {
|
||||||
let state = <AuthState>{
|
let state = <AuthState>{
|
||||||
user: username,
|
user: username,
|
||||||
token: res.jwt,
|
token: res.jwt,
|
||||||
is_auth: true,
|
is_auth: true,
|
||||||
};
|
};
|
||||||
this._state.set(state);
|
this._state.set(state);
|
||||||
this.loginFailed.update(() => false);
|
this.loginFailed.update(() => false);
|
||||||
this._router.navigateByUrl('/');
|
this._router.navigateByUrl('/');
|
||||||
|
@ -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
|
next: (event) => {
|
||||||
.get('/api/logout', { withCredentials: true})
|
this._clearState();
|
||||||
.subscribe({
|
},
|
||||||
next: (event) => {
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue