Skip to content

Instantly share code, notes, and snippets.

@camkidman
Last active January 7, 2016 04:04
Show Gist options
  • Save camkidman/cbdfa968e130b68a1dff to your computer and use it in GitHub Desktop.
Save camkidman/cbdfa968e130b68a1dff to your computer and use it in GitHub Desktop.
import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';
import {AuthHttp} from 'angular2-jwt';
import {Config, SATELLIZER_PROVIDERS, Auth} from 'ng2-ui-auth';
import {AppComponent} from './app.component';
bootstrap(AppComponent, [
HTTP_PROVIDERS,
SATELLIZER_PROVIDERS({providers: {google: {clientId: GOOGLE_CLIENT_ID}}}),
provide(AuthHttp, {
useFactory: (auth: Auth, config: Config) => {
return new AuthHttp({
tokenName: config.tokenName,
tokenGetter: () => auth.getToken(),
});
},
deps: [Auth, Config],
}),
]);
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: '../templates/login_form.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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment