Git Product home page Git Product logo

ng2-ui-auth's Introduction

ng2-ui-auth

an angular2 repository for authentication based on angular1's satellizer This is mostly copy-paste from the great satellizer (https://satellizer.herokuapp.com/#/ https://github.com/sahat/satellizer) library. To use this run npm install ng2-ui-auth --save.

for a full client + server-side example: https://github.com/ronzeidman/ng2-ui-auth-example

For configuration do the following:

import {bootstrap} from '@angular/platform-browser-dynamic';
import {provide} from '@angular/core';
import {HTTP_PROVIDERS} from '@angular/http';
import {NG2_UI_AUTH_PROVIDERS, JwtHttp} from 'ng2-ui-auth';
import {Main} from './main';
const DEFAULT_POST_HEADER: {[name: string]: string} = {
  'Content-Type': 'application/json'
};
const GOOGLE_CLIENT_ID = '******************************.apps.googleusercontent.com';

bootstrap(Main, [
    HTTP_PROVIDERS,
    NG2_UI_AUTH_PROVIDERS({defaultHeaders: DEFAULT_POST_HEADER, providers: {google: {clientId: GOOGLE_CLIENT_ID}}}),
]);

or if you want to provide your own http implementation (or replace existing http):

import {Http,RequestOptions,BaseRequestOptions,ResponseOptions,BaseResponseOptions,BrowserXhr,XHRBackend} from '@angular/http';
import {JwtHttp, Config, Shared} from 'ng2-ui-auth';

export class MyHttp extends JwtHttp {
    constructor(backend: ConnectionBackend,
                defaultOptions: RequestOptions,
                shared: Shared,
                config: Config) {
        super(backend, defaultOptions, shared, config);
    }

    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
        options = options || {};
        if (!options.headers) {
            options.headers = new Headers();
        }
        if (!options.headers.has('Content-Type')) {
            options.headers.set('Content-Type', 'application/json');
        }
        return super.request(url, options)
            .catch((err, source, caught) => {
                handleError(err);
                return Observable.empty();
            });
    }
}

export const MY_HTTP_PROVIDERS = [
    provide(
        MyHttp, // If you replace the JwtHttp or the Http itself make sure you are not changing the response type since NG2_UI_AUTH_PROVIDERS is using JwtHttp and expects the default response type
        {
            useFactory:
                (xhrBackend, requestOptions, shared, config, router) =>
                    new MyHttp(xhrBackend, requestOptions, shared, config),
            deps: [XHRBackend, RequestOptions, Shared, Config],
        }),
    DefaultHandlers,
    BrowserXhr,
    provide(RequestOptions, {useClass: BaseRequestOptions}),
    provide(ResponseOptions, {useClass: BaseResponseOptions}),
    XHRBackend,
];

//in the bootstrap file:
bootstrap(Main, [
    NG2_UI_AUTH_PROVIDERS({providers: {google: {clientId: GOOGLE_CLIENT_ID}}}),
    MY_HTTP_PROVIDERS,
]);

For usage look at the satellizer project it's 99% the same (instead of promises it uses Observables)

import {Component, AfterContentInit, ElementRef, Renderer} from '@angular/core';
import {Router, ROUTER_DIRECTIVES} from '@angular/router-deprecated';
import {FormBuilder, ControlGroup, Validators} from '@angular/common';
import {Auth} from 'ng2-ui-auth';
import {NgMessages} from '../formComponents/ngMessages';
import {CustomValidators} from '../formComponents/customValidators';
import {DefaultHandlers} from '../httpDefaults';

/**
 * Created by Ron on 18/12/2015.
 */

@Component({
    selector: 'app-login',
    templateUrl: './src/login/login.html',
    directives: [NgMessages, ROUTER_DIRECTIVES],
})
export class Login implements AfterContentInit {
    form: ControlGroup;
    user = { email: '', password: '' };
    userControlsConfig = {
        email: ['', Validators.compose([Validators.required, CustomValidators.email])],
        password: ['', Validators.required],
    };
    login() {
        this.auth.login(this.user)
            .subscribe(
                () => this.goToMain(),
                this.handlers.error
            );
    }
    authenticate(provider: string) {
        this.auth.authenticate(provider)
            .subscribe(
                () => this.goToMain(),
                this.handlers.error
            );
    }
    goToMain() {
        this.router.navigate(['Main']);
    }
    ngAfterContentInit() {
        this.renderer.setElementClass(this.element, 'app', true);
        if (this.auth.isAuthenticated()) {
            this.goToMain();
        }
    }
    constructor(private auth: Auth,
                private fb: FormBuilder,
                private router: Router,
                private element: ElementRef,
                private renderer: Renderer,
                private handlers: DefaultHandlers) {
        this.form = fb.group(this.userControlsConfig);
    }
}

The corresponding html (also migrated from satellizer):

<div class="row main-container">
    <div class="center-form panel">
        <div class="panel-body">
            <h2 class="text-center">Log in</h2>

            <form (ngSubmit)="login()" [ngFormModel]="form" #f="ngForm">
                <div class="form-group has-feedback" [class.has-error]="f.form.controls.email.dirty && !f.form.controls.email.valid">
                    <input type="text" ngControl="email" class="form-control input-lg" [(ngModel)]="user.email" placeholder="Email" autofocus>
                    <i class="fa form-control-feedback fa-at"></i>
                    <ng-messages [errors]="f.form.controls.email.errors"></ng-messages>
                </div>
                <div class="form-group has-feedback" [class.has-error]="f.form.controls.password.dirty && !f.form.controls.password.valid">
                    <input type="password" ngControl="password" class="form-control input-lg" [(ngModel)]="user.password" placeholder="Password">
                    <i class="fa form-control-feedback fa-key"></i>
                    <ng-messages [errors]="f.form.controls.password.errors"></ng-messages>
                </div>
                <button type="submit" [disabled]="!f.form.valid" class="btn btn-lg btn-block btn-success">Log in</button>
                <br>
                <p class="text-center text-muted" >
                    <small>Don&apos;t have an account yet?</small>
                    <a [routerLink]="['Signup']">Sign up</a>
                </p>
                <div class="signup-or-separator">
                    <h6 class="text">or</h6>
                    <hr>
                </div>
            </form>

            <button class="btn btn-block btn-google-plus" (click)="authenticate('google')">
                <i class="fa fa-google-plus"></i>
                sign in with Google
            </button>
        </div>
    </div>
</div>

If something doesn't work, feel free to issue a pull request and/or create a new issue, I'm not sure I'll be responsive though since I use it internally for my project and will only update and fix the repository if it affects my work. If someone (like let's say @shahat) will want to take control of this repository and maintain it he is more than welcome to do so.

If someone thinks that some of my code can be written better I encourage you to inform me.

ng2-ui-auth's People

Contributors

chad-autry avatar ronzeidman avatar royipressburger avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.