-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathauth.js
More file actions
156 lines (128 loc) · 3.73 KB
/
auth.js
File metadata and controls
156 lines (128 loc) · 3.73 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
import firebase from 'firebase'
import {appName} from '../config'
import {Record} from 'immutable'
//import store from '../redux'
import {all, cps, 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_SUCCESS = `${appName}/${moduleName}/SIGN_IN_SUCCESS`
export const SIGN_IN_REQUEST = `${appName}/${moduleName}/SIGN_IN_REQUEST`
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`
export const AUTH = firebase.auth()
//console.log('---', ReducerRecord)
export default function reducer(state = new ReducerRecord(), action) {
const {type, payload, error} = action
switch (type) {
case SIGN_UP_REQUEST:
return state.set('loading', true)
case SIGN_IN_SUCCESS:
return state
.set('loading', false)
.set('user', payload.user)
.set('error', null)
case SIGN_UP_ERROR:
return state
.set('loading', false)
.set('error', error)
case SIGN_OUT_SUCCESS:
return new ReducerRecord()
default:
return state
}
}
export function signUp(email, password) {
return {
type: SIGN_UP_REQUEST,
payload: {email, password}
}
}
export function signIn(email, password) {
return {
type: SIGN_IN_REQUEST,
payload: {email, password}
}
}
export function signOut() {
return {
type: SIGN_OUT_REQUEST
}
}
export const signUpSaga = function * () {
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}
})
} catch (error) {
yield put({
type: SIGN_UP_ERROR,
error
})
}
}
}
export const signInSaga = function * () {
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('/admin/people'))
} catch (error) {
yield put({
type: SIGN_IN_ERROR,
error
})
}
}
}
export const watchStatusChange = function * () {
try {
yield cps([AUTH, AUTH.onAuthStateChanged])
} catch (user) {
yield put({
type: SIGN_IN_SUCCESS,
payload: {user}
})
}
}
export const signOutSaga = function * () {
try {
yield call([AUTH, AUTH.signOut])
yield put({
type: SIGN_OUT_SUCCESS
})
yield put(push('/auth/signin'))
} catch (_) {
}
}
export const saga = function * () {
yield all([
signUpSaga(),
signInSaga(),
watchStatusChange(),
takeEvery(SIGN_OUT_REQUEST, signOutSaga),
])
}