-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathauth.js
More file actions
190 lines (162 loc) · 4.65 KB
/
auth.js
File metadata and controls
190 lines (162 loc) · 4.65 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import firebase from 'firebase'
import {appName} from '../config'
import {Record} from 'immutable'
import {all, call, put, take, takeEvery} from 'redux-saga/effects'
import {push} from 'react-router-redux'
const ReducerRecord = Record({
user: null,
error: null,
loading: false
})
export const moduleName = 'auth'
export const SIGN_UP_REQUEST = `${appName}/${moduleName}/SIGN_UP_REQUEST`
export const SIGN_UP_SUCCESS = `${appName}/${moduleName}/SIGN_UP_SUCCESS`
export const SIGN_UP_ERROR = `${appName}/${moduleName}/SIGN_UP_ERROR`
export const SIGN_IN_REQUEST = `${appName}/${moduleName}/SIGN_IN_REQUEST`
export const SIGN_IN_SUCCESS = `${appName}/${moduleName}/SIGN_IN_SUCCESS`
export const SIGN_IN_ERROR = `${appName}/${moduleName}/SIGN_IN_ERROR`
export const SIGN_OUT_REQUEST = `${appName}/${moduleName}/SIGN_OUT_REQUEST`
export const SIGN_OUT_SUCCESS = `${appName}/${moduleName}/SIGN_OUT_SUCCESS`
//console.log('---', ReducerRecord)
export default function reducer(state = new ReducerRecord(), action) {
const {type, payload, error} = action
switch (type) {
case SIGN_IN_REQUEST:
case SIGN_UP_REQUEST:
return state.set('loading', true)
case SIGN_IN_SUCCESS:
case SIGN_UP_SUCCESS:
return state
.set('loading', false)
.set('user', payload.user)
.set('error', null)
case SIGN_IN_ERROR:
case SIGN_UP_ERROR:
return state
.set('loading', false)
.set('error', error)
case SIGN_OUT_SUCCESS:
return new ReducerRecord()
default:
return state
}
}
export function signIn(email, password) {
return {
type: SIGN_IN_REQUEST,
payload: {email, password}
}
}
export function signUp(email, password) {
return {
type: SIGN_UP_REQUEST,
payload: {email, password}
}
}
export function signOut() {
return {
type: SIGN_OUT_REQUEST
}
}
export const signInSaga = function * () {
const auth = firebase.auth()
while (true) {
const action = yield take(SIGN_IN_REQUEST)
try {
const user = yield call(
[auth, auth.signInWithEmailAndPassword],
action.payload.email, action.payload.password
)
yield put({
type: SIGN_IN_SUCCESS,
payload: {user}
})
yield put(push('/people'))
} catch (error) {
yield put({
type: SIGN_IN_ERROR,
error
})
}
}
}
export const signUpSaga = function * () {
const auth = firebase.auth()
while (true) {
const action = yield take(SIGN_UP_REQUEST)
try {
const user = yield call(
[auth, auth.createUserWithEmailAndPassword],
action.payload.email, action.payload.password
)
yield put({
type: SIGN_UP_SUCCESS,
payload: {user}
})
yield put(push('/people'))
} catch (error) {
yield put({
type: SIGN_UP_ERROR,
error
})
}
}
}
/*
export function signUp(email, password) {
return (dispatch) => {
dispatch({
type: SIGN_UP_REQUEST
})
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(user => dispatch({
type: SIGN_UP_SUCCESS,
payload: {user}
}))
.catch(error => dispatch({
type: SIGN_UP_ERROR,
error
}))
}
}
*/
// export const watchStatusChange = function * () {
// const auth = firebase.auth()
// try {
// yield cps([auth, auth.onAuthStateChanged])
// } catch (user) {
// yield put({
// type: SIGN_IN_SUCCESS,
// payload: {user}
// })
// }
// }
// NOTE: We don't need to listen to auth changes if we already dispatching user when he is signing in/up
/*
firebase.auth().onAuthStateChanged(user => {
const store = require('../redux').default
store.dispatch({
type: SIGN_IN_SUCCESS,
payload: {user}
})
})
*/
export const signOutSaga = function * () {
const auth = firebase.auth()
try {
yield call([auth, auth.signOut])
yield put({
type: SIGN_OUT_SUCCESS
})
yield put(push('/auth/signin'))
} catch (_) {
}
}
export const saga = function * () {
yield all([
signInSaga(),
signUpSaga(),
// watchStatusChange(),
takeEvery(SIGN_OUT_REQUEST, signOutSaga)
])
}