Fix redirect on login
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
import { useState } from "react";
|
import { useState, useContext } from "react";
|
||||||
import login from "../../utils/login.js";
|
|
||||||
import getUser from "../../utils/getUser.js";
|
|
||||||
import styles from "./loginForm.module.css";
|
import styles from "./loginForm.module.css";
|
||||||
import Button from "../ui/button/button.jsx";
|
import Button from "../ui/button/button.jsx";
|
||||||
import Input from "../ui/input/input.jsx";
|
import Input from "../ui/input/input.jsx";
|
||||||
import { useNavigate } from "react-router";
|
import { useNavigate } from "react-router";
|
||||||
|
import { AuthContext } from "../../contexts/auth/AuthContext.js";
|
||||||
|
|
||||||
|
|
||||||
function LoginForm({ className }) {
|
function LoginForm({ className }) {
|
||||||
@@ -12,16 +11,12 @@ function LoginForm({ className }) {
|
|||||||
const [password, setPassword] = useState('');
|
const [password, setPassword] = useState('');
|
||||||
const [showPassword, setShowPassword] = useState(false);
|
const [showPassword, setShowPassword] = useState(false);
|
||||||
let navigate = useNavigate();
|
let navigate = useNavigate();
|
||||||
|
const { login } = useContext(AuthContext);
|
||||||
|
|
||||||
async function handleSubmit(e) {
|
async function handleSubmit(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
await login(email, password);
|
||||||
const res = await login(email, password);
|
navigate("/");
|
||||||
|
|
||||||
if(res.status === 200) {
|
|
||||||
await getUser();
|
|
||||||
navigate("/");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const toggleShowPassword = () => {
|
const toggleShowPassword = () => {
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ export function AuthProvider({ children }) {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const login = async (email, password) => {
|
const login = async (email, password) => {
|
||||||
await loginFunction(email, password);
|
const res = await loginFunction(email, password);
|
||||||
|
if (!res || res.status !== 200) throw new Error("Login error");
|
||||||
const u = await getUser();
|
const u = await getUser();
|
||||||
setUser(u);
|
setUser(u);
|
||||||
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user