[1f4846d] | 1 | import { Injectable, OnDestroy } from '@angular/core';
|
---|
| 2 | import { Router } from '@angular/router';
|
---|
| 3 | import { HttpClient } from '@angular/common/http';
|
---|
| 4 | import { BehaviorSubject, Observable, of, Subscription } from 'rxjs';
|
---|
| 5 | import { map, tap, delay, finalize } from 'rxjs/operators';
|
---|
| 6 | import { IPharmacyHead } from '../interfaces';
|
---|
| 7 | import { environment } from '../../../environments/environment';
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | interface LoginResult {
|
---|
[1db5673] | 11 | userName: string;
|
---|
[1f4846d] | 12 | role: string;
|
---|
[d23bf72] | 13 | originalUserName?: string;
|
---|
[1f4846d] | 14 | accessToken: string;
|
---|
| 15 | refreshToken: string;
|
---|
[d23bf72] | 16 | head?: IPharmacyHead;
|
---|
[1f4846d] | 17 | }
|
---|
| 18 |
|
---|
| 19 | @Injectable({
|
---|
| 20 | providedIn: 'root',
|
---|
| 21 | })
|
---|
| 22 | export class AuthService implements OnDestroy {
|
---|
| 23 | private readonly apiUrl = `${environment.baseApiUrl}api/pharmacyhead`;
|
---|
| 24 | private timer: Subscription;
|
---|
| 25 | private _user = new BehaviorSubject<IPharmacyHead>(null);
|
---|
| 26 | user$: Observable<IPharmacyHead> = this._user.asObservable();
|
---|
| 27 |
|
---|
| 28 | private storageEventListener(event: StorageEvent) {
|
---|
| 29 | if (event.storageArea === localStorage) {
|
---|
| 30 | if (event.key === 'logout-event') {
|
---|
| 31 | this.stopTokenTimer();
|
---|
| 32 | this._user.next(null);
|
---|
| 33 | }
|
---|
| 34 | if (event.key === 'login-event') {
|
---|
| 35 | this.stopTokenTimer();
|
---|
| 36 | this.http.get<LoginResult>(`${this.apiUrl}/user`).subscribe((x) => {
|
---|
| 37 | this._user.next({
|
---|
[1db5673] | 38 | Email: x.userName,
|
---|
[1f4846d] | 39 | Passwd: x.head.Passwd,
|
---|
[ad60966] | 40 | Role: x.role,
|
---|
| 41 | originalUserName: x.originalUserName,
|
---|
[d23bf72] | 42 | Name: x.head.Name,
|
---|
| 43 | PharmacyMedicines: x.head.PharmacyMedicines,
|
---|
| 44 | Pharmacy: x.head.Pharmacy
|
---|
[1f4846d] | 45 | });
|
---|
| 46 | });
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | constructor(private router: Router, private http: HttpClient) {
|
---|
| 52 | window.addEventListener('storage', this.storageEventListener.bind(this));
|
---|
| 53 | console.log(this.apiUrl);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | ngOnDestroy(): void {
|
---|
| 57 | window.removeEventListener('storage', this.storageEventListener.bind(this));
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | login(username: string, password: string) {
|
---|
| 61 | return this.http
|
---|
| 62 | .post<LoginResult>(`${this.apiUrl}/login`, { username, password })
|
---|
| 63 | .pipe(
|
---|
| 64 | map((x) => {
|
---|
| 65 | this._user.next({
|
---|
[1db5673] | 66 | Email: x.userName,
|
---|
[1f4846d] | 67 | Passwd: x.head.Passwd,
|
---|
[ad60966] | 68 | Role: x.role,
|
---|
| 69 | originalUserName: x.originalUserName,
|
---|
[d23bf72] | 70 | Name: x.head.Name,
|
---|
| 71 | PharmacyMedicines: x.head.PharmacyMedicines,
|
---|
| 72 | Pharmacy: x.head.Pharmacy
|
---|
[1f4846d] | 73 | });
|
---|
| 74 | this.setLocalStorage(x);
|
---|
| 75 | this.startTokenTimer();
|
---|
| 76 | return x;
|
---|
| 77 | })
|
---|
| 78 | );
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | logout() {
|
---|
| 82 | this.http
|
---|
| 83 | .post<unknown>(`${this.apiUrl}/logout`, {})
|
---|
| 84 | .pipe(
|
---|
| 85 | finalize(() => {
|
---|
| 86 | this.clearLocalStorage();
|
---|
| 87 | this._user.next(null);
|
---|
| 88 | this.stopTokenTimer();
|
---|
| 89 | this.router.navigate(['login']);
|
---|
| 90 | })
|
---|
| 91 | )
|
---|
| 92 | .subscribe();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | refreshToken() {
|
---|
| 96 | const refreshToken = localStorage.getItem('refresh_token');
|
---|
| 97 | if (!refreshToken) {
|
---|
| 98 | this.clearLocalStorage();
|
---|
| 99 | return of(null);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return this.http
|
---|
| 103 | .post<LoginResult>(`${this.apiUrl}/refresh-token`, { refreshToken })
|
---|
| 104 | .pipe(
|
---|
| 105 | map((x) => {
|
---|
| 106 | this._user.next({
|
---|
[1db5673] | 107 | Email: x.userName,
|
---|
[1f4846d] | 108 | Passwd: x.head.Passwd,
|
---|
[ad60966] | 109 | Role: x.role,
|
---|
| 110 | originalUserName: x.originalUserName,
|
---|
[d23bf72] | 111 | Name: x.head.Name,
|
---|
| 112 | PharmacyMedicines: x.head.PharmacyMedicines,
|
---|
| 113 | Pharmacy: x.head.Pharmacy
|
---|
[1f4846d] | 114 | });
|
---|
| 115 | this.setLocalStorage(x);
|
---|
| 116 | this.startTokenTimer();
|
---|
| 117 | return x;
|
---|
| 118 | })
|
---|
| 119 | );
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | setLocalStorage(x: LoginResult) {
|
---|
| 123 | localStorage.setItem('access_token', x.accessToken);
|
---|
| 124 | localStorage.setItem('refresh_token', x.refreshToken);
|
---|
| 125 | localStorage.setItem('login-event', 'login' + Math.random());
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | clearLocalStorage() {
|
---|
| 129 | localStorage.removeItem('access_token');
|
---|
| 130 | localStorage.removeItem('refresh_token');
|
---|
| 131 | localStorage.setItem('logout-event', 'logout' + Math.random());
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | getUser() {
|
---|
| 135 | return this.user$;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private getTokenRemainingTime() {
|
---|
| 139 | const accessToken = localStorage.getItem('access_token');
|
---|
| 140 | if (!accessToken) {
|
---|
| 141 | return 0;
|
---|
| 142 | }
|
---|
| 143 | const jwtToken = JSON.parse(atob(accessToken.split('.')[1]));
|
---|
| 144 | const expires = new Date(jwtToken.exp * 1000);
|
---|
| 145 | return expires.getTime() - Date.now();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | private startTokenTimer() {
|
---|
| 149 | const timeout = this.getTokenRemainingTime();
|
---|
| 150 | this.timer = of(true)
|
---|
| 151 | .pipe(
|
---|
| 152 | delay(timeout),
|
---|
| 153 | tap(() => this.refreshToken().subscribe())
|
---|
| 154 | )
|
---|
| 155 | .subscribe();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | private stopTokenTimer() {
|
---|
| 159 | this.timer?.unsubscribe();
|
---|
| 160 | }
|
---|
| 161 | } |
---|