Last active
February 17, 2018 17:18
-
-
Save ShaMan123/11243fcb13f30066cedbdb8b4e5b074f to your computer and use it in GitHub Desktop.
FirebaseUI User verification/logic as part of auth flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
//js file handling FirebaseUI | |
//TO-DO: FirebaseUI init code....... | |
documentation: https://github.com/firebase/firebaseui-web#available-callbacks | |
Flow of Data: | |
-- Pass params from signInSuccess callback (auth.js) | |
-- to cloud function getUserClaims (cloudFunctions.js) | |
-- via rewrites (firebase.json) | |
*/ | |
const signInSuccessUrl = '/'; | |
var uiConfig = { | |
signInSuccessUrl: signInSuccessUrl, | |
callbacks: { | |
signInSuccess: function (currentUser, credential, redirectUrl) { | |
const user = currentUser.toJSON(); | |
firebase.auth().currentUser.getIdToken(true) | |
.then(function (idToken) { | |
var url = '/login?uid=' + user.uid + '&exponge=true'; //TO-DO: change url to rewrite => in firebase.json & pass params | |
location.href = url; //calls cloudFunction getUserClaims via rewrite | |
return; | |
}); | |
}, | |
signInFailure: function (error) { | |
console.log('ERROR: failed to sign in user', error); | |
location.href = signInSuccessUrl; | |
}, | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//TO-DO: firebase.admin() init code..... | |
const welcomeUrl = '/path/to/page'; | |
const index = '/'; | |
/* | |
auth().setCustomUserClaims() | |
documentation: https://firebase.google.com/docs/auth/admin/custom-claims | |
*/ | |
exports.newUser = functions.auth.user().onCreate((event) => { | |
const user = event.data; | |
const uid = user.uid; | |
admin.auth().setCustomUserClaims(uid, { isNewUser: true }) | |
.then(() => { | |
console.log('successful setCustomUserClaims', uid); | |
}) | |
.catch(err => { | |
console.log('Couldn\'t set custom claims', err, uid) | |
}); | |
//Do stuff | |
}); | |
exports.getUserClaims = functions.https.onRequest((request, response) => { | |
var query = request.query; | |
var token = query.token; | |
var uid = query.uid; | |
var exponge = query.exponge; | |
admin.auth().getUser(uid).then((userRecord) => { | |
var claims = userRecord.customClaims; | |
if (claims && claims.isNewUser) { | |
response.redirect(welcomeUrl); | |
} | |
else { | |
response.redirect(index); | |
} | |
//Add lots of logic and params to the query string to enable versatile redirecting | |
if (exponge) { | |
admin.auth().setCustomUserClaims(uid, { isNewUser: null }) | |
.catch(err => { | |
console.log('Couldn\'t set custom claims', err, uid) | |
}); | |
} | |
}).catch(err => { | |
console.log('Couldn\'t get custom claims', err, uid) | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"database": { | |
"rules": "database.rules.json" | |
}, | |
"hosting": { | |
"public": "public", | |
"rewrites": [ | |
{ | |
/* | |
This rewrite calls the cloud funtion getUserClaims when hosting location.href = '/login' | |
documentation: https://firebase.google.com/docs/hosting/functions | |
*/ | |
"source": "/login", | |
"function": "getUserClaims" | |
} | |
], | |
"ignore": [ | |
"firebase.json", | |
"**/ .*", "**/node_modules/**" | |
] | |
}, | |
"storage": { | |
"rules": "storage.rules" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment