user.js 5.31 KB
import {all, fork, takeEvery, call, put, delay} from 'redux-saga/effects';
import axios from 'axios';
import * as SecureStore from 'expo-secure-store';
import AsyncStorage from '@react-native-community/async-storage';
import {
    signUpSaga,
    loginSaga,
    loadMeSaga,
    updateUserInfoSaga,
    logoutSaga,

    loginReducer,
    loadMeReducer,
    updateUserInfoReducer,
    logoutReducer,
} from '../reducers/user';
import {
    setInfoReducer,
    resetInfoReducer,
} from '../reducers/info';
import env from "../env";

function signUpAPI(data) {
    const {email, nickName, password} = data;
    return axios.post(`${env.HTTP_OR_HTTPS}://${env.HOST}:${env.PORT}/user/signUp`, {
        email, nickName, password
    }, {
        withCredentials: true
    });
}

function* signUp(action) {
    try {
        const res = yield call(signUpAPI, action.data);
        const {info} = res.data;
        yield put({
            type: setInfoReducer,
            data: {info}
        });
        yield delay(5000);
        yield put({
            type: resetInfoReducer,
        })
    } catch (e) {
        console.error(e);
        if (e.response && e.response.data.info) {
            const {info} = e.response.data;
            yield put({
                type: setInfoReducer,
                data: {info}
            });
            yield delay(3000);
            yield put({
                type: resetInfoReducer,
            })
        }
    }
}

function* signUpSagaWatch() {
    yield takeEvery(signUpSaga, signUp);
}

async function loginAPI(data) {
    const {email, password} = data;
    const res = await axios.post(`${env.HTTP_OR_HTTPS}://${env.HOST}:${env.PORT}/user/login`, {
        email, password,
    }, {
        withCredentials: true
    });
    const {user} = res.data;

    const cookie = res.headers['set-cookie'][0];
    if (SecureStore.isAvailableAsync()) {
        await SecureStore.setItemAsync('cookie', cookie);
    } else {
        await AsyncStorage.setItem('cookie', cookie);
    }
    axios.defaults.headers.Cookie = cookie;

    return {user};
}

function* login(action) {
    try {
        const {user} = yield call(loginAPI, action.data);
        yield put({
            type: loginReducer,
            data: {user}
        });
    } catch (e) {
        console.error(e);
        if (e.response && e.response.data.info) {
            const {info} = e.response.data;
            yield put({
                type: setInfoReducer,
                data: {info}
            });
            yield delay(3000);
            yield put({
                type: resetInfoReducer,
            })
        }
    }
}

function* loginSagaWatch() {
    yield takeEvery(loginSaga, login);
}

function loadMeAPI(data) {
    return axios.get(`${env.HTTP_OR_HTTPS}://${env.HOST}:${env.HOST}/user/loadMe`, {
        withCredentials: true
    });
}

function* loadMe(action) {
    try {
        const res = yield call(loadMeAPI, action.data);
        const {user} = res.data;
        yield put({
            type: loadMeReducer,
            data: {user}
        });
    } catch (e) {
        console.error(e);
        if (e.response && e.response.data.info) {
            const {info} = e.response.data;
            yield put({
                type: setInfoReducer,
                data: {info}
            });
            yield delay(3000);
            yield put({
                type: resetInfoReducer,
            })
        }
    }
}

function* loadMeSagaWatch() {
    yield takeEvery(loadMeSaga, loadMe);
}

function logoutAPI(data) {
    return axios.get(`${env.HTTP_OR_HTTPS}://${env.HOST}:${env.PORT}/user/logout`, {
        withCredentials: true
    });
}

function* logout(action) {
    try {
        yield call(logoutAPI, action.data);
        yield put({
            type: logoutReducer,
        });
    } catch (e) {
        console.error(e);
        if (e.response && e.response.data.info) {
            const {info} = e.response.data;
            yield put({
                type: setInfoReducer,
                data: {info}
            });
            yield delay(3000);
            yield put({
                type: resetInfoReducer,
            })
        }
    }
}

function* logoutSagaWatch() {
    yield takeEvery(logoutSaga, logout);
}

function updateUserInfoAPI(data) {
    const {nickName} = data;
    return axios.post(`${env.HTTP_OR_HTTPS}://${env.HOST}:${env.PORT}/user/updateUserInfo`, {
        nickName,
    }, {
        withCredentials: true
    });
}

function* updateUserInfo(action) {
    try {
        const res = yield call(updateUserInfoAPI, action.data);
        const {user} = res.data;
        yield put({
            type: updateUserInfoReducer,
            data: {user}
        });
    } catch (e) {
        console.error(e);
        if (e.response && e.response.data.info) {
            const {info} = e.response.data;
            yield put({
                type: setInfoReducer,
                data: {info}
            });
            yield delay(3000);
            yield put({
                type: resetInfoReducer,
            })
        }
    }
}

function* updateUserInfoSagaWatch() {
    yield takeEvery(updateUserInfoSaga, updateUserInfo);
}

export default function* userSaga() {
    yield all([
        fork(signUpSagaWatch),
        fork(loginSagaWatch),
        fork(loadMeSagaWatch),
        fork(updateUserInfoSagaWatch),
        fork(logoutSagaWatch),
    ]);
}