Skip to content

Commit

Permalink
[#4] fix : login 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
JZU0 committed Dec 20, 2023
1 parent c0b1712 commit e0b84b6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 11 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"react-router-dom": "^6.21.0",
"react-scripts": "5.0.1",
"recoil": "^0.7.7",
"recoil-persist": "^5.1.0",
"styled-components": "^6.1.1",
"web-vitals": "^2.1.4"
},
Expand Down
6 changes: 4 additions & 2 deletions src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { TestResult5 } from "./pages/TestResult5";
import { TestResult6 } from "./pages/TestResult6";
import { TestResult7 } from "./pages/TestResult7";
import { TestResult8 } from "./pages/TestResult8";
import KakaoShareButton from './pages/Kakao';
import KakaoShareButton from "./pages/Kakao";
import { Frame } from "./pages/Frame";
import Login from "./pages/Login";

const Router = () => {
return (
Expand All @@ -21,6 +22,7 @@ const Router = () => {
<Route path="/" element={<Main />} />
<Route path="/test/:step" element={<TestPage />} />
<Route path="/loading" element={<Loading />} />
<Route path="/login" element={<Login />} />
<Route path="/one" element={<TestResult1 />} />
<Route path="/two" element={<TestResult2 />} />
<Route path="/three" element={<TestResult3 />} />
Expand All @@ -30,7 +32,7 @@ const Router = () => {
<Route path="/seven" element={<TestResult7 />} />
<Route path="/eight" element={<TestResult8 />} />
<Route path="/nine" element={<Frame />} />
<Route path="/ten" element={<KakaoShareButton/>} />
<Route path="/ten" element={<KakaoShareButton />} />
</Routes>
</BrowserRouter>
);
Expand Down
29 changes: 20 additions & 9 deletions src/components/LoginModal/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { useNavigate } from "react-router-dom";
import React, { useEffect } from "react";
import axios from "axios";
import { useNavigate, useLocation } from "react-router-dom";
import {
Container,
GoogleButton,
Expand All @@ -11,26 +12,36 @@ import kakao from "../../assets/images/kakao.png";

const LoginModal = () => {
const navigate = useNavigate();
const location = useLocation();

const handleNonmemberButtonClick = () => {
navigate("/test/0");
};

const handleGoogleLogin = () => {
window.location.href = `https://www.noonsachin.com/oauth2/authorization/google`;
const handleLogin = provider => {
window.location.href = `https://www.noonsachin.com/oauth2/authorization/${provider}`;
};

const handleKakaoLogin = () => {
window.location.href = `https://www.noonsachin.com/oauth2/authorization/kakao`;
};
useEffect(() => {
// Parse the URL to get the JWT
const jwt = new URL(window.location.href).searchParams.get("jwt");
console.log(jwt);
if (jwt) {
// If JWT exists, set it in the headers
axios.defaults.headers.common["Authorization"] = `Bearer ${jwt}`;

// Redirect to the home screen
navigate("/");
}
}, [navigate, location.search]);

return (
<Container>
<GoogleButton onClick={handleGoogleLogin}>
<GoogleButton onClick={() => handleLogin("google")}>
<img src={google} alt="google" />
&nbsp;Google 계정으로 로그인
</GoogleButton>
<KakaoButton onClick={handleKakaoLogin}>
<KakaoButton onClick={() => handleLogin("kakao")}>
<img src={kakao} alt="kakao" />
&nbsp;카카오톡으로 로그인
</KakaoButton>
Expand Down
37 changes: 37 additions & 0 deletions src/pages/Login/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { useEffect } from "react";
import axios from "axios";
import { useNavigate, useLocation } from "react-router-dom";
import { useSetRecoilState } from "recoil";
import { loginState } from "../../store/atoms";

const Login = () => {
const navigate = useNavigate();
const location = useLocation();
const setLoginState = useSetRecoilState(loginState);

useEffect(() => {
// Parse the URL to get the JWT
const jwt = new URL(window.location.href).searchParams.get("jwt");

if (jwt) {
// If JWT exists, set it in the headers
axios.defaults.headers.common["Authorization"] = `Bearer ${jwt}`;

// Set login state to true
setLoginState(true);

// Redirect to the home screen
navigate("/");
console.log("로그인!");
}
}, [navigate, location.search, setLoginState]);

return (
<div>
{/* You can add a loading spinner or other UI elements here */}
Logging in...
</div>
);
};

export default Login;
Empty file added src/pages/Login/styled.js
Empty file.
12 changes: 12 additions & 0 deletions src/store/atoms.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { atom } from "recoil";
import { recoilPersist } from "recoil-persist";

export const testState = atom({
key: "testState",
Expand All @@ -12,3 +13,14 @@ export const testState = atom({
O: 0,
},
});

const { persistAtom } = recoilPersist({
key: "recoil-persist",
storage: sessionStorage,
});

export const loginState = atom({
key: "loginState",
default: false,
effects_UNSTABLE: [persistAtom],
});

0 comments on commit e0b84b6

Please sign in to comment.