import { Request , Response } from "express" ; import * as admin from 'firebase-admin' ; /** Add a new user */ export async function addUser ( req , res ) { const { displayName , email , phoneNumber , photoURL , disabled , emailVerified , password , customClaims } = req . body ; console . log ( 'Adding user' , email ) ; if ( ! email || ! password ) { return res . status ( 400 ) . send ( { message : 'Missing fields' } ) ; } try { const auth = admin . auth ( ) ; const user = await auth . createUser ( { displayName , email , password , phoneNumber , photoURL , disabled , emailVerified } ) ; if ( customClaims ) { await auth . setCustomUserClaims ( user . uid , customClaims ) ; } return res . status ( 201 ) . send ( user ) ; } catch ( err ) { return res . status ( 500 ) . send ( { message : `${ err . code } - ${ err . message }` } ) ; } } /** Returns the full UserRecord of the requested user */ export async function getUser ( req , res ) { const { uid } = req . params ; console . log ( 'Getting user' , uid ) ; try { const user = await admin . auth ( ) . getUser ( uid ) ; return res . status ( 201 ) . send ( user ) ; } catch ( err ) { return res . status ( 500 ) . send ( { message : `${ err . code } - ${ err . message }` } ) ; } } /** Updates the UserRecord of the requested user */ export async function updateUser ( req , res ) { const { displayName , email , phoneNumber , photoURL , disabled , emailVerified , password , customClaims } = req . body ; const { uid } = req . params ; console . log ( 'Updating user' , uid ) ; const needUpdate = ! ! ( displayName || email || phoneNumber || photoURL || emailVerified || password ) ; if ( ! ( needUpdate || customClaims !== undefined ) ) { return res . status ( 400 ) . send ( { message : 'Missing fields' } ) ; } try { const auth = admin . auth ( ) ; if ( customClaims !== undefined ) { await auth . setCustomUserClaims ( uid , customClaims ) ; } if ( needUpdate ) { const user = await auth . updateUser ( uid , { displayName , email , password , phoneNumber , photoURL , disabled , emailVerified } ) ; return res . status ( 201 ) . send ( user ) ; } else { const user = await auth . getUser ( uid ) ; return res . status ( 201 ) . send ( user ) ; } } catch ( err ) { return res . status ( 500 ) . send ( { message : `${ err . code } - ${ err . message }` } ) ; } } /** Deletes a user */ export async function deleteUser ( req , res ) { const { uid } = req . params ; console . log ( 'Deleting user' , uid ) ; try { await admin . auth ( ) . deleteUser ( uid ) ; return res . status ( 201 ) . send ( { } ) ; } catch ( err ) { return res . status ( 500 ) . send ( { message : `${ err . code } - ${ err . message }` } ) ; } } /** Lists users by several identifiers (uid, email, phoneNumber, ...) */ export async function listUsers ( req , res ) { console . log ( 'Listing users' , req . body ) ; if ( ! Array . isArray ( req . body ) || req . body . length <= 0 ) { return res . status ( 400 ) . send ( { message : 'Missing fields' } ) ; } try { const result = await admin . auth ( ) . getUsers ( req . body ) ; return res . status ( 201 ) . send ( result . users ) ; } catch ( err ) { return res . status ( 500 ) . send ( { message : `${ err . code } - ${ err . message }` } ) ; } } /** List all the users sorting the result in ascending UID order */ export async function listAllUsers ( req , res ) { console . log ( 'Listing all users' ) ; try { const allUsers = await _listAllUsers ( ) ; const users = allUsers . sort ( ( a , b ) => a . uid > b . uid ? 1 : ( a . uid < b . uid ? - 1 : 0 ) ) ; return res . status ( 201 ) . send ( users ) ; } catch ( err ) { return res . status ( 500 ) . send ( { message : `${ err . code } - ${ err . message }` } ) ; } } async function _listAllUsers ( nextPageToken ? ) { const listUsersResult = await admin . auth ( ) . listUsers ( 1000 , nextPageToken ) ; if ( listUsersResult . pageToken ) { return listUsersResult . users . concat ( await _listAllUsers ( listUsersResult . pageToken ) ) ; } return listUsersResult . users ; }