Skip to content

Commit 6f5231c

Browse files
mukesh51katowulf
authored andcommitted
docs(authentication): Adds API documentation for AngularFireAuth methods
Added AngulaFireAuth API's as per issue #93. Fixes a type in logout code; update to match auth guide This closes #93.
1 parent 04f257c commit 6f5231c

File tree

1 file changed

+123
-4
lines changed

1 file changed

+123
-4
lines changed

docs/api-reference.md

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,22 @@ bootstrap(App, [
121121
]);
122122
```
123123

124-
### FirebaseAuth
124+
### AngularFireAuth
125125

126-
Injectable service for managing authentication state.
126+
Type: `class`
127+
128+
Injectable service for managing authentication state. Extends rxjs `ReplaySubject`, which represents an object that is both an observable sequence as well as an observer.
127129

128130
#### Logging In
129131
To log in a user, call the `login` method on an instance of `FirebaseAuth` class. The method has
130132
the following two signatures:
131133

132134
```ts
133-
login(config?: AuthConfiguration): Promise<FirebaseAuthState>;
134-
login(credentials: AuthCredentials, config?: AuthConfiguration): Promise<FirebaseAuthState>;
135+
login(config?: AuthConfiguration): firebase.Promise<FirebaseAuthState>;
136+
login(credentials?: EmailPasswordCredentials |
137+
firebase.auth.AuthCredential | string): firebase.Promise<FirebaseAuthState>;
138+
login(credentials: EmailPasswordCredentials |
139+
firebase.auth.AuthCredential | string, config?: AuthConfiguration): firebase.Promise<FirebaseAuthState>
135140
```
136141

137142
The signature that is used depends on which AuthMethod you chose to use to login.
@@ -189,6 +194,61 @@ export class MyApp {
189194
}
190195
```
191196

197+
The AuthConfiguration Object has the following signature
198+
199+
```ts
200+
export interface AuthConfiguration {
201+
method?: AuthMethods;
202+
provider?: AuthProviders;
203+
remember?: string;
204+
scope?: string[];
205+
}
206+
```
207+
208+
* The AuthMethods and AuthProviders are enums, defining values for Methods and Providers respectively.
209+
See [Facebook-Login](https://firebase.google.com/docs/auth/web/facebook-login) and [Google-Signin](https://firebase.google.com/docs/auth/web/google-signin) for more information.
210+
211+
Usage:
212+
213+
```ts
214+
....
215+
signInWithFacebook(): Promise<FirebaseAuthState> {
216+
return this.auth$.login({
217+
provider: AuthProviders.Facebook,
218+
method: AuthMethods.Popup,
219+
});
220+
}
221+
....
222+
```
223+
224+
225+
```ts
226+
....
227+
signInWithGoogle(): Promise<FirebaseAuthState> {
228+
return this.auth$.login({
229+
provider: AuthProviders.Google,
230+
method: AuthMethods.Redirect,
231+
});
232+
....
233+
```
234+
235+
236+
`login(credentials?: EmailPasswordCredentials | firebase.auth.AuthCredential | string): firebase.Promise<FirebaseAuthState>` :
237+
238+
Takes one of the three arguments as input. The `EmailPasswordCredentials` object is same, as mentioned in createUser method. The login method can also take a [firebase.auth.AuthCredential](https://firebase.google.com/docs/reference/js/firebase.auth.AuthCredential) object as mentioned here.
239+
240+
Usage:
241+
242+
```ts
243+
....
244+
signInWithEmail(): Promise<FirebaseAuthState> {
245+
return this.auth$.login({
246+
247+
password: 'yourPassword'
248+
});
249+
....
250+
```
251+
192252
#### Subscribing to Authentication State
193253
194254
Type: `class FirebaseAuth extends ReplaySubject<FirebaseAuthState>`
@@ -226,6 +286,64 @@ class App {
226286

227287
```
228288
289+
290+
`getAuth(): FirebaseAuthState` : Returns the client's current Authentication State. The FirebaseAuthState is an interface with following signature
291+
292+
```ts
293+
export interface FirebaseAuthState {
294+
uid: string;
295+
provider: AuthProviders;
296+
auth: firebase.User;
297+
expires?: number;
298+
github?: firebase.UserInfo;
299+
google?: firebase.UserInfo;
300+
twitter?: firebase.UserInfo;
301+
facebook?: firebase.UserInfo;
302+
anonymous?: boolean;
303+
}
304+
```
305+
306+
Sample Usage:
307+
308+
```ts
309+
constructor(public auth: FirebaseAuth) {
310+
this.authState = auth.getAuth();
311+
auth.subscribe((state: FirebaseAuthState) => {
312+
this.authState = state;
313+
});
314+
}
315+
316+
get authenticated(): boolean {
317+
return this.authState !== null;
318+
}
319+
```
320+
321+
`logout(): void`: 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) for more information.
322+
323+
*It is worth noting that logout() is an asynchronous operation. There is an open bug against the Firebase SDK to make this return a promise.*
324+
325+
Sample Usage:
326+
327+
```ts
328+
signOut(): {
329+
this.af.auth.logout();
330+
}
331+
```
332+
333+
`createUser(credentials: EmailPasswordCredentials): firebase.Promise<FirebaseAuthState>` : Creates a new user with email/password provided. Returns a promise filled with FirebaseAuthState, which contains the uid of the created user.
334+
335+
The credentials object is an object containing email and password of the user to be created. See [createUserWithEmailAndPassword](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword) for more information.
336+
337+
```ts
338+
createNewUser(properties:Object): Promise<FirebaseAuthState> {
339+
return this.auth.createUser({
340+
341+
password: 'uniquePassword'
342+
})
343+
}
344+
```
345+
346+
229347
### FirebaseListObservable
230348
231349
Subclass of rxjs `Observable` which also has methods for updating
@@ -294,3 +412,4 @@ This is the equivalent of the Firebase SDK's
294412
If no `item` argument is provided, it removes all elements from the list.
295413
This is the equivalent of the Firebase SDK's
296414
[remove() method](https://firebase.google.com/docs/reference/js/firebase.database.Reference#remove).
415+

0 commit comments

Comments
 (0)