diff --git a/src/pages/Login.js b/src/pages/Login.js index 5bb7ddd..0c45fb3 100644 --- a/src/pages/Login.js +++ b/src/pages/Login.js @@ -1,7 +1,9 @@ -import React from "react"; +import React, { useState } from "react"; +import { Route, Routes, useNavigate } from "react-router"; import '../css/pages/Login.css'; import logoImg from '../img/logo.png'; -import { post } from "../util/Util"; +import { useLoginContext } from "../structures/UserContext"; +import { fetchUser, post } from "../util/Util"; const loginMethods = [ { @@ -10,35 +12,52 @@ const loginMethods = [ } ]; -const Login = () => { - document.body.classList.add('bg-triangles'); + +const CredentialFields = () => { + + const [, setUser] = useLoginContext(); + const [fail, setFail] = useState(false); + const navigate = useNavigate(); + console.log(fail); const loginClick = async () => { const username = document.getElementById('username').value; const password = document.getElementById('password').value; if (!username.length || !password.length) return; - await post('/api/login', {username, password}); + const result = await post('/api/login', { username, password }); + console.log(result); + if (!result.success) { + setFail(true); + return; + } + + if (!result.twoFactor) { + setUser(await fetchUser()); + return navigate('/home'); + } + return navigate('/login/verify'); }; - return
-
-
- -
-
-

Log in

+ return
+
+ +
+
+ {fail ? 'Invalid credentials' : ''} +

Log in

- - - -
+ + + +
-
- Alternate login methods -
+
+ Alternate login methods +
{loginMethods.map(method => {method.name} { width={48} height={48} onClick={() => { window.location.pathname = `/api/login/${method.name}`; }} />)} -
- +
+
; +}; +const TwoFactor = () => { + + const [, setUser] = useLoginContext(); + const navigate = useNavigate(); + const twoFactorClick = async () => { + const code = document.getElementById('2faCode').value; + if (!code) return; + + const result = await post('/api/login/verify', { code }); + console.log(result); + if (result.success) { + setUser(await fetchUser()); + return navigate('/home', { replace: true }); + } + // else throw error? + }; + + return
+

Verification code

+ + +
; +}; + +const Login = () => { + document.body.classList.add('bg-triangles'); + + return
+
+ + + } /> + } /> + +
;