diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-05-03 21:26:49 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-05-03 21:26:49 +0500 |
| commit | e02f7ee46876d32ca094c2b5cf3647248baf7645 (patch) | |
| tree | 7f81450d2ccbcb4fea241a0925b45d9385ae7192 /front/src/Authentication | |
| parent | 533c04d9558bd0a575671c157ca42236f59a2c5a (diff) | |
Register page + logout
Diffstat (limited to 'front/src/Authentication')
| -rw-r--r-- | front/src/Authentication/LoginPage.tsx | 4 | ||||
| -rw-r--r-- | front/src/Authentication/LogoutPage.tsx | 11 | ||||
| -rw-r--r-- | front/src/Authentication/RegisterPage.tsx | 59 |
3 files changed, 73 insertions, 1 deletions
diff --git a/front/src/Authentication/LoginPage.tsx b/front/src/Authentication/LoginPage.tsx index e98c506..e673418 100644 --- a/front/src/Authentication/LoginPage.tsx +++ b/front/src/Authentication/LoginPage.tsx @@ -3,7 +3,6 @@ import { useContext, useState } from "react"; import { useNavigate } from "react-router"; import { BackendURL } from "../Config"; import { Authentication, SaveState } from "./ContextProvider"; -import { useCookies } from 'react-cookie'; import { GetLocalizedString } from "../Locales/Locales"; import { LanguageContext } from "../Locales/Context"; @@ -40,6 +39,9 @@ function LoginPage() { window.location.reload(); }); }}> { GetLocalizedString("Log in", lang) } </button> + + + <p> Don't have an account? <a href="/register"> Click here. </a> </p> </div> ); } diff --git a/front/src/Authentication/LogoutPage.tsx b/front/src/Authentication/LogoutPage.tsx new file mode 100644 index 0000000..a234985 --- /dev/null +++ b/front/src/Authentication/LogoutPage.tsx @@ -0,0 +1,11 @@ +import { useNavigate } from "react-router"; + +function LogoutPage() { + const navigate = useNavigate(); + + document.cookie = "X-AUTH-TOKEN=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; + navigate("/"); + return (<p> Click <a href="/"> here </a> if you were not redirected already </p>); +} + +export default LogoutPage; diff --git a/front/src/Authentication/RegisterPage.tsx b/front/src/Authentication/RegisterPage.tsx new file mode 100644 index 0000000..b30b182 --- /dev/null +++ b/front/src/Authentication/RegisterPage.tsx @@ -0,0 +1,59 @@ +import axios from "axios"; +import { useContext, useState } from "react"; +import { useNavigate } from "react-router"; +import { BackendURL } from "../Config"; +import { Authentication, SaveState } from "./ContextProvider"; +import { GetLocalizedString } from "../Locales/Locales"; +import { LanguageContext } from "../Locales/Context"; + +const RegisterURL = `${BackendURL}/auth/register`; + +function RegisterPage() { + const lang = useContext(LanguageContext); + + const [username, setUsername] = useState<string>(""); + const [passw, setPassw] = useState<string>(""); + const navigate = useNavigate(); + + function SetAuthState(newAuthState: Authentication | null) { + if (newAuthState) { + console.log(`Logging in as ${newAuthState.User}...`); + } else { + console.log(`Logging out...`); + } + SaveState(newAuthState, (cookie: string) => { + document.cookie = `X-AUTH-TOKEN=${cookie}; path=/;`; + }) + } + + return ( + <div> + <h2> { GetLocalizedString("Username", lang) } </h2> + <input onChange={ev => setUsername(ev.target.value)} /> + <h2> { GetLocalizedString("Password", lang) } </h2> + <input onChange={ev => setPassw(ev.target.value)} /> + <button onClick={() => { + Register(username, passw, (data) => { + SetAuthState(data); + navigate("/"); + window.location.reload(); + }); + }}> { GetLocalizedString("Register", lang) } </button> + </div> + ); +} + +async function Register(username: string, passw: string, onSuccess: (data: Authentication) => void) { + await axios.post<Authentication>( + RegisterURL, { + Username: username, + Password: passw + } + ).then(resp => { + onSuccess(resp.data); + }).catch(err => { + console.log(`Failed to send a login responce: ${err}`); + }); +} + +export default RegisterPage; |
