Compare commits
2 Commits
d8f65f1241
...
d96572f975
Author | SHA1 | Date |
---|---|---|
Igor Hrenowitsch Propisnov | d96572f975 | |
Igor Hrenowitsch Propisnov | 876f0b42b8 |
|
@ -14,6 +14,7 @@ export class CorsMiddleware implements NestMiddleware {
|
||||||
const requestOrigin = req.headers.origin;
|
const requestOrigin = req.headers.origin;
|
||||||
|
|
||||||
if (!requestOrigin || allowedOrigins.includes(requestOrigin)) {
|
if (!requestOrigin || allowedOrigins.includes(requestOrigin)) {
|
||||||
|
res.header('Access-Control-Allow-Credentials', 'true');
|
||||||
res.header('Access-Control-Allow-Origin', requestOrigin || '*');
|
res.header('Access-Control-Allow-Origin', requestOrigin || '*');
|
||||||
res.header(
|
res.header(
|
||||||
'Access-Control-Allow-Methods',
|
'Access-Control-Allow-Methods',
|
||||||
|
|
|
@ -12,6 +12,7 @@ export class CspMiddleware implements NestMiddleware {
|
||||||
if (cspDirectives) {
|
if (cspDirectives) {
|
||||||
res.setHeader('Content-Security-Policy', cspDirectives);
|
res.setHeader('Content-Security-Policy', cspDirectives);
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,9 +56,9 @@ export class AuthController {
|
||||||
})
|
})
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@UseGuards(SessionGuard)
|
@UseGuards(SessionGuard)
|
||||||
@Post('logout')
|
@Post('signout')
|
||||||
public async logout(@Req() request: Request): Promise<SuccessDto> {
|
public async signout(@Req() request: Request): Promise<SuccessDto> {
|
||||||
return this.authService.logout(request.sessionID);
|
return this.authService.signout(request.sessionID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiCreatedResponse({
|
@ApiCreatedResponse({
|
||||||
|
|
|
@ -110,6 +110,18 @@ export class AuthService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async signout(sessionId: string): Promise<{ success: boolean }> {
|
||||||
|
try {
|
||||||
|
this.sessionService.deleteSessionBySessionId(sessionId);
|
||||||
|
return { success: true };
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Fehler beim Logout',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async checkAuthStatus(
|
public async checkAuthStatus(
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
userAgend: string
|
userAgend: string
|
||||||
|
@ -145,16 +157,4 @@ export class AuthService {
|
||||||
|
|
||||||
return responseData;
|
return responseData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async logout(sessionId: string): Promise<{ success: boolean }> {
|
|
||||||
try {
|
|
||||||
this.sessionService.deleteSessionBySessionId(sessionId);
|
|
||||||
return { success: true };
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Fehler beim Logout',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,10 +27,14 @@ export class SessionInitService {
|
||||||
maxAge: 86400000,
|
maxAge: 86400000,
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure:
|
secure:
|
||||||
this.configService.get<string>('NODE_ENV') === 'production'
|
this.configService.get<string>('NODE_ENV') === 'development'
|
||||||
? true
|
? false
|
||||||
: false,
|
: true,
|
||||||
sameSite: 'strict',
|
|
||||||
|
sameSite:
|
||||||
|
this.configService.get<string>('NODE_ENV') === 'development'
|
||||||
|
? 'strict'
|
||||||
|
: 'none',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { Component, OnInit } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
import { RouterOutlet, Router } from '@angular/router';
|
import { RouterOutlet } from '@angular/router';
|
||||||
|
|
||||||
import { AuthService } from './shared/service';
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
|
@ -10,24 +9,6 @@ import { AuthService } from './shared/service';
|
||||||
templateUrl: './app.component.html',
|
templateUrl: './app.component.html',
|
||||||
styleUrl: './app.component.scss',
|
styleUrl: './app.component.scss',
|
||||||
})
|
})
|
||||||
export class AppComponent implements OnInit {
|
export class AppComponent {
|
||||||
public constructor(
|
public constructor() {}
|
||||||
private readonly authService: AuthService,
|
|
||||||
private readonly router: Router
|
|
||||||
) {}
|
|
||||||
|
|
||||||
public ngOnInit(): void {
|
|
||||||
this.checkAuthentication();
|
|
||||||
}
|
|
||||||
|
|
||||||
private checkAuthentication(): void {
|
|
||||||
this.authService.isAuthenticated$.subscribe((isAuthenticated: boolean) => {
|
|
||||||
if (isAuthenticated) {
|
|
||||||
console.log('User is authenticated');
|
|
||||||
this.router.navigateByUrl('dashboard');
|
|
||||||
} else {
|
|
||||||
this.router.navigateByUrl('signup');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,18 @@ import { ApplicationConfig } from '@angular/core';
|
||||||
import { provideAnimations } from '@angular/platform-browser/animations';
|
import { provideAnimations } from '@angular/platform-browser/animations';
|
||||||
import { provideRouter, withComponentInputBinding } from '@angular/router';
|
import { provideRouter, withComponentInputBinding } from '@angular/router';
|
||||||
|
|
||||||
|
import { Configuration } from './api';
|
||||||
import { routes } from './app.routes';
|
import { routes } from './app.routes';
|
||||||
import { AuthInterceptor } from './shared/interceptors/auth.interceptor';
|
import { ApiConfiguration } from './config/api-configuration';
|
||||||
|
|
||||||
|
const apiConfiguration = new ApiConfiguration({
|
||||||
|
withCredentials: true,
|
||||||
|
});
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [
|
providers: [
|
||||||
provideHttpClient(withInterceptors([AuthInterceptor])),
|
{ provide: Configuration, useValue: apiConfiguration },
|
||||||
|
provideHttpClient(withInterceptors([])),
|
||||||
provideRouter(routes, withComponentInputBinding()),
|
provideRouter(routes, withComponentInputBinding()),
|
||||||
provideAnimations(),
|
provideAnimations(),
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import { Routes } from '@angular/router';
|
import { Routes } from '@angular/router';
|
||||||
|
|
||||||
import { AuthGuard } from './shared/guard/auth.guard';
|
|
||||||
|
|
||||||
const publicRoutes: Routes = [
|
const publicRoutes: Routes = [
|
||||||
{
|
{
|
||||||
path: '',
|
path: '',
|
||||||
loadComponent: () => import('./app.component').then((m) => m.AppComponent),
|
loadComponent: () =>
|
||||||
|
import('./pages/home-root/home-root.component').then(
|
||||||
|
(m) => m.HomeComponent
|
||||||
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'signup',
|
path: 'signup',
|
||||||
|
@ -30,7 +31,7 @@ const protectedRoutes: Routes = [
|
||||||
import('./pages/dashboard-root/dashboard-root.component').then(
|
import('./pages/dashboard-root/dashboard-root.component').then(
|
||||||
(m) => m.DashboardRootComponent
|
(m) => m.DashboardRootComponent
|
||||||
),
|
),
|
||||||
canActivate: [AuthGuard],
|
canActivate: [],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
import { Configuration, ConfigurationParameters } from '../api';
|
||||||
|
|
||||||
|
export class ApiConfiguration extends Configuration {
|
||||||
|
public constructor(params?: Partial<ConfigurationParameters>) {
|
||||||
|
super({
|
||||||
|
...params,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
import { HttpErrorResponse } from '@angular/common/http';
|
||||||
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
import { Router } from '@angular/router';
|
||||||
|
|
||||||
|
import { SuccessDtoApiModel } from '../../api';
|
||||||
|
import { AuthService } from '../../shared/service';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-foo',
|
||||||
|
standalone: true,
|
||||||
|
providers: [],
|
||||||
|
imports: [],
|
||||||
|
templateUrl: './home-root.component.html',
|
||||||
|
styleUrl: './home-root.component.scss',
|
||||||
|
})
|
||||||
|
export class HomeComponent implements OnInit {
|
||||||
|
public constructor(
|
||||||
|
private readonly authService: AuthService,
|
||||||
|
private readonly router: Router
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public ngOnInit(): void {
|
||||||
|
this.authService.status().subscribe(
|
||||||
|
(response: SuccessDtoApiModel) => {
|
||||||
|
if (response.success) {
|
||||||
|
this.router.navigate(['/dashboard']);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(error: HttpErrorResponse) => {
|
||||||
|
if (error.status === 401) {
|
||||||
|
this.router.navigate(['signup'], {
|
||||||
|
queryParams: { login: true },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,25 @@
|
||||||
<div id="background">
|
<div id="background">
|
||||||
<div class="img-zone">
|
<div class="img-zone">
|
||||||
<div class="img-wrapper">
|
<div class="img-wrapper">
|
||||||
|
@if (userSignupSuccess()) {
|
||||||
|
<div class="success">
|
||||||
|
<h1>Danke für deine Registrierung!</h1>
|
||||||
|
<h2>
|
||||||
|
Wir haben dir eine Mail geschickt an
|
||||||
|
{{ form?.get('email')?.value }}. Bitte bestätige deine
|
||||||
|
E-Mail-Adresse um fortzufahren.
|
||||||
|
</h2>
|
||||||
|
<p>Du kannst diesen Tab nun schließen</p>
|
||||||
|
</div>
|
||||||
|
} @else {
|
||||||
|
<div class="headline">
|
||||||
<h1>Hi, Welcome to Ticket App.</h1>
|
<h1>Hi, Welcome to Ticket App.</h1>
|
||||||
</div>
|
</div>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if (!userSignupSuccess()) {
|
||||||
<div class="content-zone">
|
<div class="content-zone">
|
||||||
<h1>
|
<h1>
|
||||||
@if (isSignupSignal()) {
|
@if (isSignupSignal()) {
|
||||||
|
@ -29,7 +45,6 @@
|
||||||
(click)="toggleAction('register')"></button>
|
(click)="toggleAction('register')"></button>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
@if (isSignupSignal() || isRegisterSignal()) {
|
@if (isSignupSignal() || isRegisterSignal()) {
|
||||||
<div class="register-wrapper">
|
<div class="register-wrapper">
|
||||||
@if (form) {
|
@if (form) {
|
||||||
|
@ -91,4 +106,5 @@
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -12,11 +12,21 @@
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|
||||||
|
.success {
|
||||||
|
margin-left: 4em;
|
||||||
|
h1 {
|
||||||
|
font-size: 4em;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.headline {
|
||||||
h1 {
|
h1 {
|
||||||
font-size: 4em;
|
font-size: 4em;
|
||||||
margin-left: 1em;
|
margin-left: 1em;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.content-zone {
|
.content-zone {
|
||||||
|
|
|
@ -25,8 +25,14 @@ import { CheckboxModule } from 'primeng/checkbox';
|
||||||
import { InputTextModule } from 'primeng/inputtext';
|
import { InputTextModule } from 'primeng/inputtext';
|
||||||
import { PasswordModule } from 'primeng/password';
|
import { PasswordModule } from 'primeng/password';
|
||||||
|
|
||||||
import { AuthService } from '../../shared/service';
|
import {
|
||||||
import { LoginCredentials } from '../../shared/types';
|
Configuration,
|
||||||
|
SigninResponseDtoApiModel,
|
||||||
|
SuccessDtoApiModel,
|
||||||
|
UserCredentialsDtoApiModel,
|
||||||
|
} from '../../api';
|
||||||
|
import { ApiConfiguration } from '../../config/api-configuration';
|
||||||
|
import { AuthService, SessionStorageService } from '../../shared/service';
|
||||||
import {
|
import {
|
||||||
customEmailValidator,
|
customEmailValidator,
|
||||||
customPasswordValidator,
|
customPasswordValidator,
|
||||||
|
@ -47,13 +53,20 @@ type AuthAction = 'register' | 'signup';
|
||||||
PasswordModule,
|
PasswordModule,
|
||||||
HttpClientModule,
|
HttpClientModule,
|
||||||
],
|
],
|
||||||
providers: [],
|
providers: [
|
||||||
|
{
|
||||||
|
provide: Configuration,
|
||||||
|
useFactory: (): unknown =>
|
||||||
|
new ApiConfiguration({ withCredentials: true }),
|
||||||
|
},
|
||||||
|
],
|
||||||
templateUrl: './register-root.component.html',
|
templateUrl: './register-root.component.html',
|
||||||
styleUrl: './register-root.component.scss',
|
styleUrl: './register-root.component.scss',
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
export class RegisterRootComponent implements OnInit {
|
export class RegisterRootComponent implements OnInit {
|
||||||
public verified: InputSignal<boolean> = input<boolean>(false);
|
public verified: InputSignal<boolean> = input<boolean>(false);
|
||||||
|
public login: InputSignal<boolean> = input<boolean>(false);
|
||||||
public email: InputSignal<string> = input<string>('');
|
public email: InputSignal<string> = input<string>('');
|
||||||
public form: FormGroup | undefined;
|
public form: FormGroup | undefined;
|
||||||
public isRegisterSignal: WritableSignal<boolean> = signal(false);
|
public isRegisterSignal: WritableSignal<boolean> = signal(false);
|
||||||
|
@ -62,12 +75,14 @@ export class RegisterRootComponent implements OnInit {
|
||||||
public emailInvalid: WritableSignal<string | null> = signal(null);
|
public emailInvalid: WritableSignal<string | null> = signal(null);
|
||||||
public passwordInvalid: WritableSignal<string | null> = signal(null);
|
public passwordInvalid: WritableSignal<string | null> = signal(null);
|
||||||
public termsInvalid: WritableSignal<string | null> = signal(null);
|
public termsInvalid: WritableSignal<string | null> = signal(null);
|
||||||
|
public userSignupSuccess: WritableSignal<boolean> = signal(false);
|
||||||
private removeQueryParams: WritableSignal<boolean> = signal(false);
|
private removeQueryParams: WritableSignal<boolean> = signal(false);
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly formBuilder: FormBuilder,
|
private readonly formBuilder: FormBuilder,
|
||||||
private readonly authService: AuthService,
|
private readonly authService: AuthService,
|
||||||
private readonly router: Router
|
private readonly router: Router,
|
||||||
|
private readonly sessionStorageService: SessionStorageService
|
||||||
) {
|
) {
|
||||||
effect(() => {
|
effect(() => {
|
||||||
if (this.form) {
|
if (this.form) {
|
||||||
|
@ -90,13 +105,22 @@ export class RegisterRootComponent implements OnInit {
|
||||||
public ngOnInit(): void {
|
public ngOnInit(): void {
|
||||||
this.initializeForm();
|
this.initializeForm();
|
||||||
this.setupValueChanges();
|
this.setupValueChanges();
|
||||||
|
this.preselectForm();
|
||||||
|
|
||||||
if (this.email() || this.verified()) {
|
if ((this.email() && this.verified()) || this.login()) {
|
||||||
this.handleRedirect();
|
this.handleRedirect();
|
||||||
this.removeQueryParams.set(true);
|
this.removeQueryParams.set(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public preselectForm(): void {
|
||||||
|
if (!this.email() || !this.verified()) {
|
||||||
|
const email = this.sessionStorageService.getItem('email');
|
||||||
|
|
||||||
|
this.form?.get('email')?.setValue(email);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public toggleAction(action: AuthAction): void {
|
public toggleAction(action: AuthAction): void {
|
||||||
if (action === 'register') {
|
if (action === 'register') {
|
||||||
this.isRegisterSignal.set(true);
|
this.isRegisterSignal.set(true);
|
||||||
|
@ -113,7 +137,7 @@ export class RegisterRootComponent implements OnInit {
|
||||||
|
|
||||||
if (this.form?.valid) {
|
if (this.form?.valid) {
|
||||||
if (this.isRegisterSignal()) {
|
if (this.isRegisterSignal()) {
|
||||||
this.register(this.form.value);
|
this.signup(this.form.value);
|
||||||
} else {
|
} else {
|
||||||
this.signin(this.form.value);
|
this.signin(this.form.value);
|
||||||
}
|
}
|
||||||
|
@ -150,7 +174,6 @@ export class RegisterRootComponent implements OnInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleRedirect(): void {
|
private handleRedirect(): void {
|
||||||
console.log('handleRedirect');
|
|
||||||
if (this.verified()) {
|
if (this.verified()) {
|
||||||
this.isDisplayButtons.set(false);
|
this.isDisplayButtons.set(false);
|
||||||
this.isRegisterSignal.set(false);
|
this.isRegisterSignal.set(false);
|
||||||
|
@ -159,6 +182,12 @@ export class RegisterRootComponent implements OnInit {
|
||||||
if (this.email()) {
|
if (this.email()) {
|
||||||
this.form?.get('email')?.setValue(decodeURIComponent(atob(this.email())));
|
this.form?.get('email')?.setValue(decodeURIComponent(atob(this.email())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.login()) {
|
||||||
|
this.isSignupSignal.set(true);
|
||||||
|
this.isDisplayButtons.set(false);
|
||||||
|
this.isRegisterSignal.set(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private clearRouteParams(): void {
|
private clearRouteParams(): void {
|
||||||
|
@ -237,11 +266,23 @@ export class RegisterRootComponent implements OnInit {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private signin(logiCredentials: LoginCredentials): void {
|
private signin(logiCredentials: UserCredentialsDtoApiModel): void {
|
||||||
this.authService.signin(logiCredentials);
|
this.authService
|
||||||
|
.signin(logiCredentials)
|
||||||
|
.subscribe((response: SigninResponseDtoApiModel) => {
|
||||||
|
if (response) {
|
||||||
|
this.router.navigate(['/dashboard']);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private register(logiCredentials: LoginCredentials): void {
|
private signup(logiCredentials: UserCredentialsDtoApiModel): void {
|
||||||
this.authService.signup(logiCredentials);
|
this.authService
|
||||||
|
.signup(logiCredentials)
|
||||||
|
.subscribe((response: SuccessDtoApiModel) => {
|
||||||
|
if (response.success) {
|
||||||
|
this.userSignupSuccess.set(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
import { inject } from '@angular/core';
|
|
||||||
import {
|
|
||||||
ActivatedRouteSnapshot,
|
|
||||||
CanActivateFn,
|
|
||||||
Router,
|
|
||||||
RouterStateSnapshot,
|
|
||||||
UrlTree,
|
|
||||||
} from '@angular/router';
|
|
||||||
|
|
||||||
import { Observable } from 'rxjs';
|
|
||||||
|
|
||||||
import { AuthService } from '../service';
|
|
||||||
|
|
||||||
export const AuthGuard: CanActivateFn = (
|
|
||||||
route: ActivatedRouteSnapshot,
|
|
||||||
state: RouterStateSnapshot
|
|
||||||
):
|
|
||||||
| Observable<boolean | UrlTree>
|
|
||||||
| Promise<boolean | UrlTree>
|
|
||||||
| boolean
|
|
||||||
| UrlTree => {
|
|
||||||
const authService: AuthService = inject(AuthService);
|
|
||||||
const router: Router = inject(Router);
|
|
||||||
|
|
||||||
authService.isAuthenticated$.subscribe((isAuthenticated: boolean) => {
|
|
||||||
if (!isAuthenticated) {
|
|
||||||
router.navigateByUrl('signup');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return true;
|
|
||||||
};
|
|
|
@ -1,79 +0,0 @@
|
||||||
import {
|
|
||||||
HttpInterceptorFn,
|
|
||||||
HttpRequest,
|
|
||||||
HttpHandlerFn,
|
|
||||||
HttpEvent,
|
|
||||||
HttpErrorResponse,
|
|
||||||
} from '@angular/common/http';
|
|
||||||
import { inject } from '@angular/core';
|
|
||||||
import { Router } from '@angular/router';
|
|
||||||
|
|
||||||
import { Observable, throwError } from 'rxjs';
|
|
||||||
import { catchError, switchMap } from 'rxjs/operators';
|
|
||||||
|
|
||||||
import { AuthService } from '../service';
|
|
||||||
|
|
||||||
export const AuthInterceptor: HttpInterceptorFn = (
|
|
||||||
request: HttpRequest<unknown>,
|
|
||||||
next: HttpHandlerFn
|
|
||||||
): Observable<HttpEvent<unknown>> => {
|
|
||||||
const router = inject(Router);
|
|
||||||
const authService = inject(AuthService);
|
|
||||||
|
|
||||||
const handleRequest = (
|
|
||||||
req: HttpRequest<unknown>
|
|
||||||
): Observable<HttpEvent<unknown>> => {
|
|
||||||
const accessToken = authService.access_token;
|
|
||||||
|
|
||||||
if (accessToken) {
|
|
||||||
req = addAuthHeader(req, accessToken);
|
|
||||||
}
|
|
||||||
return next(req);
|
|
||||||
};
|
|
||||||
|
|
||||||
const addAuthHeader = (
|
|
||||||
req: HttpRequest<unknown>,
|
|
||||||
token: string
|
|
||||||
): HttpRequest<unknown> => {
|
|
||||||
return req.clone({
|
|
||||||
setHeaders: {
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handle401Error = (
|
|
||||||
req: HttpRequest<unknown>
|
|
||||||
): Observable<HttpEvent<unknown>> => {
|
|
||||||
console.log(authService.refresh_token);
|
|
||||||
if (!authService.refresh_token) {
|
|
||||||
router.navigateByUrl('signup');
|
|
||||||
return throwError(() => new Error('Authentication required'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return authService.refreshToken().pipe(
|
|
||||||
switchMap((tokens) => {
|
|
||||||
req = addAuthHeader(req, tokens.access_token);
|
|
||||||
return next(req);
|
|
||||||
}),
|
|
||||||
catchError((refreshError) => {
|
|
||||||
router.navigateByUrl('signup');
|
|
||||||
return throwError(() => new Error(refreshError));
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleError = (
|
|
||||||
error: HttpErrorResponse,
|
|
||||||
req: HttpRequest<unknown>
|
|
||||||
): Observable<HttpEvent<unknown>> => {
|
|
||||||
if (error.status === 401) {
|
|
||||||
return handle401Error(req);
|
|
||||||
}
|
|
||||||
return throwError(() => new Error('Unhandled error'));
|
|
||||||
};
|
|
||||||
|
|
||||||
return handleRequest(request).pipe(
|
|
||||||
catchError((error) => handleError(error, request))
|
|
||||||
);
|
|
||||||
};
|
|
|
@ -1,12 +1,16 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
import { BehaviorSubject, Observable, tap } from 'rxjs';
|
import { BehaviorSubject, Observable } from 'rxjs';
|
||||||
|
|
||||||
|
import {
|
||||||
|
SigninResponseDtoApiModel,
|
||||||
|
UserCredentialsDtoApiModel,
|
||||||
|
} from '../../api';
|
||||||
import { AuthenticationApiService } from '../../api/api/authentication.api.service';
|
import { AuthenticationApiService } from '../../api/api/authentication.api.service';
|
||||||
import { LoginCredentials, Tokens } from '../types';
|
|
||||||
|
|
||||||
import { LocalStorageService } from './local-storage.service';
|
type SuccessResponse = {
|
||||||
import { SessionStorageService } from './session-storage.service';
|
success: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
|
@ -14,73 +18,28 @@ import { SessionStorageService } from './session-storage.service';
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
public isAuthenticated$: BehaviorSubject<boolean> =
|
public isAuthenticated$: BehaviorSubject<boolean> =
|
||||||
new BehaviorSubject<boolean>(false);
|
new BehaviorSubject<boolean>(false);
|
||||||
private _access_token: string | null = null;
|
|
||||||
private _refresh_token: string | null = null;
|
|
||||||
|
|
||||||
public get access_token(): string | null {
|
|
||||||
return this._access_token;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get refresh_token(): string | null {
|
|
||||||
return this._refresh_token;
|
|
||||||
}
|
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly localStorageService: LocalStorageService,
|
|
||||||
private readonly sessionStorageService: SessionStorageService,
|
|
||||||
private readonly authenticationApiService: AuthenticationApiService
|
private readonly authenticationApiService: AuthenticationApiService
|
||||||
) {
|
) {}
|
||||||
this._access_token =
|
|
||||||
this.localStorageService.getItem<string>('access_token');
|
public signup(
|
||||||
this._refresh_token =
|
credentials: UserCredentialsDtoApiModel
|
||||||
this.sessionStorageService.getItem<string>('refresh_token');
|
): Observable<SuccessResponse> {
|
||||||
|
return this.authenticationApiService.authControllerSignup(credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
public signin(credentials: LoginCredentials): void {
|
public signin(
|
||||||
this.authenticationApiService
|
credentials: UserCredentialsDtoApiModel
|
||||||
.authControllerSignin(credentials)
|
): Observable<SigninResponseDtoApiModel> {
|
||||||
.subscribe((response: Tokens) => {
|
return this.authenticationApiService.authControllerSignin(credentials);
|
||||||
this.handleSuccess(response);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public signup(credentials: LoginCredentials): void {
|
public signout(): Observable<SuccessResponse> {
|
||||||
this.authenticationApiService
|
return this.authenticationApiService.authControllerSignout();
|
||||||
.authControllerSignup(credentials)
|
|
||||||
.subscribe((response: Tokens) => {
|
|
||||||
this.handleSuccess(response);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public refreshToken(): Observable<Tokens> {
|
public status(): Observable<SuccessResponse> {
|
||||||
if (this._refresh_token) {
|
return this.authenticationApiService.authControllerStatus();
|
||||||
return this.authenticationApiService
|
|
||||||
.authControllerRefresh(this._refresh_token)
|
|
||||||
.pipe(tap((response: Tokens) => this.handleSuccess(response)));
|
|
||||||
} else {
|
|
||||||
throw new Error('Refresh token is missing');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public signout(): void {
|
|
||||||
this.authenticationApiService
|
|
||||||
.authControllerLogout()
|
|
||||||
.subscribe((response: boolean) => {
|
|
||||||
if (response) {
|
|
||||||
this._access_token = null;
|
|
||||||
this._refresh_token = null;
|
|
||||||
this.localStorageService.removeItem('access_token');
|
|
||||||
this.sessionStorageService.removeItem('refresh_token');
|
|
||||||
this.isAuthenticated$.next(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private handleSuccess(tokens: Tokens): void {
|
|
||||||
this._access_token = tokens.access_token;
|
|
||||||
this._refresh_token = tokens.refresh_token;
|
|
||||||
this.localStorageService.setItem('access_token', tokens.access_token);
|
|
||||||
this.sessionStorageService.setItem('refresh_token', tokens.refresh_token);
|
|
||||||
this.isAuthenticated$.next(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
export * from './login-credentials';
|
|
||||||
export * from './tokens';
|
|
|
@ -1,4 +0,0 @@
|
||||||
export type LoginCredentials = {
|
|
||||||
email: string;
|
|
||||||
password: string;
|
|
||||||
};
|
|
|
@ -1,4 +0,0 @@
|
||||||
export type Tokens = {
|
|
||||||
access_token: string;
|
|
||||||
refresh_token: string;
|
|
||||||
};
|
|
Loading…
Reference in New Issue