### File

`projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts`

### Index

###### Properties

- Public options
- Public optionsDefault
- Public tokenStorage

###### Methods

- Public getAccessToken
- init
- Public isAuthorized
- Public login
- Public logout
- Public refreshShouldHappen
- Public refreshToken
- Private saveAccessData
- Public verifyTokenRequest

### Constructor

`constructor(http: HttpClient, tokenStorage: TokenStorage)`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:21

Parameters :

Name | Type | Optional
http | `HttpClient` | no
tokenStorage | `TokenStorage` | no

### Methods

Public getAccessToken
`getAccessToken()`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:51
Get access token Returns : `Observable<string>`

init
`init(options: )`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:28

Parameters :

Name | Optional | Default value
options | no | `this.optionsDefault`

Returns : `void`

Public isAuthorized
`isAuthorized()`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:39
Check, if user already authorized. Returns : `Observable<boolean>`

Public login
`login()`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:103
EXTRA AUTH METHODS Returns : `Observable<any>`

Public logout
`logout()`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:112
Logout Returns : `void`

Public refreshShouldHappen
`refreshShouldHappen(response: HttpErrorResponse)`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:85

Function, checks response of failed request to determine, whether token be refreshed or not.

Parameters :

Name | Type | Optional
response | `HttpErrorResponse` | no

Returns : `boolean`

Public refreshToken
`refreshToken()`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:62
Function, that should perform refresh token verifyTokenRequest Returns : `any`

Private saveAccessData
`saveAccessData(undefined: AccessData)`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:123

Save access data in the storage

Parameters :

Name | Type | Optional
`AccessData` | no

Returns : `void`

Public verifyTokenRequest
`verifyTokenRequest(url: string)`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:95

Verify that outgoing request is refresh-token, so interceptor won't intercept this request

Parameters :

Name | Type | Optional
url | `string` | no

Returns : `boolean`

### Properties

Public options
`options: `any``
Type : `any`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:21

Public optionsDefault
`optionsDefault: `object``
Type : `object`
Default value : `{ loginUrl: 'login', accessTokenId: 'app:accessToken', refreshTokenId: 'app:refreshToken', refreshTokenUrl: '' }`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:15

Public tokenStorage
`tokenStorage: `TokenStorage``
Type : `TokenStorage`
Defined in projects/app-base-library/src/lib/angular/services/authentication/authentication.service.ts:25

    import { Injectable } from '@angular/core';
    import { HttpClient, HttpErrorResponse } from '@angular/common/http';
    import { Observable } from 'rxjs/Observable';
    import { AuthService } from 'ngx-auth';
    import { TokenStorage } from './token-storage.service';

    interface AccessData {
      accessToken: string;
      refreshToken: string;
    }

    @Injectable()
    export class AuthenticationService implements AuthService {

      public optionsDefault = {
          loginUrl: 'login',
          accessTokenId: 'app:accessToken',
          refreshTokenId: 'app:refreshToken',
          refreshTokenUrl: ''
      };
      public options: any;

      constructor(
        private http: HttpClient,
        public tokenStorage: TokenStorage
      ) { }

      init (options = this.optionsDefault) {
        this.options = options;
        this.tokenStorage.init(options);
      }

      /**
       * Check, if user already authorized.
       * @description Should return Observable with true or false values
       * @returns {Observable<boolean>}
       * @memberOf AuthService
       */
      public isAuthorized(): Observable < boolean > {
        return this.tokenStorage
          .getAccessToken()
          .map(token => !!token);
      }

      /**
       * Get access token
       * @description Should return access token in Observable from e.g.
       * localStorage
       * @returns {Observable<string>}
       */
      public getAccessToken(): Observable < string > {
        // return this.tokenStorage.getAccessToken(this.accessTokenId);
        return this.tokenStorage.getAccessToken();
      }

      /**
       * Function, that should perform refresh token verifyTokenRequest
       * @description Should be successfully completed so interceptor
       * can execute pending requests or retry original one
       * @returns {Observable<any>}
       */
      public refreshToken() {
      // public refreshToken(): Observable < AccessData > {
        console.log('refreshToken()');
        return this.tokenStorage
          .getRefreshToken()
          .switchMap((refreshToken: string) => {
            return this.http.post(this.options.refreshTokenUrl, { refreshToken });
          })
          .do(this.saveAccessData.bind(this))
          .catch((err) => {
            this.logout();

            return Observable.throw(err);
          });
      }

      /**
       * Function, checks response of failed request to determine,
       * whether token be refreshed or not.
       * @description Essentialy checks status
       * @param {Response} response
       * @returns {boolean}
       */
      public refreshShouldHappen(response: HttpErrorResponse): boolean {
        return response.status === 401
      }

      /**
       * Verify that outgoing request is refresh-token,
       * so interceptor won't intercept this request
       * @param {string} url
       * @returns {boolean}
       */
      public verifyTokenRequest(url: string): boolean {
        return url.endsWith('/refresh');
      }

      /**
       * EXTRA AUTH METHODS
       */

      public login(): Observable<any> {
        console.log('auth: login',this.options);
        return this.http.post(this.options.loginUrl, { })
        .do((tokens: AccessData) => this.saveAccessData(tokens));
      }

      /**
       * Logout
       */
      public logout(): void {
        this.tokenStorage.clear();
        location.reload(true);
      }

      /**
       * Save access data in the storage
       *
       * @private
       * @param {AccessData} data
       */
      private saveAccessData({ accessToken, refreshToken }: AccessData) {
        this.tokenStorage
          .setAccessToken(accessToken)
          .setRefreshToken(refreshToken);
      }

    }

# results matching ""

# No results matching ""