UNPKG

@minimaltech/ra-infra

Version:

Minimal Technology ReactJS Infrastructure

137 lines 5.77 kB
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; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; import { CoreBindings, RequestMethods, } from '../../common'; import { DefaultAuthService } from '../../base/services'; import { inject } from '@loopback/context'; import { BaseProvider } from './base.provider'; let DefaultAuthProvider = class DefaultAuthProvider extends BaseProvider { restDataProvider; authProviderOptions; authService; constructor(restDataProvider, authProviderOptions, authService) { super(); this.restDataProvider = restDataProvider; this.authProviderOptions = authProviderOptions; this.authService = authService; } // ------------------------------------------------------------- // LOGIN // ------------------------------------------------------------- login(params) { return new Promise((resolve, reject) => { this.restDataProvider .send({ resource: (this.authProviderOptions.paths?.signIn ?? '/auth/login'), params: { method: RequestMethods.POST, body: params }, }) .then(rs => { const { userId, token } = rs.data; this.authService.saveAuth({ token, userId, username: params.username }); resolve(rs); }) .catch(error => { console.log('[LOGIN]', error); reject(error); }); }); } // ------------------------------------------------------------- // LOGOUT // ------------------------------------------------------------- logout(_params) { return new Promise(resolve => { this.authService.cleanUp(); resolve(); }); } // ------------------------------------------------------------- // CHECK_AUTH // ------------------------------------------------------------- async checkAuth(_params) { const token = this.authService.getAuth(); if (!token?.value) { return Promise.reject({ redirectTo: 'login' }); } if (!this.authProviderOptions.paths?.checkAuth) { return Promise.resolve(); } const rs = await this.restDataProvider.send({ resource: this.authProviderOptions.paths.checkAuth, params: { method: RequestMethods.GET }, }); if (!rs?.data) { return Promise.reject({ redirectTo: 'login' }); } return await Promise.resolve(); } // ------------------------------------------------------------- // CHECK_ERROR // ------------------------------------------------------------- checkError(params) { const { status } = params; if (status === 401) { this.authService.cleanUp(); return Promise.reject({ redirectTo: 'login' }); } if (status === 403) { return Promise.reject({ redirectTo: `/unauthorized`, logoutUser: false, }); } return Promise.resolve(); } // ------------------------------------------------------------- // GET_IDENTIFIER // ------------------------------------------------------------- getIdentity(_params) { const user = this.authService.getUser(); if (!user?.userId) { return Promise.reject({ message: '[getIdentity] No userId to get user identity!' }); } return Promise.resolve(user); } // ------------------------------------------------------------- // GET_PERMISSIONS // ------------------------------------------------------------- getPermissions(_params) { return Promise.resolve(); } //------------------------------------------------------------- // GET_ROLES //------------------------------------------------------------- getRoles(_params) { return Promise.resolve(this.authService.getRoles()); } // ------------------------------------------------------------- value() { return { login: (params) => this.login(params), logout: (params) => this.logout(params), checkError: (params) => this.checkError(params), checkAuth: (params) => this.checkAuth(params), getIdentity: (params) => this.getIdentity(params), getPermissions: (params) => this.getPermissions(params), getRoles: (params) => this.getRoles(params), }; } }; DefaultAuthProvider = __decorate([ __param(0, inject(CoreBindings.DEFAULT_REST_DATA_PROVIDER)), __param(1, inject(CoreBindings.AUTH_PROVIDER_OPTIONS)), __param(2, inject(CoreBindings.DEFAULT_AUTH_SERVICE)), __metadata("design:paramtypes", [Object, Object, DefaultAuthService]) ], DefaultAuthProvider); export { DefaultAuthProvider }; //# sourceMappingURL=default-auth.provider.js.map