Skip to content

Instantly share code, notes, and snippets.

@kamescg
Created June 26, 2018 18:46
Show Gist options
  • Save kamescg/bbdf4f1ec97731011bb34e8533066e5f to your computer and use it in GitHub Desktop.
Save kamescg/bbdf4f1ec97731011bb34e8533066e5f to your computer and use it in GitHub Desktop.
**
* Identity
* @desc Authenticates a uPort decentralized identity
* @param {object} request The HTTP request object
* @param {object} response The HTTP response object
*/
exports.identity = functions.https.onRequest((request, response) => {
cors(request, response, () => {
uportCredentials.receive(request.body.JWT)
.then(profile => {
/**
* TODO(@kamescg) Saving the profile should be handled better.
* During registration the information doesn't correctly appear in frontend.
*/
if (profile.name) {
admin.auth().updateUser(profile.address, {
displayName: profile.name
})
}
if (profile.avatar) {
admin.auth().updateUser(profile.address, {
photoURL: profile.avatar.uri
})
}
/**
* Generate the authentication token using the uPort MNID.
* Send the authentication token to application fronend.
* Save the latest credentials in the identity profile branch.
*/
admin.auth().createCustomToken(profile.address)
.then((AuthenticationToken) => {
response.json(AuthenticationToken).send() // Return the AuthenticationToken to the Application Frontend
const mnidDecoded = decode(profile.address)
// Save Profile in Realtime Database
database.databaseWrite({
writeType: 'update',
branch: ['users', profile.address, 'profile'],
payload: {
...profile,
addressDecoded: mnidDecoded
}
})
// Save Profile in Firestore (NoSQL) - Experimental
// TODO(@kamescg) Add Firestore Redux Saga to React Frontend
// firestore.collection('people').update(profile)
})
.catch(error => {
response.send(error)
database.databaseWrite({
writeType: 'create',
branch: ['errors'],
payload: {
data: error,
meta: {
type: 'firebase',
category: 'authenticationToken'
}
}
})
console.log('Error creating Firebase authentication token:', error)
})
})
.catch(error => {
database.databaseWrite({
writeType: 'create',
branch: ['errors'],
payload: {
data: error,
meta: {
type: 'uport',
category: 'authenticationToken'
}
}
})
response.send(error)
console.log('Error authenticating uPort decentralized identity:', error)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment