aboutsummaryrefslogtreecommitdiff
path: root/src/actions/firebaseAuth.js
blob: 8f12c73fe8ab339c624cbada802312647386bdcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { auth } from '../firebase.js';

export const AUTH_SUCCESS   = 'AUTH_SUCCESS';
export const AUTH_FAIL      = 'AUTH_FAIL';
export const AUTH_SIGN_OUT  = 'AUTH_SIGN_OUT';
export const UPDATE_ADMIN   = 'UPDATE_ADMIN';

export const authFail = (errorCode) => {
  alert(errorCode);
  return {
    type:       AUTH_FAIL,
    payload:    false,
    uid:        "",
    userEmail:  ""
  }
}

export const authSuccess = (_user) => {
  alert('Success. Checkout the other pages to see what you can do!');
  return {
    type:       AUTH_SUCCESS,
    payload:    true,
    uid:        _user.uid,
    userEmail:  _user.email
  }
}

export const authSignOut = () => {
  return {
    type:       AUTH_SIGN_OUT,
    payload:    false,
    uid:        "",
    userEmail:  ""
  }
}

//Middleware to dispatches, normally triggered by user.

export const createAccount = (_email, _password, divison) => (dispatch) => {
  auth.createUserWithEmailAndPassword(_email, _password).then(() => {
    dispatch(setUserData(_email, _password, divison));
  })
  .catch((error) => {
    alert(error.code + ": " + error.message);
  });
}
import {
  setUserData,
  fetchDivison,
  snapshotHours,
  snapshotRegisteredCompetitions
} from './firebaseFirestore.js';

import {
  adminListener,
  adminClose,
  updateUserInfo
} from './firebaseAdmin.js'

export const signIn  = (_email, _password) => (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));
    dispatch(fetchDivison());
    dispatch(snapshotHours())
    dispatch(snapshotRegisteredCompetitions());
    //Admin controls
    if( user.uid  === '4MYYFCFjQtOWxu2kQX7RtvEFzMv2' || //Bridget Denzer
        user.uid  === 'sAVjlnSAETaP5VtTKGhfBKHKeQF2' || //Michael Foiani
        user.uid  === 'W3eN6iycdlYpKYC4lF7qOLr1tEk2' || //Jessica Coombs
        user.uid  === 'Z0cGfWLHSoU7KWmsTOxwt0wvXti2'    //Britney Cankar
      ) {
      dispatch(adminListener());
    }
  })
  .catch((error) => {
    dispatch(authFail(error.code));
  });
}

export const signOut = () => (dispatch) => {
  auth.signOut().then(() => {
    dispatch(authSignOut());
    dispatch(adminClose());
  });
}