Skip to content
Open

hw5 #28

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import firebase from 'firebase'

export const appName = "advreact-21-08"
export const appName = "advreact-21-081"
export const firebaseConfig = {
apiKey: "AIzaSyDjA6CeIHuni5lNm4ML1b-TSxJltsYUO8g",
authDomain: `${appName}.firebaseapp.com`,
databaseURL: `https://${appName}.firebaseio.com`,
projectId: appName,
storageBucket: `${appName}.appspot.com`,
messagingSenderId: "789814589283"
apiKey: "AIzaSyC0hcpDsBhr85usoHIwloBeYbkDHeUcqKo",
authDomain: "advreact-21-081.firebaseapp.com",
databaseURL: "https://advreact-21-081.firebaseio.com",
projectId: "advreact-21-081",
storageBucket: "advreact-21-081.appspot.com",
messagingSenderId: "450237050585"
}

firebase.initializeApp(firebaseConfig)
147 changes: 91 additions & 56 deletions src/ducks/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ 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 {all, cps, call, put, take, takeEvery, spawn, cancel, fork} from 'redux-saga/effects'
import {delay, eventChannel} from 'redux-saga'
import {push} from 'react-router-redux'

export 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`
Expand All @@ -20,62 +19,50 @@ export const SIGN_IN_ERROR = `${appName}/${moduleName}/SIGN_IN_ERROR`
export const SIGN_IN_SUCCESS = `${appName}/${moduleName}/SIGN_IN_SUCCESS`
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_UP_REQUEST:
case SIGN_IN_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:
case SIGN_IN_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 * () {
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],
Expand All @@ -93,13 +80,66 @@ export const signUpSaga = function * () {
}
}
}

export const signInSaga = function * () {
// const createAuthSocket = (payload) => eventChannel(emit => {
// const unsubscribe = firebase.auth().onAuthStateChanged(
// user => emit({user}),
// error => emit({error})
// )
// return unsubscribe;
// })

const createAuthSocket = () => eventChannel(emmit => {
const auth = firebase.auth()
const callback = (user) => emmit({user})
auth.onAuthStateChanged(callback)
return () => {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

зачем ты возвращаешь пустую функцию? Такое тебя в результате приведет к неприятным багам. auth.onAuthStateChanged возвращает нужный колбек

})

export const checkAuth = function *(payload) {
const auth = firebase.auth();
try {
yield call(
[auth, auth.signInWithEmailAndPassword],
payload.email, payload.password
)
}
catch (error) {
yield put({
type: SIGN_IN_ERROR
})
}
}

export const realtimeSync = function *(payload) {
const socket = yield call(checkAuth,payload)
const socketChannel = yield call(createAuthSocket, socket)
while(true) {
const {user} = yield take(socketChannel)
if(user) {
yield put({
type: SIGN_IN_SUCCESS,
payload: {user}
})
}
}
}

export const cancellableSync = function *() {
let task
while (true) {
const action = yield take(SIGN_IN_REQUEST)
const {payload} = yield take(SIGN_IN_REQUEST)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не очень понял, что тего ты хочешь достичь

if (payload) {
task = yield fork(realtimeSync, payload)
} else if (task) {
yield cancel(task)
}
}
}

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],
Expand All @@ -119,26 +159,25 @@ export const signInSaga = function * () {
}

/*
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 * () {
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])
Expand All @@ -149,36 +188,32 @@ export const watchStatusChange = function * () {
})
}
}

/*
firebase.auth().onAuthStateChanged(user => {
const store = require('../redux').default
store.dispatch({
type: SIGN_IN_SUCCESS,
payload: {user}
})
})

*/

export const signOutSaga = function * () {
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 * () {
export const saga = function *() {
yield spawn(cancellableSync);
yield all([
signUpSaga(),
signInSaga(),
// signInSaga(),
watchStatusChange(),
takeEvery(SIGN_OUT_REQUEST, signOutSaga)
])
Expand Down