# ng2-ui-auth **Repository Path**: mirrors_esvit/ng2-ui-auth ## Basic Information - **Project Name**: ng2-ui-auth - **Description**: an angular2 repository for authentication based on angular1's satellizer - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-08 - **Last Updated**: 2026-04-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 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: ```typescript import {bootstrap} from 'angular2/platform/browser'; import {provide} from 'angular2/core'; import {HTTP_PROVIDERS} from 'angular2/http'; import {NG2_UI_AUTH_PROVIDERS, JwtHttp} from 'ng2-ui-auth'; import {Main} from './main'; const GOOGLE_CLIENT_ID = '******************************.apps.googleusercontent.com'; bootstrap(Main, [ HTTP_PROVIDERS, NG2_UI_AUTH_PROVIDERS({providers: {google: {clientId: GOOGLE_CLIENT_ID}}}), ]); ``` or if you want to provide your own http implementation (or replace existing http): ```typescript import {Http,RequestOptions,BaseRequestOptions,ResponseOptions,BaseResponseOptions,BrowserXhr,XHRBackend} from 'angular2/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 { 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) ```typescript import {Component, AfterContentInit, ElementRef, Renderer} from 'angular2/core'; import {Router, ROUTER_DIRECTIVES} from 'angular2/router'; import {FormBuilder, ControlGroup, Validators} from 'angular2/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): ```html

Log in


Don't have an account yet? Sign up

``` 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.