Skip to content

Instantly share code, notes, and snippets.

@vonvick
Created March 27, 2019 13:11
Show Gist options
  • Save vonvick/a1a56ea9f75d92684f5dd49f554ae7e6 to your computer and use it in GitHub Desktop.
Save vonvick/a1a56ea9f75d92684f5dd49f554ae7e6 to your computer and use it in GitHub Desktop.
// import the firebase config into the auth controller
import firebase from '../../firebase';
const firebaseAuth = async (req, res) => {
try {
// req.body the payload coming from the client to authenticate the user
// uid is the firebase uid generated when a user is authenticated on the firebase client
const userRequest = await firebase.database().ref(`users/${req.body.uid}`).once('value');
const userPayload = userRequest.val();
if (userPayload) {
// create tokenClaims if you wish to add extra data to the generated user token
const tokenClaims = {
roleId: userPayload.roleId
}
// use firebase admin auth to set token claimsm which will be decoded for additional authentication
await firebase.auth().setCustomUserClaims(user.uid, tokenClaims);
return res.status(200).json({data: tokenClaims});
} else {
return res.status(404).json({error: {message: 'No user found'}});
}
} catch (error) {
return res.status(500).json({
error: { message: 'could not complete auth request'}
});
}
}
export default {
firebaseAuth
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment