diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/actions/firebase.js | 135 | ||||
-rw-r--r-- | src/actions/firebaseAuth.js | 140 | ||||
-rw-r--r-- | src/firebase.js | 5 | ||||
-rw-r--r-- | src/reducers/firebaseAuth.js | 41 |
4 files changed, 186 insertions, 135 deletions
diff --git a/src/actions/firebase.js b/src/actions/firebase.js index db6edb0..6dca3d2 100644 --- a/src/actions/firebase.js +++ b/src/actions/firebase.js @@ -1,4 +1,4 @@ -import { firebase, firestore } from '../firebase.js'; +import { firestore, auth } from '../firebase.js'; //Start Firbase Auth export const AUTH_FAIL = 'AUTH_FAIL'; @@ -13,141 +13,8 @@ export const ADMIN_CONTROLS = 'ADMIN_CONTROLS'; export const UPDATE_ADMIN = 'UPDATE_ADMIN'; export const SET_USER_DATA = 'SET_USER_DATA'; -const auth = firebase.auth(); -export const createAccount = (_email, _password, divison) => (dispatch) => { - firebase.auth().createUserWithEmailAndPassword(_email, _password).then(() => { - dispatch(signIn(_email, _password, divison)); - }) - .catch((error) => { - // Handle Errors here. - alert(error.code + ": " + error.message); - }); -} - -export const signIn = (_email, _password, divison) => (dispatch) => { - auth.signInWithEmailAndPassword(_email, _password).then(() => { - var user = auth.currentUser; - /* User is signed in. - var displayName = user.displayName; - var email = user.email; - var emailVerified = user.emailVerified; - var photoURL = user.photoURL; - var isAnonymous = user.isAnonymous; - var uid = user.uid; - var providerData = user.providerData; - */ - dispatch(authSuccess(user)); - if(divison) { - dispatch(setUserData(divison)); - } - dispatch(fetchDivison()); - dispatch(snapshotHours()) - dispatch(snapshotRegisteredCompetitions()); - //Admin controls - if( user.uid === 'rxKROQAukzchWuueDLwA9c0YmsT2' || //Lucy Wood - user.uid === 'sAVjlnSAETaP5VtTKGhfBKHKeQF2' //Michael Foiani - ) - { - dispatch(adminListener()); - } - }) - .catch((error) => { - dispatch(authFail(error.code)); - }); - -} - -export const setUserData = (_divison) => (dispatch, getState) => { - const uid = getState().firebase.uid; - var docRef = firestore.collection('users').doc(uid); - docRef.set({ - hours: 0, - divison: _divison - }).catch((error) => { - console.log(error); - }) -} - -export const adminListener = () => (dispatch, getState) => { - document.onkeyup = function(e) { - if(e.altKey && e.which == 65) { - var docRef = firestore.collection('keys').doc('adminKey'); - docRef.get().then((doc) => { - if(prompt('Enter admin password') == doc.data().password) { - dispatch(adminControls()); - } - }); - } - } -} - -export const authFail = (errorCode) => { - alert(errorCode); - return { - type: AUTH_FAIL, - payload: false, - uid: null - } -} - -export const authSuccess = (_user) => { - alert('Sign In Success'); - return { - type: AUTH_SUCCESS, - payload: true, - uid: _user.uid, - userEmail: _user.email - } -} - -export const fetchDivison = () => (dispatch, getState) => { - const uid = getState().firebase.uid; - var docRef = firestore.collection('users').doc(uid); - - docRef.get().then((doc) => { - dispatch(updateDivison(doc.data().divison)); - }); -} - -export const updateDivison = (divison) => { - return { - type: UPDATE_DIVISON, - payload: divison - } -} - -export const adminControls = () => (dispatch) => { - dispatch(updateAdmin()); - dispatch(snapshotAdminRequests()); - dispatch(snapshotAdminCompList()); -} - -export const updateAdmin = () => { - return { - type: UPDATE_ADMIN, - payload: true - } -} -export const signOut = () => (dispatch) => { - auth.signOut().then(() => { - dispatch(authSignOut()); - }); -} - -export const authSignOut = () => { - return { - type: AUTH_SIGN_OUT, - payload: false, - code: "Signed Out User", - uid: "", - userEmail: "", - isAdmin: false, - requests: [], - compList: [] - } -} //End Firebase Auth //Start Firebase Firestore diff --git a/src/actions/firebaseAuth.js b/src/actions/firebaseAuth.js new file mode 100644 index 0000000..9783d8a --- /dev/null +++ b/src/actions/firebaseAuth.js @@ -0,0 +1,140 @@ +import { auth } from '../firebase.js'; + +export const AUTH_SUCCESS = 'AUTH_SUCCESS'; +export const UPDATE_DIVISON = 'UPDATE_DIVISON'; +export const AUTH_SIGN_OUT = 'AUTH_SIGN_OUT'; +export const UPDATE_ADMIN = 'UPDATE_ADMIN'; + +export const createAccount = (_email, _password, divison) => (dispatch) => { + auth.createUserWithEmailAndPassword(_email, _password).then(() => { + dispatch(signIn(_email, _password, divison)); + }) + .catch((error) => { + // Handle Errors here. + alert(error.code + ": " + error.message); + }); +} + +export const signIn = (_email, _password, divison) => (dispatch) => { + auth.signInWithEmailAndPassword(_email, _password).then(() => { + var user = auth.currentUser; + /* User is signed in. + var displayName = user.displayName; + var email = user.email; + var emailVerified = user.emailVerified; + var photoURL = user.photoURL; + var isAnonymous = user.isAnonymous; + var uid = user.uid; + var providerData = user.providerData; + */ + dispatch(authSuccess(user)); + if(divison) { + dispatch(setUserData(divison)); + } + dispatch(fetchDivison()); + dispatch(snapshotHours()) + dispatch(snapshotRegisteredCompetitions()); + //Admin controls + if( user.uid === 'rxKROQAukzchWuueDLwA9c0YmsT2' || //Lucy Wood + user.uid === 'sAVjlnSAETaP5VtTKGhfBKHKeQF2' //Michael Foiani + ) + { + dispatch(adminListener()); + } + }) + .catch((error) => { + dispatch(authFail(error.code)); + }); + +} + +export const setUserData = (_divison) => (dispatch, getState) => { + const uid = getState().firebase.uid; + var docRef = firestore.collection('users').doc(uid); + docRef.set({ + hours: 0, + divison: _divison + }).catch((error) => { + console.log(error); + }) +} + +export const adminListener = () => (dispatch, getState) => { + document.onkeyup = function(e) { + if(e.altKey && e.which == 65) { + var docRef = firestore.collection('keys').doc('adminKey'); + docRef.get().then((doc) => { + if(prompt('Enter admin password') == doc.data().password) { + dispatch(adminControls()); + } + }); + } + } +} + +export const authFail = (errorCode) => { + alert(errorCode); + return { + type: AUTH_FAIL, + payload: false, + uid: null + } +} + +export const authSuccess = (_user) => { + alert('Sign In Success'); + return { + type: AUTH_SUCCESS, + payload: true, + uid: _user.uid, + userEmail: _user.email + } +} + +export const fetchDivison = () => (dispatch, getState) => { + const uid = getState().firebase.uid; + var docRef = firestore.collection('users').doc(uid); + + docRef.get().then((doc) => { + dispatch(updateDivison(doc.data().divison)); + }); +} + +export const updateDivison = (divison) => { + return { + type: UPDATE_DIVISON, + payload: divison + } +} + +export const adminControls = () => (dispatch) => { + dispatch(updateAdmin()); + dispatch(snapshotAdminRequests()); + dispatch(snapshotAdminCompList()); +} + +export const updateAdmin = () => { + return { + type: UPDATE_ADMIN, + payload: true + } +} + +export const signOut = () => (dispatch) => { + auth.signOut().then(() => { + dispatch(authSignOut()); + }); +} + +export const authSignOut = () => { + return { + type: AUTH_SIGN_OUT, + payload: false, + code: "Signed Out User", + uid: "", + userEmail: "", + isAdmin: false, + requests: [], + compList: [] + } +} diff --git a/src/firebase.js b/src/firebase.js index 690c2c1..26ae172 100644 --- a/src/firebase.js +++ b/src/firebase.js @@ -1,4 +1,4 @@ -export const firebase = window.firebase; +const firebase = window.firebase; // Initialize Firebase var config = { apiKey: "AIzaSyC68wOeR88PPiw5VqzUYs7lm4ewyJgHf20", @@ -10,6 +10,9 @@ var config = { }; firebase.initializeApp(config); +//export auth +export const auth = firebase.auth(); + //set settings and export firestore export const firestore = firebase.firestore(); const settings = {timestampsInSnapshots: true}; diff --git a/src/reducers/firebaseAuth.js b/src/reducers/firebaseAuth.js new file mode 100644 index 0000000..ced6c1c --- /dev/null +++ b/src/reducers/firebaseAuth.js @@ -0,0 +1,41 @@ +/** +@license +Copyright (c) 2018 The Polymer Project Authors. All rights reserved. +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt +Code distributed by Google as part of the polymer project is also +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt +*/ + +import { + AUTH_SUCCESS, + AUTH_SIGN_OUT, + UPDATE_ADMIN +} +from '../actions/firebaseAuth.js'; + +const firebaseAuth = (state = {signedIn: false, uid: "", userEmail: ""}, action) => { + switch (action.type) { + case AUTH_SUCCESS: + return { + ...state, + signedIn: action.payload, + uid: action.uid, + userEmail: action.userEmail + }; + break; + + case AUTH_SIGN_OUT: + return { + ...state, + signedIn: action.payload + } + break; + + default: + return state; + } +}; + +export default firebaseAuth; |