Skip to content

Instantly share code, notes, and snippets.

@Villanuevand
Created September 22, 2022 03:53
Show Gist options
  • Save Villanuevand/2ff04d801fc2b551cb506cf9c5ada854 to your computer and use it in GitHub Desktop.
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.
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