Commit 4acd889b authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

RequireAuth 추가에 따른 from 경로 추가

parent 9388110e
import React, { ChangeEvent, FormEvent, useState } from "react"; import React, { ChangeEvent, FormEvent, useState } from "react";
import { useNavigate } from "react-router-dom"; import { useLocation, useNavigate } from "react-router-dom";
import { catchErrors } from "../helpers"; import { catchErrors } from "../helpers";
import { SpinnerIcon } from "../icons"; import { SpinnerIcon } from "../icons";
import { useAuth } from "./auth.context"; import { useAuth } from "./auth.context";
interface LocationState {
state: { from: string };
}
export const Login = () => { export const Login = () => {
const [error, setError] = useState(""); const [error, setError] = useState("");
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [loginData, setLoginData] = useState({ email: "", password: "" }); const [loginData, setLoginData] = useState({ email: "", password: "" });
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation() as LocationState;
const { login } = useAuth(); const { login } = useAuth();
const from = location.state.from || "/";
function handleChange(e: ChangeEvent<HTMLInputElement>) { function handleChange(e: ChangeEvent<HTMLInputElement>) {
const { name, value } = e.currentTarget; const { name, value } = e.currentTarget;
setLoginData({ ...loginData, [name]: value }); setLoginData({ ...loginData, [name]: value });
...@@ -18,11 +25,10 @@ export const Login = () => { ...@@ -18,11 +25,10 @@ export const Login = () => {
async function handleSubmit(e: FormEvent) { async function handleSubmit(e: FormEvent) {
e.preventDefault(); e.preventDefault();
console.log(loginData);
const { email, password } = loginData; const { email, password } = loginData;
try { try {
setLoading(true); setLoading(true);
await login(email, password, () => navigate("/", { replace: true })); await login(email, password, () => navigate(from, { replace: true }));
} catch (error) { } catch (error) {
setLoading(false); setLoading(false);
catchErrors(error, setError); catchErrors(error, setError);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment