@kimaya-hybrid/ngx-auth-service v1.0.2
AuthHandler
This project was generated with Angular CLI version 8.1.0.
How Install
npm install @kimaya/ngx-auth-service
Dependency
You need to install dependencies manually
CookieStorage this is being used to store all cookies in this library.
Plug into your application
Note**: Please provide AuthService 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 Component or Service.
constructor(private oAuthService: AuthService) {
}
AuthService Configure
User can configure some of keys for auth service as per project requirements.
User need to set AuthServiceConstants keys for usages.
Need to set serverUrl and endPoint. This will be used to hit login api using httpService
ServerUrl is host endPoint is api end point
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.
setUserLoggedIn(): void;
oAuthService.setUserLoggedIn();
It will store cookie for loggedIn using loggedInKey
from configure constants.
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.
refreshToken(): Observable;
oAuthService.refreshToken().subscribe((response) => {
oAuthService.storeAccessTokenResponse(
response.accessToken,
response.expiresIn,
response.refreshToken
);
oAuthService.setUserLoggedIn();
});
to refresh token when access token get expired.
storeAccessTokenResponse(accessToken: string, expiresIn: string, refreshToken: string): void;
oAuthService.storeAccessTokenResponse(
response.accessToken,
response.expiresIn,
response.refreshToken
);
to store token and expiry details for session.
For more Information
you can contact on kumarganesh088@gmail.com