Skip to content

Commit

Permalink
[#32] login 구현하기
Browse files Browse the repository at this point in the history
  • Loading branch information
00kang committed Sep 6, 2022
1 parent 2d7a424 commit 4d468c9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
7 changes: 7 additions & 0 deletions front/src/atoms/Login.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//@ts-check
import { atom } from 'recoil';

export const LoginState = atom({
key: 'src/atoms/Login.jsx-LoginState',
default: false,
});
47 changes: 27 additions & 20 deletions front/src/components/Signin.jsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
//@ts-check

import { atom } from 'recoil';
import { recoilPersist } from 'recoil-persist';
import { useRecoilState } from 'recoil';
import { useState } from 'react';
import { useEffect } from 'react';
import { LoginState } from '../atoms/Login';
import { postUsersLogin } from '../remotes';

const { persistAtom } = recoilPersist();
const [isLoggedIn, setIsLoggedIn] = useRecoilState(LoginState);
const token = window.location.href.split('?token=')[1];

export const LoginState =
atom <
boolean >
{
key: 'LoginState',
default: false,
effects_UNSTABLE: { persistAtom },
};
const Signin = () => {
const [isLoggedIn, setIsLoggedIn] = useRecoilState(LoginState);
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');

useEffect(() => {
if (token) localStorage.setItem('4242-token', token);
if (localStorage.getItem('4242-token')) setIsLoggedIn(true);
}, []);
useEffect(() => {
if (isLoggedIn) {
}
}, [isLoggedIn]);

const Signin = () => {
return (
<div>
<div className="auth-page">
Expand All @@ -37,19 +31,32 @@ const Signin = () => {
<li>That email is already taken</li>
</ul>

<form>
<form
onSubmit={(e) => {
e.preventDefault();

postUsersLogin({ email, password }).then((res) => {
if (res.user) {
setIsLoggedIn(true);
localStorage.setItem('token', res.user.token);
}
});
}}
>
<fieldset className="form-group">
<input
className="form-control form-control-lg"
type="text"
placeholder="Email"
onChange={(e) => setEmail(e.target.value)}
/>
</fieldset>
<fieldset className="form-group">
<input
className="form-control form-control-lg"
type="password"
placeholder="Password"
onChange={(e) => setPassword(e.target.value)}
/>
</fieldset>
<button className="btn btn-lg btn-primary pull-xs-right">
Expand Down
9 changes: 7 additions & 2 deletions front/src/remotes/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import axios from 'axios';

const conduitAxios = axios.create({
baseURL: 'https://conduit.productionready.io/api',
baseURL:
// 'https://api.realworld.io/api-docs/#/User%20and%20Authentication/Login',
'https://api.realworld.io/api-docs/',

This comment has been minimized.

Copy link
@gomjellie

gomjellie Sep 6, 2022

Member

주소가 이게 맞나요?

// headers: {
// 'Content-Type': 'application/json',
// },
});

/**
Expand Down Expand Up @@ -63,7 +68,7 @@ const conduitAxios = axios.create({
// ㅡㅡㅡㅡㅡ User ㅡㅡㅡㅡㅡ
/**
* @param {{ email: string; password: string;}} user
* @returns {Promise<{ data: { user: User }}>}
* @returns {Promise<{ user: User }>}

This comment has been minimized.

Copy link
@gomjellie

gomjellie Sep 6, 2022

Member

변경후가 맞나요?

*/
const postUsersLogin = (user) => conduitAxios.post(`/users/login`);

Expand Down

1 comment on commit 4d468c9

@00kang
Copy link
Collaborator Author

@00kang 00kang commented on 4d468c9 Sep 6, 2022

Choose a reason for hiding this comment

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

!아직 토큰 확인 불가!

Please sign in to comment.