-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from sunnybraille/request
feat: Login session connection
- Loading branch information
Showing
7 changed files
with
204 additions
and
0 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
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,51 @@ | ||
import React from "react"; | ||
import { Link } from "react-router-dom"; | ||
import { useAuth } from "./AuthContext"; | ||
import { useLanguage } from "../LanguageContext"; | ||
import { useHighContrast } from "./HighContrastMode"; | ||
|
||
const AuthButtons: React.FC = () => { | ||
const { language } = useLanguage(); | ||
const textClassName = language === "ko" ? "font-kor" : "font-eng"; | ||
const { isLoggedIn, setLoggedIn } = useAuth(); | ||
const { isHighContrast } = useHighContrast(); | ||
|
||
const handleLogout = () => { | ||
setLoggedIn(false); | ||
}; | ||
|
||
return ( | ||
<div className="flex justify-center space-x-4"> | ||
{isLoggedIn ? ( | ||
<> | ||
<span | ||
className={`${textClassName} ${ | ||
isHighContrast ? "text-yellow-300" : "text-neutral-800" | ||
}`} | ||
> | ||
{language === "ko" ? "로그인이 완료되었습니다" : "Login Success"} | ||
</span> | ||
<button | ||
onClick={handleLogout} | ||
className={`${textClassName} ${ | ||
isHighContrast ? "text-stone-800" : "text-white" | ||
} `} | ||
> | ||
{language === "ko" ? "로그아웃하기" : "Logout"} | ||
</button> | ||
</> | ||
) : ( | ||
<> | ||
<Link to="/login"> | ||
<button>{language === "ko" ? "로그인하기" : "Login"}</button> | ||
</Link> | ||
<Link to="/signup"> | ||
<button>{language === "ko" ? "회원가입" : "Sign Up"}</button> | ||
</Link> | ||
</> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default AuthButtons; |
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,28 @@ | ||
import React, { createContext, useContext, useState } from "react"; | ||
|
||
interface AuthContextType { | ||
isLoggedIn: boolean; | ||
setLoggedIn: (loggedIn: boolean) => void; | ||
} | ||
|
||
export const AuthContext = createContext<AuthContextType | null>(null); | ||
|
||
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [isLoggedIn, setLoggedIn] = useState<boolean>(false); | ||
|
||
return ( | ||
<AuthContext.Provider value={{ isLoggedIn, setLoggedIn }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAuth = () => { | ||
const context = useContext(AuthContext); | ||
if (!context) { | ||
throw new Error("useAuth must be used within an AuthProvider"); | ||
} | ||
return context; | ||
}; |
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
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,61 @@ | ||
import React, { useState } from "react"; | ||
import axios from "axios"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { useAuth } from "./AuthContext"; | ||
|
||
const LoginPage: React.FC = () => { | ||
const [loginId, setLoginId] = useState<string>(""); | ||
const [password, setPassword] = useState<string>(""); | ||
const [error, setError] = useState<string>(""); | ||
const navigate = useNavigate(); | ||
const { setLoggedIn } = useAuth(); | ||
|
||
const handleLogin = async () => { | ||
try { | ||
const response = await axios.post("/login", { | ||
loginId, | ||
password, | ||
}); | ||
if (response.status === 200) { | ||
setLoggedIn(true); | ||
navigate("/"); | ||
} | ||
} catch (error) { | ||
if (axios.isAxiosError(error) && error.response) { | ||
switch (error.response.status) { | ||
case 401: | ||
setError( | ||
"Authentication failed. Please check your login ID and password." | ||
); | ||
break; | ||
default: | ||
setError("An unexpected error occurred."); | ||
} | ||
} else { | ||
setError("An unexpected error occurred."); | ||
} | ||
console.error("Error during login:", error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
value={loginId} | ||
onChange={(e) => setLoginId(e.target.value)} | ||
placeholder="Login ID" | ||
/> | ||
<input | ||
type="password" | ||
value={password} | ||
onChange={(e) => setPassword(e.target.value)} | ||
placeholder="Password" | ||
/> | ||
<button onClick={handleLogin}>Login</button> | ||
{error && <p>{error}</p>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default LoginPage; |
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
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,40 @@ | ||
import React, { useState } from "react"; | ||
import axios from "axios"; | ||
|
||
const SignUpPage: React.FC = () => { | ||
const [loginId, setLoginId] = useState<string>(""); | ||
const [password, setPassword] = useState<string>(""); | ||
|
||
const handleSignUp = async () => { | ||
try { | ||
const response = await axios.post("/join", { | ||
loginId, | ||
password, | ||
}); | ||
if (response.status === 200) { | ||
} | ||
} catch (error) { | ||
console.error("Error during sign up:", error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
value={loginId} | ||
onChange={(e) => setLoginId(e.target.value)} | ||
placeholder="Login ID" | ||
/> | ||
<input | ||
type="password" | ||
value={password} | ||
onChange={(e) => setPassword(e.target.value)} | ||
placeholder="Password" | ||
/> | ||
<button onClick={handleSignUp}>Sign Up</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SignUpPage; |