[Angular2fire] Firebase Auth (Google, Github)

Source: Internet
Author: User
Tags export class

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";

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>
import {Component, OnInit} from ‘@angular/core‘;
import {AuthService} from "../shared";
import {Router} from "@angular/router";

  selector: ‘app-signup‘,
  templateUrl: ‘./signup.component.html‘,
  styleUrls: [‘./signup.component.css‘]
export class SignupComponent implements OnInit {

  constructor(private auth: AuthService, private router: Router) {


  ngOnInit() {




  postSignIn() {
    console.log("Auth id: ", this.auth.id);


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.

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.