Skip to content

Instantly share code, notes, and snippets.

@jittuu
Created June 28, 2018 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jittuu/7cab984e8ef20373098653e083db40e6 to your computer and use it in GitHub Desktop.
Save jittuu/7cab984e8ef20373098653e083db40e6 to your computer and use it in GitHub Desktop.
generate firestore token for backoffice user
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