npm.io
1.2.6 • Published 8 years ago

onestop-auth

Licence
MIT
Version
1.2.6
Deps
2
Vulns
0
Weekly
0

Onestop Auth Module

AuthModule is a set of essential modules for Onestop Angular application.

What's included:

  • Login and Spaces Select Page Templete
  • Authentication Service
  • Spaces Service to store and retrieve spaces/spaceId/user roles in localstorage
  • Onestop http interceptor to manage http calls, eg. setup common headers and handle errors

Installation

Before install this module, make sure your npm are under onstop npm registry. To check your npm registry, run

npm config get registry

To change your npm registry to Onestop npm, run

npm config set registry http://packages.prod.1-stop.biz/npm/npm/

To install this module, under the project directory run

npm install --save onestop-auth

Getting started

Import the AuthModule in your root application module:

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';

import {AppComponent} from './app.component';
import {AuthInterceptor, AuthModule} from 'onestop-auth';
import {AppRoutingModule} from './app-routing.module';
import {HomeComponent} from './home.component';
import {HTTP_INTERCEPTORS} from '@angular/common/http';

@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        AppRoutingModule,
        // authenticationUrl and appPrefix are required
        AuthModule.forRoot({
            authenticationUrl: "https://api-40.1-stop.biz/CPMSGW/api/v1/user/login",
            authorisationBaseUrl: "https://api-40.1-stop.biz/CPMSGW/api/v1",
            appPrefix: "CPMS_"
        })
    ],
    declarations: [AppComponent, HomeComponent],
    providers: [{
        provide: HTTP_INTERCEPTORS,
        useClass: AuthInterceptor,
        multi: true,
    }],
    bootstrap: [AppComponent]
})
export class AppModule {
}

More configuration details see Configuration Options

Setting routes:

import {RouterModule, Routes} from '@angular/router';
import {NgModule} from '@angular/core';
import {HomeComponent} from './home.component';
import {LoginComponent, SpacesComponent} from 'onestop-auth';

const routes: Routes = [
    {
        path: '',
        component: HomeComponent
    },
    {
        path: 'login',
        component: LoginComponent,
    },
    {
        path: 'spaces',
        component: SpacesComponent,
    },
    { path: '', redirectTo: 'login', pathMatch: 'full'},
    { path: '**', redirectTo: ''},
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule {
}

Configuration

Name Default Description
*authenticationUrl null The Authentication URL to post user name and password, eg. https://api-40.1-stop.biz/CPMSGW/api/v1/user/login
*appPrefix null Onestop App prefix, eg. CPMS_
authorisationBaseUrl null The base url in relation to user space url and user roles url
redirectTo / The Url that will be redirected to when successfully login
clientId '1STOP' ClientId in Authentication payload
spaceId null Provide a default Space ID
defaultSpaceId true If default space ID is not provided, and you don't want to go to space selection page to select a ID, you can set this config to true, then the app will skip spaces page and set up first space in user's available spaces.
spaceUrl spaces Space selection page URL
userSpacesUrl /user/spaceIds URL to get user spaces, will combine with authorisationBaseUrl
userRolesUrl /user/facilities URL to get user roles, will combine with authorisationBaseUrl