0.0.9 • Published 7 years ago

@goodhum/gh-angular-auth v0.0.9

Weekly downloads
10
License
MIT
Repository
github
Last release
7 years ago

gh-angular-auth

gh-angular-auth is a module you can add to your Angular4+ app in order to quickly setup Auth0 and Firebase authentication support.

Install

npm install --save @goodhum/gh-angular-auth

Pre Setup

Firebase

Login to your firebase console and enable the sign-in method for the providers you want to use from Authentication Menu on left side.

Auth0 (Only if you want to get user meta_data)

 1. Login to auth 0.
 2. Click on Rules menu from left.
 3. Create a rule.
 4. Select empty rule.
 5. Give a name 'attach appmetadata'
 6. Update the function with the following settings 
         function (user, context, callback) {
           var namespace = 'https://<YOUR-DOMAIN>/';
           context.idToken[namespace + 'app_metadata'] = user.app_metadata; 
           
           callback(null, user, context);
         }
 7. Save the rule

Configuration

export interface FirebaseConfig {
    apiKey: string;
    authDomain: string;
    databaseURL: string;
    projectId: string;
    storageBucket: string;
    messagingSenderId: string;
}

export interface Auth0Config {
    domain: string;
    client_id: string;
    scope?: string;
    realm?: string;
    grant_type?: string;
}

export abstract class ProvidersConfig {
    firebase?: FirebaseConfig;
    auth0?: Auth0Config;
};

Methods

Auth Service

MethodsNotes
provider(provider: string): ProviderGet the provider you want to use. 'auth0' or 'firebase'.
login(user: User): ObservableLogin with provider.
logout(): voidLogout of the provider.
signUp(): voidSignUp with email and password to the desired provider.
resetPassword(): voidRequest a reset password link to email for desired provider.

User Service

MethodsNotes
getLoggedInProvider(): stringGet providers that have been logged in with.
getUserProfile(): ObjectGet user profile.
getUserProfileFromToken: ObjectGet user profile from jwt token.
getToken(): stringGet user JWT token.
canActivate(): ObservableGet can activate.
isAuthenticated(): booleanGet if user is authenticated.

Initialization

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { GhAuthModule, ProvidersConfig } from '@goodhum/gh-angular-auth';

import { AppComponent } from './app.component';


const config: ProvidersConfig = {
     auth0: {
        YOUR AUTH0 CONFIGURATION 
     },
     firebase: {
        YOUR FIREBASE CONFIGURATION
     }
     redirectUrl: URL to redirect on CanActivate false
};


@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        GhAuthModule.forRoot(config)
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule {}

Examples

import { Component, OnInit } from '@angular/core';
import { AuthService, User, UserService } from '@goodhum/gh-angular-auth';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
    authProvider: any;

    constructor(public auth: AuthService,
                private userservice: UserService) { }


    ngOnInit() {
        this.authProvider = this.auth.provider('firebase');
    }

    login(provider, username, password) {
        const user: User = { username: username, password: password };
        
        this.authProvider.login(user).subscribe(res => {
            console.log(res);
        }, error => console.log('Error Login', error));
    }

    logout() {
        this.authProvider.logout().subscribe(res => console.log(res));
    }
}

License

MIT

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

1.0.0

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

0.0.0

7 years ago