-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathlink-multiple-accounts.js
224 lines (194 loc) · 7.24 KB
/
link-multiple-accounts.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// These samples are intended for Web so this import would normally be
// done in HTML however using modules here is more convenient for
// ensuring sample correctness offline.
import firebase from "firebase/app";
import "firebase/auth";
const auth = firebase.auth();
const MyUserDataRepo = function() {};
MyUserDataRepo.prototype.merge = function(data1, data2) {
// TODO(you): How you implement this is specific to your application!
return {
...data1,
...data2,
};
};
MyUserDataRepo.prototype.set = function(user, data) {
// TODO(you): How you implement this is specific to your application!
};
MyUserDataRepo.prototype.delete = function(user) {
// TODO(you): How you implement this is specific to your application!
};
MyUserDataRepo.prototype.get = function(user) {
// TODO(you): How you implement this is specific to your application!
return {};
};
function getProviders() {
// [START auth_get_providers]
var googleProvider = new firebase.auth.GoogleAuthProvider();
var facebookProvider = new firebase.auth.FacebookAuthProvider();
var twitterProvider = new firebase.auth.TwitterAuthProvider();
var githubProvider = new firebase.auth.GithubAuthProvider();
// [END auth_get_providers]
}
function simpleLink(credential) {
// [START auth_simple_link]
auth.currentUser.linkWithCredential(credential)
.then((usercred) => {
var user = usercred.user;
console.log("Account linking success", user);
}).catch((error) => {
console.log("Account linking error", error);
});
// [END auth_simple_link]
}
function anonymousLink(credential) {
// [START auth_anonymous_link]
auth.currentUser.linkWithCredential(credential)
.then((usercred) => {
var user = usercred.user;
console.log("Anonymous account successfully upgraded", user);
}).catch((error) => {
console.log("Error upgrading anonymous account", error);
});
// [END auth_anonymous_link]
}
function linkWithPopup() {
var provider = new firebase.auth.GoogleAuthProvider();
// [START auth_link_with_popup]
auth.currentUser.linkWithPopup(provider).then((result) => {
// Accounts successfully linked.
var credential = result.credential;
var user = result.user;
// ...
}).catch((error) => {
// Handle Errors here.
// ...
});
// [END auth_link_with_popup]
}
function linkWithRedirect() {
var provider = new firebase.auth.GoogleAuthProvider();
// [START auth_link_with_redirect]
auth.currentUser.linkWithRedirect(provider)
.then(/* ... */)
.catch(/* ... */);
// [END auth_link_with_redirect]
// [START auth_get_redirect_result]
auth.getRedirectResult().then((result) => {
if (result.credential) {
// Accounts successfully linked.
var credential = result.credential;
var user = result.user;
// ...
}
}).catch((error) => {
// Handle Errors here.
// ...
});
// [END auth_get_redirect_result]
}
function mergeAccounts(newCredential) {
// [START auth_merge_accounts]
// The implementation of how you store your user data depends on your application
var repo = new MyUserDataRepo();
// Get reference to the currently signed-in user
var prevUser = auth.currentUser;
// Get the data which you will want to merge. This should be done now
// while the app is still signed in as this user.
var prevUserData = repo.get(prevUser);
// Delete the user's data now, we will restore it if the merge fails
repo.delete(prevUser);
// Sign in user with the account you want to link to
auth.signInWithCredential(newCredential).then((result) => {
console.log("Sign In Success", result);
var currentUser = result.user;
var currentUserData = repo.get(currentUser);
// Merge prevUser and currentUser data stored in Firebase.
// Note: How you handle this is specific to your application
var mergedData = repo.merge(prevUserData, currentUserData);
return prevUser.linkWithCredential(result.credential)
.then((linkResult) => {
// Sign in with the newly linked credential
return auth.signInWithCredential(linkResult.credential);
})
.then((signInResult) => {
// Save the merged data to the new user
repo.set(signInResult.user, mergedData);
});
}).catch((error) => {
// If there are errors we want to undo the data merge/deletion
console.log("Sign In Error", error);
repo.set(prevUser, prevUserData);
});
// [END auth_merge_accounts]
}
function makeEmailCredential() {
var email = "test@test.com";
var password = "abcde12345";
// [START auth_make_email_credential]
var credential = firebase.auth.EmailAuthProvider.credential(email, password);
// [END auth_make_email_credential]
}
function unlink(providerId) {
var user = auth.currentUser;
// [START auth_unlink_provider]
user.unlink(providerId).then(() => {
// Auth provider unlinked from account
// ...
}).catch((error) => {
// An error happened
// ...
});
// [END auth_unlink_provider]
}
function accountExistsPopup(facebookProvider, goToApp, promptUserForPassword, promptUserForSignInMethod, getProviderForProviderId) {
// [START account_exists_popup]
// User tries to sign in with Facebook.
auth.signInWithPopup(facebookProvider).catch((error) => {
// User's email already exists.
if (error.code === 'auth/account-exists-with-different-credential') {
// The pending Facebook credential.
const pendingCred = error.credential;
// The provider account's email address.
const email = error.email;
// Present the user with a list of providers they might have
// used to create the original account.
// Then, ask the user to sign in with the existing provider.
const method = promptUserForSignInMethod();
if (method === 'password') {
// TODO: Ask the user for their password.
// In real scenario, you should handle this asynchronously.
const password = promptUserForPassword();
auth.signInWithEmailAndPassword(email, password).then((result) => {
return result.user.linkWithCredential(pendingCred);
}).then(() => {
// Facebook account successfully linked to the existing user.
goToApp();
});
return;
}
// All other cases are external providers.
// Construct provider object for that provider.
// TODO: Implement getProviderForProviderId.
const provider = getProviderForProviderId(method);
// At this point, you should let the user know that they already have an
// account with a different provider, and validate they want to sign in
// with the new provider.
// Note: Browsers usually block popups triggered asynchronously, so in
// real app, you should ask the user to click on a "Continue" button
// that will trigger signInWithPopup().
auth.signInWithPopup(provider).then((result) => {
// Note: Identity Platform doesn't control the provider's sign-in
// flow, so it's possible for the user to sign in with an account
// with a different email from the first one.
// Link the Facebook credential. We have access to the pending
// credential, so we can directly call the link method.
result.user.linkWithCredential(pendingCred).then((userCred) => {
// Success.
goToApp();
});
});
}
});
// [END account_exists_popup]
}