npm package: @kimaya/ngx-auth-service
Description: This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.1.0.
Latest version: 1.0.10

# AuthHandler

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.1.0.

## How Install
    npm install @kimaya/ngx-auth-service

#### Dependency 
<code>*You need to install dependencies manually*</code> 

[CookieStorage](https://github.com/gns088/storage-service)
this is being used to store all cookies in this library.

## Plug into your application
Note**: Please provide <code>AuthService</code> into your core module.

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { AppComponent } from './app.component';
    import { AuthService } from 'ngx-auth-service';
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule
      ],
      providers: [ AuthService ],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
 
 ## How to use
 Inject into your <code>Component</code> or <code>Service</code>.
        
        constructor(private oAuthService: AuthService) {
        }

 ## AuthService Configure
 User can configure some of keys for auth service as per project requirements.
   
 User need to set <code>AuthServiceConstants</code> keys for usages.
 
 *Need to set <code>serverUrl</code> and <code>endPoint</code>. This will be used to hit login api using httpService*
 
 <code>ServerUrl is host </code><br>
 <code>endPoint is api end point</code>
 
 | Keys | Required | Default Value| Usage|
 |-------------|-------------|-----|-----|
 | headerToken | true |Basic  | used to send in header for login  |
 | accessTokenKey | true |accToken | used to store access token in cookie |
 | refreshTokenKey | true |refToken | used to store refresh token in cookie |   
 | expiresInKey | true |exprIn | used to store expire time in cookie |   
 | loggedInKey | true |loggedIn | used to check whether user is loggeIn or not from cookie |   
 | redirectPath | true |/ | used for redirecting after system logut | 
 | serverUrl | true | null | used for http request host |   
 | login | true | null | used for http request for login and refresh token api end point |
 | logout | true | null | used for http request for logout api end point |
 | headers | false | {} | Provide extra headers for all Http request including multipart request |

## Object for login
        export class OAuthRequestProto {
          username: string;
          password: string;
          scope: string = 'write';
          grant_type: string = 'password';
          realm: string;
          authType: string;
          refresh_token: string;
        }

## Methods

#### getAccessToken(): string;
        const accessToken: string = oAuthService.getAccessToken();
will return access token which is stored using cookie service in cookie.
<hr>
        
#### getRefreshToken(): string;
        const refreshToken: string = oAuthService.getRefreshToken();
will return refresh token which is stored using cookie service in cookie.        
<hr>

        
#### setUserLoggedIn(): void;
        oAuthService.setUserLoggedIn();
It will store cookie for loggedIn using `loggedInKey` from configure constants.     
<hr>
        
#### isLoggedIn(): boolean;
        const isLoggedIn: boolean = oAuthService.isLoggedIn();
check whether user is loggedIn or not.        
<hr>
        
#### logoutSystem(): Observable<Object>;

        oAuthService.logoutSystem().subscribe((response) => {
        // to remove all cookie and redirect to page [default '/']
        oAuthService.removeUsersDetailsAndRedirect();
        });
        
logout user from system via api calling and after success remove all cookie value from cookie and redirect to configured page.        
<hr>
  
#### login(object: OAuthRequestProto): Observable<any>;
        oAuthService.login(object).subscribe((response) => {
            oAuthService.storeAccessTokenResponse(
            response.accessToken,
            response.expiresIn, 
            response.refreshToken
            );
            oAuthService.setUserLoggedIn();
        });
to login into system.      
<hr>

#### refreshToken(): Observable<any>;
        oAuthService.refreshToken().subscribe((response) => {
        oAuthService.storeAccessTokenResponse(
                response.accessToken,
                response.expiresIn, 
                response.refreshToken
                );
        oAuthService.setUserLoggedIn();
        });
to refresh token when access token get expired.      
<hr>

#### storeAccessTokenResponse(accessToken: string, expiresIn: string, refreshToken: string): void;
        oAuthService.storeAccessTokenResponse(
                        response.accessToken,
                        response.expiresIn, 
                        response.refreshToken
                        );
to store token and expiry details for session.      
<hr>

## For more Information 
you can contact on <code>kumarganesh088@gmail.com</code>