UNPKG

@koalarx/ui

Version:

Koala UI is a modern and accessible component library designed to speed up interface development in Angular projects. With simple integration and clear documentation, you can easily build robust and visually appealing applications.

162 lines (158 loc) 6.1 kB
import { HttpClient } from '@angular/common/http'; import * as i0 from '@angular/core'; import { inject, signal, computed, effect, Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { AppConfig } from '@koalarx/ui/core/config'; import { Confirm } from '@koalarx/ui/shared/components/confirm'; import { jwtDecode } from 'jwt-decode'; import { first } from 'rxjs/internal/operators/first'; import { tap } from 'rxjs/internal/operators/tap'; class Authorization { appConfig = inject(AppConfig); translation = this.appConfig.translation.jwtAuthorizationService; authConfig = this.appConfig.authConfig; _accessToken = signal(null, ...(ngDevMode ? [{ debugName: "_accessToken" }] : [])); _refreshToken = signal(null, ...(ngDevMode ? [{ debugName: "_refreshToken" }] : [])); _isAuthenticated = signal(false, ...(ngDevMode ? [{ debugName: "_isAuthenticated" }] : [])); _userinfo = signal(null, ...(ngDevMode ? [{ debugName: "_userinfo" }] : [])); router = inject(Router); http = inject(HttpClient); confirm = inject(Confirm); get accessToken() { const accessToken = this._accessToken(); return accessToken; } set accessToken(accessToken) { if (accessToken) { localStorage.setItem(this.authConfig.storageTokenKey, accessToken); } else { localStorage.removeItem(this.authConfig.storageTokenKey); } this._accessToken.set(accessToken); } get refreshToken() { const refreshToken = this._refreshToken(); return refreshToken; } set refreshToken(refreshToken) { if (refreshToken) { localStorage.setItem(this.authConfig.storageRefreshTokenKey, refreshToken); } else { localStorage.removeItem(this.authConfig.storageRefreshTokenKey); } this._refreshToken.set(refreshToken); } get isAuthenticated() { return this._isAuthenticated.asReadonly(); } get userinfo() { return this._userinfo.asReadonly(); } get isExpired() { return computed(() => { const accessToken = this._accessToken(); if (accessToken) { const decodedToken = jwtDecode(accessToken); if (decodedToken) { const exp = new Date((decodedToken.exp ?? 0) * 1000); const now = new Date(); return exp < now; } } return true; }); } get hasToken() { return computed(() => !!this._accessToken()); } constructor() { if (this.appConfig.authConfig) { this.init(); } } init() { this._accessToken.set(localStorage.getItem(this.authConfig.storageTokenKey)); this._refreshToken.set(localStorage.getItem(this.authConfig?.storageRefreshTokenKey)); effect(() => { const userInfo = this._userinfo(); if (userInfo) { this.updateAuthState(); } }); effect(() => { const isAuthenticated = this.isAuthenticated(); if (isAuthenticated && (!this.authConfig.loginRoute || (this.authConfig.loginRoute && location.hash.includes(this.authConfig.loginRoute))) && this.authConfig.homeRoute) { this.router.navigate([this.authConfig.homeRoute]); } }); effect(() => { const hasToken = this.hasToken(); if (hasToken) { this.loadUserInfo(); } else if (this.authConfig.loginRoute && !location.hash.includes(this.authConfig.loginRoute)) { this.router.navigate([this.authConfig.loginRoute]); } }); } updateAuthState() { this._isAuthenticated.set(this.hasToken() && !this.isExpired() && !!this._userinfo()); } logoutUser() { localStorage.removeItem(this.authConfig.storageTokenKey); localStorage.removeItem(this.authConfig.storageRefreshTokenKey); this._userinfo.set(null); this._refreshToken.set(null); this._accessToken.set(null); this.updateAuthState(); } loadUserInfo() { this.http.get(this.authConfig.userInfo.url).subscribe({ next: (userInfo) => this._userinfo.set(userInfo), error: () => this.logoutUser(), }); } logout(force = false) { if (force) { this.logoutUser(); return; } this.confirm.open({ message: this.translation.questionLogoutMessage, yesCallback: () => this.logoutUser(), }); } updateToken() { return this.http.post(this.authConfig.refreshToken.url, {}).pipe(tap({ next: (response) => { this.accessToken = response[this.authConfig.refreshToken.response.accessTokenKeyName]; this.refreshToken = response[this.authConfig.refreshToken.response.refreshTokenKeyName]; this.updateAuthState(); }, error: () => { this.logout(true); this.router.navigate([this.authConfig.loginRoute]); }, }), first()); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: Authorization, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: Authorization, providedIn: 'root' }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: Authorization, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [] }); /** * Generated bundle index. Do not edit. */ export { Authorization }; //# sourceMappingURL=koalarx-ui-shared-services.mjs.map