-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathauth.js
More file actions
193 lines (158 loc) · 4.37 KB
/
auth.js
File metadata and controls
193 lines (158 loc) · 4.37 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
191
192
193
import {all, takeEvery, take, put, apply, call} from 'redux-saga/effects'
import {eventChannel} from 'redux-saga'
import {appName} from '../config'
import {createSelector} from 'reselect'
import {Record} from 'immutable'
import firebase from 'firebase'
import {replace} from 'react-router-redux'
/**
* Constants
* */
export const moduleName = 'auth'
const prefix = `${appName}/${moduleName}`
export const SIGN_IN_REQUEST = `${prefix}/SIGN_IN_REQUEST`
export const SIGN_IN_START = `${prefix}/SIGN_IN_START`
export const SIGN_IN_SUCCESS = `${prefix}/SIGN_IN_SUCCESS`
export const SIGN_IN_ERROR = `${prefix}/SIGN_IN_ERROR`
export const SIGN_UP_REQUEST = `${prefix}/SIGN_UP_REQUEST`
export const SIGN_UP_START = `${prefix}/SIGN_UP_START`
export const SIGN_UP_SUCCESS = `${prefix}/SIGN_UP_SUCCESS`
export const SIGN_UP_ERROR = `${prefix}/SIGN_UP_ERROR`
export const SIGN_OUT = `${prefix}/SIGN_OUT`
/**
* Reducer
* */
export const ReducerRecord = Record({
user: null,
loading: false,
error: null
})
export default function reducer(state = new ReducerRecord(), action) {
const {type, payload} = action
switch (type) {
case SIGN_IN_START:
case SIGN_UP_START:
return state
.set('error', null)
.set('loading', true)
case SIGN_IN_SUCCESS:
case SIGN_UP_SUCCESS:
return state
.set('loading', false)
.set('user', payload.user)
case SIGN_IN_ERROR:
case SIGN_UP_ERROR:
return state
.set('loading', false)
.set('error', payload.error.message)
case SIGN_OUT:
return state
.set('user', null)
default:
return state
}
}
/**
* Selectors
* */
export const stateSelector = state => state[moduleName]
export const userSelector = createSelector(stateSelector, state => state.user)
export const errorSelector = createSelector(stateSelector, state => state.error)
export const loadingSelector = createSelector(stateSelector, state => state.loading)
/**
* Action Creators
* */
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
};
}
// firebase.auth().onAuthStateChanged(user => {
// if (user) window.store.dispatch({
// type: SIGN_IN_SUCCESS,
// payload: { user }
// })
// })
/**
* Sagas
*/
export const signUpSaga = function * () {
while (true) {
const action = yield take(SIGN_UP_REQUEST)
const {email, password} = action.payload
yield put({
type: SIGN_UP_START
})
try {
const auth = firebase.auth()
yield call([auth, auth.createUserWithEmailAndPassword], email, password)
} catch (error) {
yield put({
type: SIGN_UP_ERROR,
payload: {error}
})
}
}
}
export const signInSaga = function * (action) {
const { email, password } = action.payload
yield put({
type: SIGN_IN_START
})
try {
const auth = firebase.auth()
yield apply(auth, auth.signInWithEmailAndPassword, [email, password])
} catch (error) {
yield put({
type: SIGN_IN_ERROR,
payload: { error }
})
}
}
const createSocket = () => eventChannel(emit => {
const callback = user => emit(user || 'null')
return firebase.auth().onAuthStateChanged(callback)
})
export function * watchStatusChangeSaga() {
const chanel = yield call(createSocket)
while (true) {
const user = yield take(chanel)
if (user !== 'null') {
yield put({
type: SIGN_IN_SUCCESS,
payload: { user }
})
yield put(replace('/events'))
} else {
yield put(replace('/auth/sign-in'))
}
}
}
export function * singOutSaga() {
const auth = firebase.auth();
try {
yield call([auth, auth.signOut])
} catch (error) {
console.log('SignOut Error -->', error)
}
}
export const saga = function * () {
yield all([
takeEvery(SIGN_IN_REQUEST, signInSaga),
signUpSaga(),
takeEvery(SIGN_OUT, singOutSaga),
watchStatusChangeSaga()
])
}