-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import useUserModel from "../model/useUserModel"; | ||
import { useAuth } from "../components/Context"; | ||
|
||
const LoginController = ({ onClose }) => { | ||
const { user, token, authSignIn } = useUserModel(); | ||
const { login } = useAuth(); | ||
|
||
const handleLogin = async (userId, userPw) => { | ||
if (!userId || !userPw) { | ||
alert('아이디 또는 비밀번호를 입력해주세요.'); | ||
} | ||
|
||
await authSignIn(userId, userPw); | ||
|
||
if (token) { | ||
onClose(user.name); | ||
login(user); | ||
} | ||
}; | ||
|
||
return { handleLogin, user, token }; | ||
} | ||
export default LoginController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { useState, useEffect } from "react"; | ||
import { Cookies } from "react-cookie"; | ||
import client from 'gamja-backend-client'; | ||
|
||
const useUserModel = () => { | ||
const [user, setUser] = useState(''); | ||
const [token, setToken] = useState(null); | ||
const cookies = new Cookies(); | ||
|
||
const host = 'https://api.miruku.dog'; | ||
|
||
const getConnection = () => { | ||
return { | ||
host: host, | ||
headers: { | ||
...token ? { | ||
'Authorization': `Bearer ${token}` | ||
} : null | ||
} | ||
} | ||
} | ||
|
||
const setCookie = (name, value, option) => { | ||
return cookies.set(name, value, {...option}); | ||
} | ||
|
||
useEffect(() => { | ||
if (token) { | ||
setCookie("token", `${token}`, { | ||
path: '/', | ||
sameSite: 'strict' | ||
}); | ||
|
||
const getMyUser = async () => { | ||
try { | ||
await client.functional.user.me.getMyUser( | ||
getConnection() | ||
).then(response => { | ||
const user = response.user; | ||
setUser(user); | ||
}); | ||
} catch (error) { | ||
console.error("사용자 정보 가져오기 오류: ", error); | ||
} | ||
}; | ||
getMyUser(); | ||
} | ||
}, [token]); | ||
|
||
const authSignIn = async (userId, userPw) => { | ||
try { | ||
await client.functional.auth.signIn( | ||
getConnection(), | ||
{ | ||
id: userId, | ||
password: userPw | ||
} | ||
).then(response => { | ||
setToken(response.token); | ||
}); | ||
} catch (error) { | ||
console.error("로그인 에러: ", error); | ||
alert('아이디 또는 비밀번호가 일치하지 않습니다.') | ||
} | ||
}; | ||
|
||
return { user, token, authSignIn }; | ||
} | ||
export default useUserModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters