Skip to content

Commit cb00496

Browse files
jamesdanielsdavideast
authored andcommitted
Reworking the auth guide
1 parent 6d5daab commit cb00496

File tree

2 files changed

+17
-137
lines changed

2 files changed

+17
-137
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ typings/
66
npm-debug.log
77
.idea/
88
.vscode/settings.json
9+
angularfire2-*.tgz

docs/5-user-authentication.md

Lines changed: 16 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,42 @@
11
# 5. User authentication
22

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.
75

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)
559

5610
**Example app:**
5711

5812
```ts
5913
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';
6117

6218
@Component({
6319
selector: 'app-root',
6420
template: `
65-
<div> {{ (af.auth | async)?.uid }} </div>
21+
<div> {{ (user | async)?.uid }} </div>
6622
<button (click)="login()">Login</button>
6723
<button (click)="logout()">Logout</button>
6824
`,
6925
})
7026
export class AppComponent {
71-
constructor(public af: AngularFire) {}
7227

73-
login() {
74-
this.af.auth.login();
75-
}
28+
user: Observable<FirebaseUser>;
7629

77-
logout() {
78-
this.af.auth.logout();
30+
constructor(public afAuth: AngularFireAuth) {
31+
this.user = afAuth.authState;
7932
}
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-
```
9433

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-
}
15034
login() {
151-
this.af.auth.login({
152-
provider: AuthProviders.Twitter,
153-
method: AuthMethods.Popup,
154-
});
35+
this.afAuth.auth.signInWithPopup(new GoogleAuthProvider());
15536
}
156-
overrideLogin() {
157-
this.af.auth.login({
158-
provider: AuthProviders.Anonymous,
159-
method: AuthMethods.Anonymous,
160-
});
37+
38+
logout() {
39+
this.afAuth.auth.signOut();
16140
}
16241
}
16342
```

0 commit comments

Comments
 (0)