728x90
1. 회원가입 구현하기 적용 순서
- firebase.js에 만들어둔 auth 가져오기
- 리덕스에서 signupFB 함수 만들기
- auth.createUserWithEmailAndPassword()로 가입 시키기
- Signup 컴포넌트에서 signupFB를 호출!
- 가입한 후, display_name 바로 업데이트하기
- 사용자 정보 업데이트 후에 메인 페이지로 이동하기
2. user.js
import { createAction, handleActions } from "redux-actions";
import { produce } from "immer";
import { setCookie, getCookie, deleteCookie } from "../../shared/Cookie";
import { auth } from "../../shared/firebase";
// actions
const LOG_OUT = "LOG_OUT";
const GET_USER = "GET_USER";
const SET_USER = "SET_USER";
// action creators
const logOut = createAction(LOG_OUT, (user) => ({ user }));
const getUser = createAction(GET_USER, (user) => ({ user }));
const setUser = createAction(SET_USER, (user) => ({ user }));
// initialState
const initialState = {
user: null,
is_login: false,
};
// middleware actions
const loginAction = (user) => {
return function (dispatch, getState, { history }) {
console.log(history);
dispatch(setUser(user));
history.push("/");
};
};
const signupFB = (id, pwd, user_name) => {
return function (dispatch, getState, {history}){
auth
.createUserWithEmailAndPassword(id, pwd)
.then((user) => {
console.log(user);
auth.currentUser.updateProfile({
displayName: user_name,
}).then(()=>{
dispatch(setUser({user_name: user_name, id: id, user_profile: ''}));
history.push('/');
}).catch((error) => {
console.log(error);
});
// Signed in
// ...
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode, errorMessage);
// ..
});
}
}
// reducer
export default handleActions(
{
[SET_USER]: (state, action) =>
produce(state, (draft) => {
setCookie("is_login", "success");
draft.user = action.payload.user;
draft.is_login = true;
}),
[LOG_OUT]: (state, action) =>
produce(state, (draft) => {
deleteCookie("is_login");
draft.user = null;
draft.is_login = false;
}),
[GET_USER]: (state, action) => produce(state, (draft) => {}),
},
initialState
);
// action creator export
const actionCreators = {
logOut,
getUser,
loginAction,
signupFB,
};
export { actionCreators };
3. Signup.js
import React from "react";
import { Grid, Text, Input, Button } from "../elements";
import { useDispatch } from "react-redux";
import { actionCreators as userActions } from "../redux/modules/user";
const Signup = (props) => {
const dispatch = useDispatch();
const [id, setId] = React.useState("");
const [pwd, setPwd] = React.useState("");
const [pwd_check, setPwdCheck] = React.useState("");
const [user_name, setUserName] = React.useState("");
const signup = () => {
if (id === "" || pwd === "" || user_name === "") {
return;
}
if (pwd !== pwd_check) {
return;
}
dispatch(userActions.signupFB(id, pwd, user_name));
};
return (
<React.Fragment>
<Grid padding="16px">
<Text size="32px" bold>
회원가입
</Text>
<Grid padding="16px 0px">
<Input
label="아이디"
placeholder="아이디를 입력해주세요."
_onChange={(e) => {
setId(e.target.value);
}}
/>
</Grid>
<Grid padding="16px 0px">
<Input
label="닉네임"
placeholder="닉네임을 입력해주세요."
_onChange={(e) => {
setUserName(e.target.value);
}}
/>
</Grid>
<Grid padding="16px 0px">
<Input
label="비밀번호"
placeholder="비밀번호를 입력해주세요."
_onChange={(e) => {
setPwd(e.target.value);
}}
/>
</Grid>
<Grid padding="16px 0px">
<Input
label="비밀번호 확인"
placeholder="비밀번호를 다시 입력해주세요."
_onChange={(e) => {
setPwdCheck(e.target.value);
}}
/>
</Grid>
<Button text="회원가입하기" _onClick={signup}></Button>
</Grid>
</React.Fragment>
);
};
Signup.defaultProps = {};
export default Signup;
'내마음대로만들어보자 > React 프로젝트 만들기 이해' 카테고리의 다른 글
[React]Firebase Authentication (0) | 2021.11.28 |
---|---|
[React]스토어 주입하기 (0) | 2021.11.28 |
[React]리덕스 스토어 만들기 (0) | 2021.11.28 |
[React] 리덕스 유저모듈만들기 (0) | 2021.11.28 |
[React]로그인 상태에 따른 분기 하기 (0) | 2021.11.28 |