mockbase
Version:
Firebase v7+ mock.
150 lines • 5.96 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockAuth = void 0;
const firebase = require("firebase");
const social_signin_mock_1 = require("./social-signin-mock");
const user_store_1 = require("./user-store");
const auth_settings_1 = require("./auth-settings");
const user_credential_1 = require("./user-credential");
class MockAuth {
constructor(app) {
this.app = app;
this.currentUser = null;
this.languageCode = null;
this.settings = new auth_settings_1.AuthSettings();
this.store = new user_store_1.UserStore();
this.authStateEvents = new Set();
}
applyActionCode(code) {
throw new Error("Method not implemented.");
}
checkActionCode(code) {
throw new Error("Method not implemented.");
}
confirmPasswordReset(code, newPassword) {
throw new Error("Method not implemented.");
}
createUserWithEmailAndPassword(email, password) {
if (this.store.findByEmail(email)) {
throw new Error("auth/email-already-in-use");
}
const { providerId } = new firebase.auth.EmailAuthProvider();
const user = this.store.add({ email, password, providerId });
return this.signIn(user, { isNewUser: true });
}
fetchSignInMethodsForEmail(email) {
const user = this.store.findByEmail(email);
const providers = user ? user.providerData : [];
return Promise.resolve(providers.map((info) => info.providerId));
}
getRedirectResult() {
throw new Error("Method not implemented.");
}
isSignInWithEmailLink(emailLink) {
throw new Error("Method not implemented.");
}
mockSocialSignIn(provider) {
const mock = new social_signin_mock_1.SocialSignInMock(provider.providerId);
this.store.addSocialMock(mock);
return mock;
}
onAuthStateChanged(nextOrObserver, error, completed) {
this.authStateEvents.add(nextOrObserver);
nextOrObserver(this.currentUser);
return () => {
this.authStateEvents.delete(nextOrObserver);
};
}
onIdTokenChanged(nextOrObserver, error, completed) {
throw new Error("Method not implemented.");
}
sendPasswordResetEmail(email, actionCodeSettings) {
throw new Error("Method not implemented.");
}
sendSignInLinkToEmail(email, actionCodeSettings) {
throw new Error("Method not implemented.");
}
setPersistence(persistence) {
return Promise.resolve();
}
signIn(user, options) {
this.currentUser = user;
this.authStateEvents.forEach((listener) => {
listener(user);
});
return Promise.resolve(new user_credential_1.UserCredential(user, "signIn", options));
}
signInWithSocial(provider) {
return __awaiter(this, void 0, void 0, function* () {
const mockResponse = yield this.store.consumeSocialMock(provider);
let user = this.store.findByProviderAndEmail(mockResponse.email, provider.providerId);
if (user) {
return this.signIn(user, { isNewUser: false });
}
// user didn't exist, so it's created and then signed in
user = this.store.add(Object.assign(Object.assign({}, mockResponse), { providerId: provider.providerId }));
return this.signIn(user, { isNewUser: true });
});
}
signInAndRetrieveDataWithCredential(credential) {
throw new Error("Method not implemented.");
}
signInAnonymously() {
if (this.currentUser && this.currentUser.isAnonymous) {
return this.signIn(this.currentUser, { isNewUser: false });
}
const user = this.store.add({ isAnonymous: true });
return this.signIn(user, { isNewUser: true });
}
signInWithCredential(credential) {
throw new Error("Method not implemented.");
}
signInWithCustomToken(token) {
throw new Error("Method not implemented.");
}
signInWithEmailAndPassword(email, password) {
const user = this.store.findByEmail(email);
if (!user) {
return Promise.reject(new Error("auth/user-not-found"));
}
else if (user.password !== password) {
return Promise.reject(new Error("auth/wrong-password"));
}
return this.signIn(user, { isNewUser: false });
}
signInWithEmailLink(email, emailLink) {
throw new Error("Method not implemented.");
}
signInWithPhoneNumber(phoneNumber, applicationVerifier) {
throw new Error("Method not implemented.");
}
signInWithPopup(provider) {
return this.signInWithSocial(provider);
}
signInWithRedirect(provider) {
throw new Error("Method not implemented.");
}
signOut() {
this.currentUser = null;
this.authStateEvents.forEach((listener) => listener(this.currentUser));
return Promise.resolve();
}
updateCurrentUser(user) {
throw new Error("Method not implemented.");
}
useDeviceLanguage() { }
verifyPasswordResetCode(code) {
throw new Error("Method not implemented.");
}
}
exports.MockAuth = MockAuth;
//# sourceMappingURL=auth.js.map