-
Notifications
You must be signed in to change notification settings - Fork 873
/
facebook.ts
139 lines (129 loc) · 4.44 KB
/
facebook.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as externs from '@firebase/auth-types-exp';
import { FirebaseError } from '@firebase/util';
import { TaggedWithTokenResponse } from '../../model/id_token';
import { UserCredential } from '../../model/user';
import { OAuthCredential } from '../credentials/oauth';
import { OAuthProvider } from './oauth';
/**
* Provider for generating an {@link OAuthCredential} for {@link @firebase/auth-types#ProviderId.FACEBOOK}.
*
* @example
* ```javascript
* // Sign in using a redirect.
* const provider = new FacebookAuthProvider();
* // Start a sign in process for an unauthenticated user.
* provider.addScope('user_birthday');
* await signInWithRedirect(auth, provider);
* // This will trigger a full page redirect away from your app
*
* // After returning from the redirect when your app initializes you can obtain the result
* const result = await getRedirectResult(auth);
* if (result) {
* // This is the signed-in user
* const user = result.user;
* // This gives you a Facebook Access Token.
* const credential = provider.credentialFromResult(auth, result);
* const token = credential.accessToken;
* }
* ```
*
* @example
* ```javascript
* // Sign in using a popup.
* const provider = new FacebookAuthProvider();
* provider.addScope('user_birthday');
* const result = await signInWithPopup(auth, provider);
*
* // The signed-in user info.
* const user = result.user;
* // This gives you a Facebook Access Token.
* const credential = provider.credentialFromResult(auth, result);
* const token = credential.accessToken;
* ```
*
* @public
*/
export class FacebookAuthProvider extends OAuthProvider {
/** Always set to {@link @firebase/auth-types#SignInMethod.FACEBOOK}. */
static readonly FACEBOOK_SIGN_IN_METHOD = externs.SignInMethod.FACEBOOK;
/** Always set to {@link @firebase/auth-types#ProviderId.FACEBOOK}. */
static readonly PROVIDER_ID = externs.ProviderId.FACEBOOK;
constructor() {
super(externs.ProviderId.FACEBOOK);
}
/**
* Creates a credential for Facebook.
*
* @example
* ```javascript
* // `event` from the Facebook auth.authResponseChange callback.
* const credential = FacebookAuthProvider.credential(event.authResponse.accessToken);
* const result = await signInWithCredential(credential);
* ```
*
* @param accessToken - Facebook access token.
*/
static credential(accessToken: string): externs.OAuthCredential {
return OAuthCredential._fromParams({
providerId: FacebookAuthProvider.PROVIDER_ID,
signInMethod: FacebookAuthProvider.FACEBOOK_SIGN_IN_METHOD,
accessToken
});
}
/**
* Used to extract the underlying {@link OAuthCredential} from a {@link @firebase/auth-types#UserCredential}.
*
* @param userCredential - The user credential.
*/
static credentialFromResult(
userCredential: externs.UserCredential
): externs.OAuthCredential | null {
return FacebookAuthProvider.credentialFromTaggedObject(
userCredential as UserCredential
);
}
/**
* Used to extract the underlying {@link OAuthCredential} from a {@link @firebase/auth-types#AuthError} which was
* thrown during a sign-in, link, or reauthenticate operation.
*
* @param userCredential - The user credential.
*/
static credentialFromError(
error: FirebaseError
): externs.OAuthCredential | null {
return FacebookAuthProvider.credentialFromTaggedObject(
(error.customData || {}) as TaggedWithTokenResponse
);
}
private static credentialFromTaggedObject({
_tokenResponse: tokenResponse
}: TaggedWithTokenResponse): externs.OAuthCredential | null {
if (!tokenResponse || !('oauthAccessToken' in tokenResponse)) {
return null;
}
if (!tokenResponse.oauthAccessToken) {
return null;
}
try {
return FacebookAuthProvider.credential(tokenResponse.oauthAccessToken);
} catch {
return null;
}
}
}