2 |
3 |
{{ "AUTH.TITLE" | translate }}
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/app/auth/auth.component.scss:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vallettasoftware/boilerplate-angular/d5f43137184b18b93cd0f8bd73f264eacf76b96c/src/app/auth/auth.component.scss
--------------------------------------------------------------------------------
/src/app/auth/auth.component.ts:
--------------------------------------------------------------------------------
1 | import { Component, OnInit } from '@angular/core';
2 |
3 | @Component({
4 | selector: 'blr-auth',
5 | templateUrl: './auth.component.html',
6 | styleUrl: './auth.component.scss',
7 | })
8 | export class AuthComponent implements OnInit {
9 | constructor() {}
10 |
11 | ngOnInit(): void {}
12 | }
13 |
--------------------------------------------------------------------------------
/src/app/auth/auth.guard.ts:
--------------------------------------------------------------------------------
1 | import {
2 | ActivatedRouteSnapshot,
3 | CanActivateFn,
4 | Router,
5 | RouterStateSnapshot,
6 | } from '@angular/router';
7 | import { first, map } from 'rxjs/operators';
8 |
9 | import { inject } from '@angular/core';
10 | import { AuthService } from './auth.service';
11 | import { BrowserStorageService } from '../helpers/browser-storage.service';
12 | import { PATH } from '../models/routes.model';
13 |
14 | export const nonAuthorizedGuard: CanActivateFn = (
15 | next: ActivatedRouteSnapshot,
16 | state: RouterStateSnapshot
17 | ) => {
18 | const isResetPasswordPage = state.url.startsWith(
19 | `/${PATH.root.auth}/${PATH.auth.resetPassword}`
20 | );
21 | if (isResetPasswordPage) {
22 | inject(BrowserStorageService).clearLocalStorage();
23 | }
24 |
25 | return inject(AuthService).isAuthorized$.pipe(
26 | map((hasAuth) => (hasAuth ? inject(Router).createUrlTree(['/']) : true))
27 | );
28 | };
29 |
30 | export const authorizedGuard: CanActivateFn = (
31 | next: ActivatedRouteSnapshot,
32 | state: RouterStateSnapshot
33 | ) => {
34 | const router = inject(Router);
35 | return inject(AuthService).isAuthorized$.pipe(
36 | first(),
37 | map((hasAuth) => {
38 | console.debug(hasAuth);
39 | return !hasAuth
40 | ? router.createUrlTree(['/', PATH.root.auth, PATH.auth.resetPassword])
41 | : true;
42 | })
43 | );
44 | };
45 |
--------------------------------------------------------------------------------
/src/app/auth/auth.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Injectable } from '@angular/core';
2 | import {
3 | HttpEvent,
4 | HttpInterceptor,
5 | HttpHandler,
6 | HttpRequest,
7 | } from '@angular/common/http';
8 | import { Observable } from 'rxjs';
9 | import { Store } from '@ngxs/store';
10 | import { AuthState } from './auth.state';
11 | import { environment } from '../../environments/environment';
12 |
13 | @Injectable()
14 | export class AuthInterceptor implements HttpInterceptor {
15 | constructor(private store: Store) {}
16 |
17 | intercept(
18 | req: HttpRequest