diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-04-28 23:33:59 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-04-28 23:33:59 +0500 |
| commit | 533c04d9558bd0a575671c157ca42236f59a2c5a (patch) | |
| tree | 895ec8672f9633fd6cfef85f6a12921fc5339ab3 /front/src/Authentication/LoginPage.tsx | |
| parent | ec5ef35ed47b90b9f09199d28f7885f8287815a8 (diff) | |
frontend auth
Diffstat (limited to 'front/src/Authentication/LoginPage.tsx')
| -rw-r--r-- | front/src/Authentication/LoginPage.tsx | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/front/src/Authentication/LoginPage.tsx b/front/src/Authentication/LoginPage.tsx new file mode 100644 index 0000000..e98c506 --- /dev/null +++ b/front/src/Authentication/LoginPage.tsx @@ -0,0 +1,60 @@ +import axios from "axios"; +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"; + +const LoginURL = `${BackendURL}/auth/login`; + +function LoginPage() { + 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={() => { + Login(username, passw, (data) => { + SetAuthState(data); + navigate("/"); + window.location.reload(); + }); + }}> { GetLocalizedString("Log in", lang) } </button> + </div> + ); +} + +async function Login(username: string, passw: string, onSuccess: (data: Authentication) => void) { + await axios.post<Authentication>( + LoginURL, { + Username: username, + Password: passw + } + ).then(resp => { + onSuccess(resp.data); + }).catch(err => { + console.log(`Failed to send a login responce: ${err}`); + }); +} + +export default LoginPage; |
