forked from NUSvigate/NUSvigate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
82 lines (71 loc) · 2 KB
/
user.js
File metadata and controls
82 lines (71 loc) · 2 KB
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
import firebaseDb, { db } from '../firebaseDb';
import { Alert } from 'react-native';
export const UPDATE_EMAIL = 'UPDATE_EMAIL'
export const UPDATE_PASSWORD = 'UPDATE_PASSWORD'
export const UPDATE_NAME = 'UPDATE_NAME'
export const LOGIN = 'LOGIN'
export const SIGNUP = 'SIGNUP'
export const updateEmail = email => {
return {
type: UPDATE_EMAIL,
payload: email
}
}
export const updatePassword = password => {
return {
type: UPDATE_PASSWORD,
payload: password
}
}
export const updateName = name => {
return {
type: UPDATE_NAME,
payload: name
}
}
export const login = () => {
return async (dispatch, getState) => {
try {
const { email, password } = getState().user
const response = await firebaseDb.auth().signInWithEmailAndPassword(email, password)
dispatch(getUser(response.user.uid))
} catch (e) {
alert(e)
}
}
}
export const getUser = uid => {
return async (dispatch, getState) => {
try {
const user = await db
.collection('users')
.doc(uid)
.get()
dispatch({ type: LOGIN, payload: user.data() })
} catch (e) {
alert(e)
}
}
}
export const signup = () => {
return async (dispatch, getState) => {
try {
const { email, password, name } = getState().user
const response = await firebaseDb.auth().createUserWithEmailAndPassword(email, password)
if (response.user.uid) {
const user = {
uid: response.user.uid,
email: email,
name: name
}
db.collection('users')
.doc(response.user.uid)
.set(user)
dispatch({ type: SIGNUP, payload: user });
Alert.alert('Sign Up Successful!');
}
} catch (e) {
alert(e)
}
}
}