Skip to content

Commit

Permalink
chore(Formatting): Add prettier config file .prettierr and reformat…
Browse files Browse the repository at this point in the history
…ted existing files

i
Co-authored-by: Keith Brewster <kbrewster@tribalscale.com>
  • Loading branch information
Alice Mao committed Jul 11, 2019
1 parent 2eb90dd commit 06c285e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 110 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"printWidth": 80,
"semi": false
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
# Firebase 2-Factor Authentication

## Overview

The [Firebase Authentication SDK](https://firebase.google.com/docs/auth) does not currently support 2 Factor Authentication ("2FA"). The purpose of this library is to provide both a starter react-native application and wrapper functions around the Friebase Authentication SDK to streamline development for 2FA apps using Firebase.

## Getting Started

_This section describes how to set up the project to develop and test, as well as how to use the project if it is an external package._

## Prerequisites

_What needs to be done before development, testing, and/or usage can occur._

## Installing

_How to install and run the project within a given environment._

## Documentation

_Links to other supporting documents for the project._

## How to Use

_Can include a general overview of how to use the project, code samples are strongly recommended here. Being as specific as possible is helpful to users, including a description of the API._

## Running the Tests

_Steps to run the test suite of the project._

## Deployment

_Steps for new development code get deployed to production._

## Technologies Used

_A list of the major technologies used in the project._

## Contribute

_Description of how other developers can contribute to this project (i.e. pull requests)._

## Versioning

_Description of how to handle versioning of the project and which versioning pattern is being followed._

## Authors

_Who is responsible for the project._
43 changes: 20 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const firebase = require("firebase");
const fb = require("./src/firebase");
const firebase = require('firebase')
const fb = require('./src/firebase')

const requiredConfigFields = ["apiKey", "authDomain", "projectId", "appId"];
const requiredConfigFields = ['apiKey', 'authDomain', 'projectId', 'appId']

const Firebase2FA = () => {
/**
Expand All @@ -12,28 +12,28 @@ const Firebase2FA = () => {
* @param {string} config.appId the generated app ID for the firebase project
*/
const initializeWithConfig = config => {
const appHasBeenInitialized = fb.checkInitializationState();
const appHasBeenInitialized = fb.checkInitializationState()

if (!config) {
throw new Error("Please provide a firebase app configuration.");
throw new Error('Please provide a firebase app configuration.')
}
if (appHasBeenInitialized) {
throw new Error("Firebase has already been initialized.");
throw new Error('Firebase has already been initialized.')
}
requiredConfigFields.forEach(field => {
if (!config[field]) {
throw new Error(`${field} value is required.`);
throw new Error(`${field} value is required.`)
}
});
})

try {
fb.initializeFirebase(config);
fb.initializeFirebase(config)
} catch (err) {
console.error(err);
console.error(err)
}
};
}

const initializeWithInstance = instance => {};
const initializeWithInstance = instance => {}

/**
* This function attempts to create a new firebase user with email/password
Expand All @@ -42,24 +42,21 @@ const Firebase2FA = () => {
* @param {boolean} [sendVerificationEmail=false] whether or not to send a verification email upon account creation
*/
const createAccountWithEmail = ({
email = "",
password = "",
sendVerificationEmail = false
email = '',
password = '',
sendUserVerification = false
}) => {
if (!email || !password) {
throw new Error(`${!email ? "Email" : "Password"} is required.`);
throw new Error(`${!email ? 'Email' : 'Password'} is required.`)
}
return fb.createUser({ email, password });
// .then(getUser)
// .then(sendVerificationEmail)
// .catch(getErrorMessage);
};
return fb.createUser({ email, password }).catch(getErrorMessage)
}

return {
createAccountWithEmail,
initializeWithConfig,
initializeWithInstance
};
};
}
}

module.exports = Firebase2FA();
159 changes: 79 additions & 80 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,96 @@
const firebase2FA = require("./index");
const fb = require("./src/firebase");
const firebase2FA = require('./index')
const fb = require('./src/firebase')

