[Angular2Fire] Firebase auth (Google, Github)
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)的更多相关文章
- [AngularFire2] Auth with Firebase auth -- email
First, you need to enable the email auth in Firebase console. Then implement the auth service: login ...
- istio的安全(概念)
Istio 安全(概念) 目录 Istio 安全(概念) 高层架构 Istio身份 身份和证书管理 认证 Mutial TLS认证 宽容(Permissive)模式 安全命名 认证架构 认证策略 策略 ...
- GitHub:Google
ylbtech-GitHub:Google 1.返回顶部 · horenso 探しやすいコードで漢字直接入力 Perl Apache-2.0 94710Updated on 19 Apr · ...
- [Firebase + PWA] Keynote: Progressive Web Apps on Firebase
Link : Video. 1. Firebase Auth: provides simple login with Github, Google, Facebook, Twittr. Link 2. ...
- 用 Flutter 和 Firebase 轻松构建 Web 应用
作者 / Very Good Ventures Team 我们 (Very Good Ventures 团队) 与 Google 合作,在今年的 Google I/O 大会上推出了 照相亭互动体验 ( ...
- Expo大作战(十六)--expo结合firebase 一个nosql数据库(本章令我惊讶但又失望!)
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- GitHub没有实时通知怎么办?当然是自己上手写一个啊!
相信各位程序员对github已经不陌生了.不知道各位有没有注意到GitHub没有推送通知这个功能.当有人在我的存储库中创建了一个提取请求/问题时,我可以收到电子邮件通知,但当有人stars/forks ...
- Google Material Design的图标字体使用教程
使用教程 1. 打开Material icons下载页 2. 选择要下载的图标 (目前不能多选>_<) 3.选择要下载的格式即可 图标字体使用教程 [方法一] STEP 1: 引入字体文件 ...
- android 很多牛叉布局github地址(转)
原文地址 http://blog.csdn.net/luo15309823081/article/details/41449929 点击可到达github-------https://github.c ...
随机推荐
- deep-in-es6(七)
Symbols对象 JavaScript的第七种原始类型 以前的数据类型: Undefined 未定义 Null 空值 Boolean 布尔类型 Number 数字类型 String 字符串类型 Ob ...
- NOI2017整数
NOI2017 整数 题意: 让你实现两个操作: 1 \(a\) \(b\):将\(x\)加上整数\(a \cdot 2 ^ b\),其中 \(a\)为一个整数,\(b\)为一个非负整数 2 \( ...
- LuoguP2764 最小路径覆盖问题(最大流)
题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开 ...
- mac下的词典翻译快捷键
mac下的词典翻译快捷键:cmd+ctl+d;很方便
- actionBarTab-actionBarTab自定义 布局没法改变其中字体相对中间的位置
我们经常遇到对actionBarTab 进行操作的情况.现在记录修改它的样式的方法,已经如何自定义tab的显示布局 1.在你的theme主题中添加<item name="android ...
- 可变参数的实现my_sprintf
#include "stdafx.h" #include <stdio.h> #include <stdarg.h> void my_sprintf(cha ...
- 用Zebra打造Linux下小型路由器
用Zebra打造Linux下小型路由器 现在的Internet网络相当庞大,不可能在不同的网络之间建立直接的连接,所以这时就必须用路由器为不同网络之间的通信提供路径选择.Linux下搭建路由器价格非常 ...
- BZOJ5020: [THUWC 2017]在美妙的数学王国中畅游(LCT,泰勒展开,二项式定理)
Description 数字和数学规律主宰着这个世界. 机器的运转, 生命的消长, 宇宙的进程, 这些神秘而又美妙的过程无不可以用数学的语言展现出来. 这印证了一句古老的名言: ...
- vue踩坑记- Cannot find module 'wrappy'
找不到模块"包装" 当你维护别人的项目代码的时候,在自己这里电脑上运行,打开一个项目cnpm run dev的时候,报错如下 Cannot find module 'wrappy' ...
- JS数组排序技巧汇总(冒泡、sort、快速、希尔等排序)
本文实例总结了JS数组排序技巧.分享给大家供大家参考,具体如下: 1.冒泡排序 var temp = 0; for (var i = 0; i < array.length; i++) { fo ...