-
-
Save Villanuevand/2ff04d801fc2b551cb506cf9c5ada854 to your computer and use it in GitHub Desktop.
Cloud function que genera un nuevo identicon basado en el correo electronico del usuario, usando el servicio de gravatar.
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
import * as functions from "firebase-functions"; | |
import * as admin from 'firebase-admin'; | |
import UserRecord = admin.auth.UserRecord; | |
import {EventContext, logger} from "firebase-functions"; | |
import {Md5} from 'ts-md5'; | |
admin.initializeApp({ projectId: 'villanuevand-labs' }); | |
export const newUser = functions.auth.user().onCreate(async (user: UserRecord, context: EventContext) => { | |
logger.info('Usuario original',{user}); | |
try { | |
let updateUserInfo; | |
const hash = Md5.hashStr(`${user.email}`); | |
if(!user.photoURL) { | |
updateUserInfo = await admin.auth().updateUser(user.uid, { | |
photoURL: `https://www.gravatar.com/avatar/${hash}.jpg?d=identicon` | |
}); | |
} | |
logger.info('Se actualizo correctamente', {updateUserInfo}); | |
} catch (e) { | |
logger.error('Hubo un error en la función newUser', e) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment