[Angular2fire] Firebase Auth (Google, Github)

Source: Internet
Author: User

Tags: Sub boolean Click Sim Sign provider Port htm simple

To does Auth, first you need to go firebase.console.com to enable the Auth methods, for example, enable Google, GitHub ...

Enable Goolge is quite simple, just one click, enable Github, Twitter, and you need-to-do more configuration.

Follow the Link:https://firebase.google.com/docs/auth/web/github-auth

After successfully enable it, we create a service to do the AUTH:

Import {authproviders, firebaseauthstate, Firebaseauth} from "Angularfire2"; import {injectable} from "@angular/core"; @Injectable () ExportclassAuthservice {PrivateAuthstate:firebaseauthstate =NULL; Constructor ( Publicauth$: Firebaseauth) {Auth$.subscribe (state:firebaseauthstate)= {       This. authstate =State ;  }); }  Getauthenticated (): boolean {return  This. authstate!==NULL; }  GetID ():string {    return  This. authenticated? This. Authstate.uid:"'; } signIn (Provider:number): Firebase. Promise<FirebaseAuthState> {    return  This. Auth$.login ({provider}).Catch(Error = Console.log ('ERROR @ authservice#signin ():', error)); } signinwithgithub (): Firebase. Promise<FirebaseAuthState> {    return  This. SignIn (Authproviders.github)} signinwithgoogle (): Firebase. Promise<FirebaseAuthState> {    return  This. SignIn (Authproviders.google); } signinwithtwitter (): Firebase. Promise<FirebaseAuthState> {    return  This. SignIn (Authproviders.twitter); } signout ():void {     This. Auth$.logout (); }}

Using it in controller:

< Sectionclass= "Signup">  <ButtonMd-button (click)= "Signinwithgoogle ()">Google</Button>  <ButtonMd-button (click)= "Signinwithtwitter ()">Twitter</Button>  <ButtonMd-button (click)= "Signinwithgithub ()">Github</Button></ Section>
Import {Component, OnInit} from '@angular/core'; import {Authservice} from ".. /shared"; import {Router} from "@angular/router"; @Component ({selector:'App-signup', Templateurl:'./signup.component.html', Styleurls: ['./signup.component.css']}) exportclassSignupcomponent implements OnInit {constructor (PrivateAuth:authservice,Privaterouter:router) {} ngoninit () {} signinwithgithub () { This. Auth.signinwithgithub (). Then ( This. Postsignin.bind ( This) } signinwithtwitter () { This. Auth.signinwithtwitter (). Then ( This. Postsignin.bind ( This) } signinwithgoogle () { This. Auth.signinwithgoogle (). Then ( This. Postsignin.bind ( This) } postsignin () {Console.log ("Auth ID:", This. auth.id);  This. router.navigate ([' /Home']); }}

Happy auth!

[Angular2fire] Firebase auth (Google, Github)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: