61 lines
2.4 KiB
JavaScript
61 lines
2.4 KiB
JavaScript
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||
|
|
};
|
||
|
|
import { Injectable, inject } from '@angular/core';
|
||
|
|
import { Router } from '@angular/router';
|
||
|
|
import { finalize } from 'rxjs';
|
||
|
|
import { AuthService } from './auth.service';
|
||
|
|
import { PermissionService } from './permission.service';
|
||
|
|
import { SessionService } from './session.service';
|
||
|
|
/**
|
||
|
|
* Public surface for components/pages. Components should depend on this,
|
||
|
|
* not on AuthService/SessionService/PermissionService directly, so the
|
||
|
|
* orchestration details (which service owns what) can change without
|
||
|
|
* touching UI code.
|
||
|
|
*/
|
||
|
|
let AuthFacade = class AuthFacade {
|
||
|
|
constructor() {
|
||
|
|
this.auth = inject(AuthService);
|
||
|
|
this.session = inject(SessionService);
|
||
|
|
this.permissions = inject(PermissionService);
|
||
|
|
this.router = inject(Router);
|
||
|
|
this.isAuthenticated = this.session.isAuthenticated;
|
||
|
|
this.status = this.session.status;
|
||
|
|
this.role = this.session.role;
|
||
|
|
this.loginPhase = this.auth.loginPhase;
|
||
|
|
this.lastError = this.auth.lastError;
|
||
|
|
}
|
||
|
|
restoreSession() {
|
||
|
|
this.auth.restoreSession();
|
||
|
|
}
|
||
|
|
login(onSuccessRedirectTo) {
|
||
|
|
this.auth.login().subscribe({
|
||
|
|
next: () => {
|
||
|
|
if (onSuccessRedirectTo) {
|
||
|
|
this.router.navigateByUrl(onSuccessRedirectTo);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
error: () => {
|
||
|
|
const code = this.auth.lastError()?.code ?? 'unauthorized';
|
||
|
|
this.router.navigate(['/admin-login/error', code]);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
logout(redirectTo = '/admin-login') {
|
||
|
|
this.auth
|
||
|
|
.logout()
|
||
|
|
.pipe(finalize(() => this.router.navigateByUrl(redirectTo)))
|
||
|
|
.subscribe({ error: () => undefined });
|
||
|
|
}
|
||
|
|
can(permission) {
|
||
|
|
return this.permissions.has(permission);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
AuthFacade = __decorate([
|
||
|
|
Injectable({ providedIn: 'root' })
|
||
|
|
], AuthFacade);
|
||
|
|
export { AuthFacade };
|