@levante-framework/firekit
Version:
A library to facilitate Firebase authentication and Firestore interaction for LEVANTE apps
54 lines (53 loc) • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchEmailAuthMethods = exports.isUsernameAvailable = exports.isEmailAvailable = exports.isRoarAuthEmail = exports.roarEmail = void 0;
const auth_1 = require("firebase/auth");
/**
* Return a unique and reproducible email address for the user.
*
* @function
* @param {string} roarPid - The ROAR user PID
* @returns {string} - The email address
*/
const roarEmail = (roarPid) => {
return `${roarPid}@roar-auth.com`;
};
exports.roarEmail = roarEmail;
const isRoarAuthEmail = (email) => {
return email.split('@')[1] === 'roar-auth.com';
};
exports.isRoarAuthEmail = isRoarAuthEmail;
const isEmailAvailable = async (auth, email) => {
return (0, auth_1.fetchSignInMethodsForEmail)(auth, email).then((signInMethods) => {
return signInMethods.length === 0;
});
};
exports.isEmailAvailable = isEmailAvailable;
const isUsernameAvailable = async (auth, username) => {
return (0, exports.isEmailAvailable)(auth, (0, exports.roarEmail)(username));
};
exports.isUsernameAvailable = isUsernameAvailable;
const fetchEmailAuthMethods = async (auth, email) => {
return (0, auth_1.fetchSignInMethodsForEmail)(auth, email).then((signInMethods) => {
const methods = [];
if (signInMethods.indexOf(auth_1.EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD) != -1) {
// User can sign in with email/password.
methods.push('password');
}
if (signInMethods.indexOf(auth_1.EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD) != -1) {
// User can sign in with email/link.
methods.push('link');
}
if (signInMethods.indexOf('google.com') != -1) {
methods.push('google');
}
if (signInMethods.indexOf('oidc.clever') != -1) {
methods.push('clever');
}
if (signInMethods.indexOf('oidc.classlink') != -1) {
methods.push('classlink');
}
return methods;
});
};
exports.fetchEmailAuthMethods = fetchEmailAuthMethods;