diff --git a/webapp/src/index.js b/webapp/src/index.js index 076c63e..5661dca 100644 --- a/webapp/src/index.js +++ b/webapp/src/index.js @@ -1,20 +1,36 @@ -import React from 'react'; +import React, {useEffect, useState} from 'react'; import ReactDOM from 'react-dom/client'; import './index.css'; import Home from "./pages/home"; import Login from "./pages/login"; import Game from "./pages/game"; import {BrowserRouter, Route, Routes} from "react-router-dom"; +import Register from "./pages/register"; const root = ReactDOM.createRoot(document.getElementById('root')); -root.render( - - - - }/> - }/> - }/> - - - -); +function App() { + const savedToken = localStorage.getItem('token'); + const [token, setToken] = useState(savedToken ? savedToken : null) + useEffect(() => { + if(savedToken !== token){ + localStorage.setItem('token', token); + } + }, [token]); + + return ( + + + + }/> + }/> + }/> + }/> + + + + ) +} + + + +root.render() \ No newline at end of file diff --git a/webapp/src/modules/fetcher.js b/webapp/src/modules/fetcher.js new file mode 100644 index 0000000..2b682a2 --- /dev/null +++ b/webapp/src/modules/fetcher.js @@ -0,0 +1,69 @@ + +async function get(url, token) { + const options = { + method: 'GET', + headers: { 'Content-Type': 'application/json', authorization: `${token}` }, + }; + + return await fetch(url, options) + .then(res => res.json()) + .then(json => { + return json; + }) + +} + +async function post(url, body, token) { + const options = { + method: 'POST', + mode: 'cors', + headers: { 'Content-Type': 'application/json', authorization: `${token}` }, + body: JSON.stringify(body), + }; + + return await fetch(url, options) + .then(res => res.json()) + .then(json => { + return json; + }) + +} + +async function patch(url, body, token) { + const options = { + method: 'PATCH', + mode: 'cors', + headers: { 'Content-Type': 'application/json', authorization: `${token}` }, + body: JSON.stringify(body), + }; + + return await fetch(url, options) + .then(res => res.json()) + .then(json => { + return json; + }) + +} + +async function put(url, body, token) { + const options = { + method: 'PUT', + mode: 'cors', + headers: { 'Content-Type': 'application/json', authorization: `${token}` }, + body: JSON.stringify(body), + }; + + return await fetch(url, options) + .then(res => res.json()) + .then(json => { + return json; + }) + +} + +export { + get, + post, + patch, + put, +}; \ No newline at end of file diff --git a/webapp/src/pages/login/index.jsx b/webapp/src/pages/login/index.jsx index 48e6b87..6272cdb 100644 --- a/webapp/src/pages/login/index.jsx +++ b/webapp/src/pages/login/index.jsx @@ -1,60 +1,60 @@ +import {Link, redirect, useNavigate} from "react-router-dom"; +import {useState} from "react"; +import {get, post} from '../../modules/fetcher'; +function Login({setToken}) { + const navigate = useNavigate(); + const loginUser = async (e) => { + e.preventDefault() + post('https://saucisson.justw.tf/api/users/login', {username, password}, '') + .then(async res => { + if (res.status === 200) { + console.log(res.JSON.message) + setToken(res.JSON.token) + navigate('/') + } else { + setError(res.message) + } + }) - -function Login() { + } + const [error, setError] = useState(null) + const [username, setUsername] = useState(null) + const [password, setPassword] = useState(null) return ( -
+
- - logo - Debate -
+ className="w-full bg-white rounded-lg shadow md:mt-0 sm:max-w-md xl:p-0">
-

- Sign in to your account +

+ Login to your account

-
+ + {error && ( +
+

{error}

+
+ )}
- + className="block mb-2 text-sm font-medium text-gray-900">Your + username + {setUsername(e.target.value)}}/>
+ className="block mb-2 text-sm font-medium text-gray-900">Password + className="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-blue-600 focus:border-blue-600 block w-full p-2.5" + required onChange={e => {setPassword(e.target.value)}}/>
-
-
-
- -
-
- -
-
- Forgot - password? -
- -

- Don’t have an account yet? Sign - up + +

+ Don’t have an account yet? Sign + up

diff --git a/webapp/src/pages/register/index.jsx b/webapp/src/pages/register/index.jsx index 7de5649..7f460ef 100644 --- a/webapp/src/pages/register/index.jsx +++ b/webapp/src/pages/register/index.jsx @@ -1,10 +1,79 @@ +import {Link, redirect, useNavigate} from "react-router-dom"; +import {post} from "../../modules/fetcher"; +import {useState} from "react"; -function Register() { +function Register({setToken}) { + const navigate = useNavigate(); + const registerUser = async (e) => { + e.preventDefault() + if(password === confirmPassword) { + post('https://saucisson.justw.tf/api/users/register', {username, password}, '') + .then(async res => { + if (res.status === 200) { + setToken(res.JSON.token) + navigate('/') + } else { + setError(res.message) + } + }) + } else { + setError("Password doesn't match") + } + + } + const [error, setError] = useState(null) + const [username, setUsername] = useState(null) + const [password, setPassword] = useState(null) + const [confirmPassword, setconfirmPassword] = useState(null) return ( -
- -
+
+
+
+
+

+ Create an account +

+
+ {error && ( +
+

{error}

+
+ )} +
+ + {setUsername(e.target.value)}}/> +
+
+ + {setPassword(e.target.value)}}/> +
+
+ + {setconfirmPassword(e.target.value)}}/> +
+ +

+ Don’t have an account yet? Sign + up +

+
+
+
+
+
) }