|
1 | 1 | # 5. User authentication
|
2 | 2 |
|
3 |
| -> AngularFire2 allows you configure your authentication methods in the bootstrap |
4 |
| -phase of your application. This means that if your app only uses one form |
5 |
| -of authentication, you can specify that ahead of time so you only need to call |
6 |
| -`af.auth.login()` later, and with no parameters. |
| 3 | +> AngularFire2 provides you an Observable for your Firebase Authentication |
| 4 | +State via the `AngularFireAuth` module. |
7 | 5 |
|
8 |
| -## Configure application in bootstrap |
9 |
| - |
10 |
| -To specify your authentication ahead of time, you provide the `AngularFireModule.initializeApp` function |
11 |
| -with an `AuthProvider` and an `AuthMethod`. |
12 |
| - |
13 |
| -```ts |
14 |
| -import { BrowserModule } from '@angular/platform-browser'; |
15 |
| -import { NgModule } from '@angular/core'; |
16 |
| -import { AppComponent } from './app.component'; |
17 |
| -import { AngularFireModule, AuthProviders, AuthMethods } from 'angularfire2'; |
18 |
| - |
19 |
| -const myFirebaseConfig = { |
20 |
| - apiKey: '<your-key>', |
21 |
| - authDomain: '<your-project-authdomain>', |
22 |
| - databaseURL: '<your-database-URL>', |
23 |
| - storageBucket: '<your-storage-bucket>', |
24 |
| - messagingSenderId: '<your-messaging-sender-id>' |
25 |
| -}; |
26 |
| - |
27 |
| -const myFirebaseAuthConfig = { |
28 |
| - provider: AuthProviders.Google, |
29 |
| - method: AuthMethods.Redirect |
30 |
| -}; |
31 |
| - |
32 |
| -@NgModule({ |
33 |
| - imports: [ |
34 |
| - BrowserModule, |
35 |
| - AngularFireModule.initializeApp(myFirebaseConfig, myFirebaseAuthConfig) |
36 |
| - ], |
37 |
| - declarations: [ AppComponent ], |
38 |
| - bootstrap: [ AppComponent ] |
39 |
| -}) |
40 |
| -export class AppModule {} |
41 |
| -``` |
42 |
| - |
43 |
| - |
44 |
| -## Login users |
45 |
| - |
46 |
| -If you have setup authentication in bootstrap like above, then all you need to do |
47 |
| -is call login on `af.auth.login()` |
48 |
| - |
49 |
| -The lone exception is if you're using username and password, then you'll have |
50 |
| -to call `af.auth.login()` with the user's credentials. |
51 |
| - |
52 |
| -```ts |
53 |
| -af.auth.login({ email: 'email', password: 'pass' }); |
54 |
| -``` |
| 6 | +AngularFireAuth's `.auth` object will return an initialized |
| 7 | +`firebase.auth.Auth` instance, allowing you to log users in and out. [See |
| 8 | +the Firebase docs for more information on what methods are availabile.](https://firebase.google.com/docs/reference/js/firebase.auth.Auth) |
55 | 9 |
|
56 | 10 | **Example app:**
|
57 | 11 |
|
58 | 12 | ```ts
|
59 | 13 | import { Component } from '@angular/core';
|
60 |
| -import { AngularFire } from 'angularfire2'; |
| 14 | +import { Observable } from 'rxjs/Observable'; |
| 15 | +import { AngularFireAuth } from 'angularfire2/auth'; |
| 16 | +import { GoogleAuthProvider, User as FirebaseUser } from 'firebase/auth'; |
61 | 17 |
|
62 | 18 | @Component({
|
63 | 19 | selector: 'app-root',
|
64 | 20 | template: `
|
65 |
| - <div> {{ (af.auth | async)?.uid }} </div> |
| 21 | + <div> {{ (user | async)?.uid }} </div> |
66 | 22 | <button (click)="login()">Login</button>
|
67 | 23 | <button (click)="logout()">Logout</button>
|
68 | 24 | `,
|
69 | 25 | })
|
70 | 26 | export class AppComponent {
|
71 |
| - constructor(public af: AngularFire) {} |
72 | 27 |
|
73 |
| - login() { |
74 |
| - this.af.auth.login(); |
75 |
| - } |
| 28 | + user: Observable<FirebaseUser>; |
76 | 29 |
|
77 |
| - logout() { |
78 |
| - this.af.auth.logout(); |
| 30 | + constructor(public afAuth: AngularFireAuth) { |
| 31 | + this.user = afAuth.authState; |
79 | 32 | }
|
80 |
| -} |
81 |
| -``` |
82 |
| - |
83 |
| -## Logout users |
84 |
| - |
85 |
| -Deletes the authentication token issued by Firebase and signs user out. See [Auth.signOut()](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signOut) in the Firebase API reference. |
86 |
| - |
87 |
| -Sample Usage: |
88 |
| - |
89 |
| -```ts |
90 |
| - signOut(): { |
91 |
| - this.af.auth.logout(); |
92 |
| - } |
93 |
| -``` |
94 | 33 |
|
95 |
| -## Override configuration / No config |
96 |
| - |
97 |
| -Authentication works without configuration, and even if you have setup |
98 |
| -authentication in the bootstrap phase, you can still override the configuration. |
99 |
| - |
100 |
| -```ts |
101 |
| -// Anonymous |
102 |
| -af.auth.login({ |
103 |
| - provider: AuthProviders.Anonymous, |
104 |
| - method: AuthMethods.Anonymous, |
105 |
| -}); |
106 |
| - |
107 |
| -// Email and password |
108 |
| -af.auth.login({ |
109 |
| - |
110 |
| - password: 'password', |
111 |
| -}, |
112 |
| -{ |
113 |
| - provider: AuthProviders.Password, |
114 |
| - method: AuthMethods.Password, |
115 |
| -}); |
116 |
| - |
117 |
| -// Social provider redirect |
118 |
| -af.auth.login({ |
119 |
| - provider: AuthProviders.Twitter, |
120 |
| - method: AuthMethods.Redirect, |
121 |
| -}); |
122 |
| - |
123 |
| -// Social provider popup |
124 |
| -af.auth.login({ |
125 |
| - provider: AuthProviders.Github, |
126 |
| - method: AuthMethods.Popup, |
127 |
| -}); |
128 |
| -``` |
129 |
| - |
130 |
| -**Example app:** |
131 |
| - |
132 |
| -*Before running the below example, make sure you've correctly enabled the appropriate sign-in providers in your Firebase console under Auth tab to avoid any exceptions.* |
133 |
| - |
134 |
| -```ts |
135 |
| -import { Component } from '@angular/core'; |
136 |
| -import { AngularFire, AuthProviders, AuthMethods } from 'angularfire2'; |
137 |
| - |
138 |
| -@Component({ |
139 |
| - selector: 'app-root', |
140 |
| - template: ` |
141 |
| - <div> {{ (af.auth | async)?.uid }} </div> |
142 |
| - <button (click)="login()">Login With Twitter</button> |
143 |
| - <button (click)="overrideLogin()">Login Anonymously</button> |
144 |
| - `, |
145 |
| -}) |
146 |
| -export class AppComponent { |
147 |
| - constructor(public af: AngularFire) { |
148 |
| - this.af.auth.subscribe(auth => console.log(auth)); |
149 |
| - } |
150 | 34 | login() {
|
151 |
| - this.af.auth.login({ |
152 |
| - provider: AuthProviders.Twitter, |
153 |
| - method: AuthMethods.Popup, |
154 |
| - }); |
| 35 | + this.afAuth.auth.signInWithPopup(new GoogleAuthProvider()); |
155 | 36 | }
|
156 |
| - overrideLogin() { |
157 |
| - this.af.auth.login({ |
158 |
| - provider: AuthProviders.Anonymous, |
159 |
| - method: AuthMethods.Anonymous, |
160 |
| - }); |
| 37 | + |
| 38 | + logout() { |
| 39 | + this.afAuth.auth.signOut(); |
161 | 40 | }
|
162 | 41 | }
|
163 | 42 | ```
|
|
0 commit comments