Skip to content

Instantly share code, notes, and snippets.

@nikitph
Created September 16, 2017 00:17
Show Gist options
  • Save nikitph/004782604d8981a0b48162cc062c6a86 to your computer and use it in GitHub Desktop.
Save nikitph/004782604d8981a0b48162cc062c6a86 to your computer and use it in GitHub Desktop.
/**
* Created by Omkareshwar on 9/15/17.
*/
import firebase from 'firebase'
import config from './FirebaseConfig'
import ReduxSagaFirebase from 'redux-saga-firebase';
const mapp = firebase.initializeApp(config);
export const reduxSagaFirebase = new ReduxSagaFirebase(mapp);
const FirebaseHelper = fb = {};
fb.ref = (endpoint) => firebase.database().ref(endpoint);
// Using set() overwrites data at the specified location, including any child nodes.
fb.set = (endpoint, data, callback) => {
firebase.database().ref(endpoint).set(data, callback);
};
// The listener receives a snapshot that contains the data at the
// specified location in the database at the time of the event.
fb.on = (endpoint, callback) => {
const ref = firebase.database().ref(endpoint)
ref.on('value', callback);
return ref;
};
// Same as on(), but only fires callback once
fb.once = (endpoint, callback) => {
firebase.database().ref(endpoint).once('value').then(callback);
};
fb.off = (ref, callback) => {
if (ref) ref.off('value', callback);
};
// Github Login (via redirect)
// https://firebase.google.com/docs/auth/web/github-auth
fb.githubLogin = (success, error) => {
// Create an instance of the GitHub provider object
const provider = new firebase.auth.GithubAuthProvider();
// Grant read/write access to profile info
provider.addScope('user');
// Sign in with redirect
firebase.auth().signInWithRedirect(provider);
};
// Retrieve the GitHub provider's OAuth token
// (do this on page load to check if we're logged in)
fb.getGithubToken = (success, error) => {
firebase.auth().getRedirectResult().then(success).catch(error);
};
fb.githubLoginWithToken = (token, success, error) => {
const credential = firebase.auth.GithubAuthProvider.credential(token);
firebase.auth().signInWithCredential(credential).then(success).catch(error);
}
export default FirebaseHelper;
export const auth = firebase.auth();
export const storage = firebase.storage();
export const database = firebase.database();
export function createUserWithEmailAndPassword (email, password, callback) {
firebase.auth().createUserWithEmailAndPassword(email, password)
.then((message) => {
callback(true)
})
.catch((error) => {
callback(error);
})
}
export function signInWithEmailAndPassword (email, password, callback) {
console.tron.log(password);
firebase.auth().signInWithEmailAndPassword(email.toString(), '123456')
.then((message) => {
callback(true)
})
.catch((error) => {
callback(error);
})
}
export function resetPassword (email, callback) {
firebase.auth().sendPasswordResetEmail(email)
.then((message) => {
callback({
code: 'auth/success',
message: 'Reset Email Successfully sent. Please check your inbox for further instructions'
})
})
.catch((error) => {
callback(error);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment