Skip to content

Commit f5966cd

Browse files
committed
Auth linking snippets
1 parent 73165b6 commit f5966cd

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

auth/link-multiple-accounts.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// These samples are intended for Web so this import would normally be
2+
// done in HTML however using modules here is more convenient for
3+
// ensuring sample correctness offline.
4+
var firebase = require('firebase');
5+
var auth = firebase.auth();
6+
7+
var MyUserDataRepo = function() {};
8+
9+
MyUserDataRepo.prototype.merge = function(data1, data2) {
10+
// TODO(you): How you implement this is specific to your application!
11+
return {
12+
...data1,
13+
...data2,
14+
}
15+
}
16+
17+
MyUserDataRepo.prototype.set = function(user, data) {
18+
// TODO(you): How you implement this is specific to your application!
19+
}
20+
21+
MyUserDataRepo.prototype.delete = function(user) {
22+
// TODO(you): How you implement this is specific to your application!
23+
}
24+
25+
MyUserDataRepo.prototype.get = function(user) {
26+
// TODO(you): How you implement this is specific to your application!
27+
return {};
28+
}
29+
30+
function getProviders() {
31+
// [START auth_get_providers]
32+
var googleProvider = new firebase.auth.GoogleAuthProvider();
33+
var facebookProvider = new firebase.auth.FacebookAuthProvider();
34+
var twitterProvider = new firebase.auth.TwitterAuthProvider();
35+
var githubProvider = new firebase.auth.GithubAuthProvider();
36+
// [END auth_get_providers]
37+
}
38+
39+
function linkWithPopup() {
40+
var provider = new firebase.auth.GoogleAuthProvider();
41+
42+
// [START auth_link_with_popup]
43+
auth.currentUser.linkWithPopup(provider).then(function(result) {
44+
// Accounts successfully linked.
45+
var credential = result.credential;
46+
var user = result.user;
47+
// ...
48+
}).catch(function(error) {
49+
// Handle Errors here.
50+
// ...
51+
});
52+
// [END auth_link_with_popup]
53+
}
54+
55+
function linkWithRedirect() {
56+
var provider = new firebase.auth.GoogleAuthProvider();
57+
58+
// [START auth_link_with_redirect]
59+
auth.currentUser.linkWithRedirect(provider)
60+
.then(/* ... */)
61+
.catch(/* ... */);
62+
// [END auth_link_with_redirect]
63+
64+
// [START auth_get_redirect_result]
65+
auth.getRedirectResult().then(function(result) {
66+
if (result.credential) {
67+
// Accounts successfully linked.
68+
var credential = result.credential;
69+
var user = result.user;
70+
// ...
71+
}
72+
}).catch(function(error) {
73+
// Handle Errors here.
74+
// ...
75+
});
76+
// [END auth_get_redirect_result]
77+
}
78+
79+
// For one, the user variable in the line:
80+
// return user.delete().then(function() {
81+
// does not have a .delete function, it would need to be user.user.delete()
82+
// & in this example they suggest:
83+
// // Merge prevUser and currentUser data stored in Firebase
84+
// which in this auth context (& assuming basic security rules enabled) would mean you would only have access to currentUser ’s storage,
85+
// but then they delete the current user & link the credential to the previous user. In otherwords you would only be able to merge the previous user’s
86+
// data into the current user’s storage, however the current user is deleted & no longer accessible
87+
// So I’d just like a sanity check here that this example is indeed a flawed approach!
88+
89+
90+
function mergeAccounts() {
91+
// This is just a dummy variable for sample purposes, the other
92+
// snippets demonstrate how to get a real credential.
93+
var newCredential = new firebase.auth.AuthCredential();
94+
95+
// [START auth_merge_accounts]
96+
// The implementation of how you store your user data depends on your application
97+
var repo = new MyUserDataRepo();
98+
99+
// Get reference to the currently signed-in user
100+
var prevUser = auth.currentUser;
101+
102+
// Get the data which you will want to merge. This should be done now
103+
// while the app is still signed in as this user.
104+
var prevUserData = repo.get(prevUser);
105+
106+
// Delete the user's data now, we will restore it if the merge fails
107+
repo.delete(prevUser);
108+
109+
// Sign in user with the account you want to link to
110+
auth.signInWithCredential(newCredential).then(function(result) {
111+
console.log("Sign In Success", result);
112+
var currentUser = result.user;
113+
var currentUserData = repo.get(currentUser);
114+
115+
// Merge prevUser and currentUser data stored in Firebase.
116+
// Note: How you handle this is specific to your application
117+
var mergedData = repo.merge(prevUserData, currentUserData);
118+
119+
return prevUser.linkWithCredential(result.credential)
120+
.then(function(linkResult) {
121+
// Sign in with the newly linked credential
122+
return auth.signInWithCredential(linkResult.credential);
123+
})
124+
.then(function(signInResult) {
125+
// Save the merged data to the new user
126+
repo.set(signInResult.user, mergedData);
127+
});
128+
}).catch(function(error) {
129+
// If there are errors we want to undo the data merge/deletion
130+
console.log("Sign In Error", error);
131+
repo.set(prevUser, prevUserData);
132+
repo.set(currentUser, currentUserData);
133+
});
134+
// [END auth_merge_accounts]
135+
}
136+

auth/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "auth",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {},
6+
"author": "",
7+
"license": "Apache 2.0",
8+
"dependencies": {
9+
"firebase": "^7.12.0"
10+
}
11+
}

0 commit comments

Comments
 (0)