describe("Firebase2FA", () => {
describe("Initialization", () => {
test("it returns methods initializeWithConfig, initializeWithInstance", () => {
const actual = firebase2FA;
const methodsArray = ["initializeWithConfig", "initializeWithInstance"];
methodsArray.forEach(expected => expect(actual).toHaveProperty(expected));
});
describe('Firebase2FA', () => {
describe('Initialization', () => {
test('it returns methods initializeWithConfig, initializeWithInstance', () => {
const actual = firebase2FA
const methodsArray = ['initializeWithConfig', 'initializeWithInstance']
methodsArray.forEach(expected => expect(actual).toHaveProperty(expected))
})

test("initializeWithConfig fails when not provided a config object", () => {
const expected = /Please provide a firebase app configuration./;
const actual = firebase2FA.initializeWithConfig;
expect(actual).toThrow(expected);
});

["apiKey", "authDomain", "projectId", "appId"].forEach(key => {
test('initializeWithConfig fails when not provided a config object', () => {
const expected = /Please provide a firebase app configuration./
const actual = firebase2FA.initializeWithConfig
expect(actual).toThrow(expected)
})
;['apiKey', 'authDomain', 'projectId', 'appId'].forEach(key => {
const config = {
apiKey: "test",
authDomain: "test",
projectId: "test",
appId: "test"
};
delete config[key];
const actual = () => firebase2FA.initializeWithConfig(config);
apiKey: 'test',
authDomain: 'test',
projectId: 'test',
appId: 'test'
}
delete config[key]
const actual = () => firebase2FA.initializeWithConfig(config)

test(`intializeWithConfig fails when [${key}] config key is missing`, () => {
const expected = `${key} value is required.`;
expect(actual).toThrow(expected);
});
});
const expected = `${key} value is required.`
expect(actual).toThrow(expected)
})
})

test("initializeWithConfig fails when app has already been initialized", () => {
test('initializeWithConfig fails when app has already been initialized', () => {
const config = {
apiKey: "test",
authDomain: "test",
projectId: "test",
appId: "test"
};
firebase2FA.initializeWithConfig(config);
const actual = () => firebase2FA.initializeWithConfig(config);
const expected = /Firebase has already been initialized./;
expect(actual).toThrow(expected);
});
apiKey: 'test',
authDomain: 'test',
projectId: 'test',
appId: 'test'
}
firebase2FA.initializeWithConfig(config)
const actual = () => firebase2FA.initializeWithConfig(config)
const expected = /Firebase has already been initialized./
expect(actual).toThrow(expected)
})

test("initializeWithConfig succeeds when provided a valid config object", () => {
test('initializeWithConfig succeeds when provided a valid config object', () => {
const config = {
apiKey: "test",
authDomain: "test",
projectId: "test",
appId: "test"
};
const actual = () => firebase2FA.initializeWithConfig(config);
const expected = "function";
expect(typeof actual).toBe(expected);
});
});
apiKey: 'test',
authDomain: 'test',
projectId: 'test',
appId: 'test'
}
const actual = () => firebase2FA.initializeWithConfig(config)
const expected = 'function'
expect(typeof actual).toBe(expected)
})
})

describe("User", () => {
describe("Account Creation", () => {
test("createAccountWithEmail fails when email is not provided", () => {
describe('User', () => {
describe('Account Creation', () => {
test('createAccountWithEmail fails when email is not provided', () => {
const userObj = {
email: "",
password: "test"
};
const actual = () => firebase2FA.createAccountWithEmail(userObj);
const expected = /Email is required./;
expect(actual).toThrow(expected);
});
email: '',
password: 'test'
}
const actual = () => firebase2FA.createAccountWithEmail(userObj)
const expected = /Email is required./
expect(actual).toThrow(expected)
})

test("createAccountWithEmail fails when password is not provided", () => {
test('createAccountWithEmail fails when password is not provided', () => {
const userObj = {
email: "test",
password: ""
};
const actual = () => firebase2FA.createAccountWithEmail(userObj);
const expected = /Password is required./;
expect(actual).toThrow(expected);
});
email: 'test',
password: ''
}
const actual = () => firebase2FA.createAccountWithEmail(userObj)
const expected = /Password is required./
expect(actual).toThrow(expected)
})

test("createAccountWithEmail is successful when email & password are provided", () => {
test('createAccountWithEmail is successful when email & password are provided', () => {
const userObj = {
email: "test",
password: "testpassword"
};
email: 'test',
password: 'testpassword'
}

const spy = jest.spyOn(fb, "createUser");
spy.mockReturnValue("mocked");
const spy = jest.spyOn(fb, 'createUser')
spy.mockReturnValue('mocked')

firebase2FA.createAccountWithEmail(userObj);
expect(spy).toHaveBeenCalledWith(userObj);
spy.mockRestore();
});
});
});
});
firebase2FA.createAccountWithEmail(userObj)
expect(spy).toHaveBeenCalledWith(userObj)
spy.mockRestore()
})
})
})
})
14 changes: 7 additions & 7 deletions src/firebase.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const firebase = require("firebase");
const firebase = require('firebase')

const initializeFirebase = config => {
firebase.initializeApp(config);
};
firebase.initializeApp(config)
}

const createUser = async ({ email, password }) => {
return await firebase.auth().createUserWithEmailAndPassword(email, password);
};
return await firebase.auth().createUserWithEmailAndPassword(email, password)
}

const checkInitializationState = () => firebase.apps.length > 0;
const checkInitializationState = () => firebase.apps.length > 0

module.exports = {
checkInitializationState,
createUser,
initializeFirebase
};
}

0 comments on commit 06c285e

Please sign in to comment.