Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { UpdateUserRequest } from '../../models/update-user-request.model';
- import { UserRepresentation } from '../../models/user-representation.model';
- import { Injectable } from '@angular/core';
- import { User } from '../../models/user.model';
- import { LocalStorage } from 'ngx-webstorage-fix';
- import { HttpClient, HttpHeaders } from '@angular/common/http';
- import 'rxjs/add/operator/map';
- import 'rxjs/add/operator/toPromise';
- import * as _ from 'lodash';
- import { AuthResponse } from './auth-response';
- @Injectable()
- export class AuthService {
- @LocalStorage() private _user: User;
- private set user(value: User) {
- this._user = value;
- this.fillAuthFlags();
- }
- private get user() {
- return this._user;
- }
- public isModerator;
- public isInvestor;
- public isInitiator;
- public isGISP;
- constructor(private http: HttpClient) {
- this.fillAuthFlags();
- }
- private fillAuthFlags() {
- if (!this._user) {
- this.isModerator = false;
- this.isInvestor = false;
- this.isInitiator = false;
- this.isGISP = false;
- return;
- }
- this.isModerator = this._user.groupTypes.includes('MODERATORS');
- this.isInvestor = this._user.groupTypes.includes('INVESTORS');
- this.isInitiator = this._user.groupTypes.includes('COMPANY_USERS');
- this.isGISP = this._user.groupTypes.includes('GISP_USERS');
- }
- public getUser(): Promise<User> {
- return Promise.resolve(this.user);
- }
- userHasSubscription(): boolean {
- return this._user.hasSubscription || this.isInitiator || this.isModerator;
- }
- public loadCurrentUser(): Promise<User> {
- return this.http
- .post<AuthResponse>('auth/status', {})
- .toPromise()
- .then((user: AuthResponse) => {
- this.user = {
- authenticated: user.authenticated,
- groupTypes: user.groupTypes,
- hasSubscription: user.accessType === 'EXTENDED',
- name: user.name,
- organizationId: user.organizationId,
- userId: user.userId,
- externalAuth: user.externalAuth
- };
- return user;
- })
- .catch(() => {
- this._user = null;
- return null;
- });
- }
- public login(usernameOrEmail: string, password: string, rememberMe: boolean) {
- const body = {
- usernameOrEmail,
- password,
- rememberMe
- };
- const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
- const options = { headers: headers };
- return this.http
- .post<AuthResponse>('auth/login', body, options)
- .toPromise()
- .then((data: AuthResponse) => {
- if (data['authenticated']) {
- this.user = {
- authenticated: data['authenticated'],
- name: data['name'],
- userId: +data['userId'],
- organizationId: +data['organizationId'],
- groupTypes: data['groupTypes'],
- hasSubscription: data.accessType === 'EXTENDED',
- externalAuth: data.externalAuth
- };
- return Promise.resolve(this.user);
- }
- return Promise.reject('Unauthenticated');
- });
- }
- public loggedIn(): Promise<boolean> {
- return this.getUser().then((user: User) => user != null);
- }
- public logOut(): Promise<any> | void {
- if (this.user.externalAuth) {
- location.href = `auth/logout`;
- } else {
- return this.http
- .post(`auth/logout`, {})
- .toPromise()
- .then(() => {
- this.user = null;
- });
- }
- }
- public getCurrentUserInformation(): Promise<UserRepresentation> {
- return this.http.get<UserRepresentation>('auth/user').toPromise();
- }
- public updateUserInformation(userInfo: UserRepresentation, userRequest: UpdateUserRequest): Promise<any> {
- const lengthPassword = _.get(userRequest, ['password', 'length']);
- if (!userInfo.isAbleToChangePassword && _.isNumber(lengthPassword) && lengthPassword > 0) {
- return Promise.reject('Нет прав на смену пароля');
- }
- return this.http.post('auth/user', userRequest).toPromise();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement