Created
June 28, 2018 08:25
-
-
Save jittuu/7cab984e8ef20373098653e083db40e6 to your computer and use it in GitHub Desktop.
generate firestore token for backoffice user
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 { compare } from 'bcryptjs'; | |
import { Merchant } from '../state'; | |
interface AuthRequest { | |
username: string; | |
password: string; | |
} | |
export const authMerchant = functions.https.onCall(async (data: AuthRequest, context) => { | |
const { username, password } = data; | |
if (username) { | |
const qry = admin.firestore().collection('merchants').where('loginUserName', '==', username.toUpperCase()); | |
const sn = await qry.get(); | |
if (sn.size > 0) { | |
const d = sn.docs[0]; | |
const { password: hashed } = d.data() as Merchant; | |
const same = await compare(password, hashed) | |
const auth = admin.auth(); | |
if (same) { | |
const ftoken = await admin.auth().createCustomToken(d.id, { | |
merchant: true, | |
}); | |
return { | |
success: true, | |
token: ftoken, | |
}; | |
} | |
return { | |
success: false, | |
error: 'invalidCredential', | |
}; | |
} | |
} | |
return { | |
success: false, | |
error: 'userNotFound', | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment