-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathapple.js
209 lines (175 loc) · 6.38 KB
/
apple.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
// Docs: https://source.corp.google.com/piper///depot/google3/third_party/devsite/firebase/en/docs/auth/web/apple.md
function appleProvider() {
// [START auth_apple_provider_create]
const { OAuthProvider } = require("firebase/auth");
const provider = new OAuthProvider('apple.com');
// [END auth_apple_provider_create]
// [START auth_apple_provider_scopes]
provider.addScope('email');
provider.addScope('name');
// [END auth_apple_provider_scopes]
// [START auth_apple_provider_params]
provider.setCustomParameters({
// Localize the Apple authentication screen in French.
locale: 'fr'
});
// [END auth_apple_provider_params]
}
function appleSignInPopup(provider) {
// [START auth_apple_signin_popup]
const { getAuth, signInWithPopup, OAuthProvider } = require("firebase/auth");
const auth = getAuth();
signInWithPopup(auth, provider)
.then((result) => {
// The signed-in user info.
const user = result.user;
// Apple credential
const credential = OAuthProvider.credentialFromResult(result);
const accessToken = credential.accessToken;
const idToken = credential.idToken;
// IdP data available using getAdditionalUserInfo(result)
// ...
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.customData.email;
// The credential that was used.
const credential = OAuthProvider.credentialFromError(error);
// ...
});
// [END auth_apple_signin_popup]
}
function appleSignInRedirect(provider) {
// [START auth_apple_signin_redirect]
const { getAuth, signInWithRedirect } = require("firebase/auth");
const auth = getAuth();
signInWithRedirect(auth, provider);
// [END auth_apple_signin_redirect]
}
function appleSignInRedirectResult() {
// [START auth_apple_signin_redirect_result]
const { getAuth, getRedirectResult, OAuthProvider } = require("firebase/auth");
// Result from Redirect auth flow.
const auth = getAuth();
getRedirectResult(auth)
.then((result) => {
const credential = OAuthProvider.credentialFromResult(result);
if (credential) {
// You can also get the Apple OAuth Access and ID Tokens.
const accessToken = credential.accessToken;
const idToken = credential.idToken;
}
// The signed-in user info.
const user = result.user;
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.customData.email;
// The credential that was used.
const credential = OAuthProvider.credentialFromError(error);
// ...
});
// [END auth_apple_signin_redirect_result]
}
function appleReauthenticatePopup() {
// [START auth_apple_reauthenticate_popup]
const { getAuth, reauthenticateWithPopup, OAuthProvider } = require("firebase/auth");
// Result from Redirect auth flow.
const auth = getAuth();
const provider = new OAuthProvider('apple.com');
reauthenticateWithPopup(auth.currentUser, provider)
.then((result) => {
// User is re-authenticated with fresh tokens minted and can perform
// sensitive operations like account deletion, or updating their email
// address or password.
// The signed-in user info.
const user = result.user;
// You can also get the Apple OAuth Access and ID Tokens.
const credential = OAuthProvider.credentialFromResult(result);
const accessToken = credential.accessToken;
const idToken = credential.idToken;
// ...
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.customData.email;
// The credential that was used.
const credential = OAuthProvider.credentialFromError(error);
// ...
});
// [END auth_apple_reauthenticate_popup]
}
function appleLinkFacebook() {
// [START auth_apple_link_facebook]
const { getAuth, linkWithPopup, FacebookAuthProvider } = require("firebase/auth");
const auth = getAuth();
const provider = new FacebookAuthProvider();
provider.addScope('user_birthday');
// Assuming the current user is an Apple user linking a Facebook provider.
linkWithPopup(auth.currentUser, provider)
.then((result) => {
// Facebook credential is linked to the current Apple user.
// ...
// The user can now sign in to the same account
// with either Apple or Facebook.
})
.catch((error) => {
// Handle error.
});
// [END auth_apple_link_facebook]
}
function appleNonceNode() {
// [START auth_apple_nonce_node]
const crypto = require("crypto");
const string_decoder = require("string_decoder");
// Generate a new random string for each sign-in
const generateNonce = (length) => {
const decoder = new string_decoder.StringDecoder("ascii");
const buf = Buffer.alloc(length);
let nonce = "";
while (nonce.length < length) {
crypto.randomFillSync(buf);
nonce = decoder.write(buf);
}
return nonce.slice(0, length);
};
const unhashedNonce = generateNonce(10);
// SHA256-hashed nonce in hex
const hashedNonceHex = crypto.createHash('sha256')
.update(unhashedNonce).digest().toString('hex');
// [END auth_apple_nonce_node]
}
function appleSignInNonce(appleIdToken, unhashedNonce,) {
// [START auth_apple_signin_nonce]
const { getAuth, signInWithCredential, OAuthProvider } = require("firebase/auth");
const auth = getAuth();
// Build Firebase credential with the Apple ID token.
const provider = new OAuthProvider('apple.com');
const authCredential = provider.credential({
idToken: appleIdToken,
rawNonce: unhashedNonce,
});
// Sign in with credential form the Apple user.
signInWithCredential(auth, authCredential)
.then((result) => {
// User signed in.
})
.catch((error) => {
// An error occurred. If error.code == 'auth/missing-or-invalid-nonce',
// make sure you're sending the SHA256-hashed nonce as a hex string
// with your request to Apple.
console.log(error);
});
// [END auth_apple_signin_nonce]
}