[Angular2Fire] Firebase auth (Google, Github)

來源:互聯網
上載者:User

標籤:sub   boolean   click   sim   sign   provider   port   htm   simple   

To do 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, 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()export class AuthService {  private authState: FirebaseAuthState = null;  constructor(public auth$: FirebaseAuth) {    auth$.subscribe((state: FirebaseAuthState) => {      this.authState = state;    });  }  get authenticated(): boolean {    return this.authState !== null;  }  get id(): 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: 

<section class="signup">  <button md-button (click)="signInWithGoogle()">Google</button>  <button md-button (click)="signInWithTwitter()">Twitter</button>  <button md-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‘]})export class SignupComponent implements OnInit {  constructor(private auth: AuthService, private router: 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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.