Skip to content
Snippets Groups Projects
Commit 257ae560 authored by Zink Hannah's avatar Zink Hannah
Browse files

Set Cookies in Cookies.ts

parent 09727822
No related branches found
No related tags found
1 merge request!29final login and registration process
export function setCookie(name: string, value: any, daysToLive: number){
const date = new Date();
date.setTime(date.getTime() + (daysToLive * 24 * 60 * 60 * 1000));
let expires = "expires=" + date.toUTCString();
document.cookie = `${name}=${value}; ${expires}`
}
import {useState} from "react";
import {setCookie} from "../jwt/Cookies.ts";
function Login() {
const [email, setEmail] = useState('');
......@@ -12,6 +13,7 @@ function Login() {
try {
const res = await fetch("http://localhost:8080/api/v1/auth/authenticate",{method:"POST",body:JSON.stringify({email:email,password:password})})
const {token} = await res.json();
setCookie("Jwt",token, 1);
console.log(token);
} catch (error:any) {
console.error('Fehler bei der Anmeldung:', error.message);
......
import {useState} from "react";
function Register() {
const [firstname, setFirstname] = useState('');
const [lastname, setLastname] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleSubmit = async (e: { preventDefault: () => void; }) => {
e.preventDefault(); //verhindert, dass die Seite neu geladen wird, wenn das Formular abgeschickt wird
await handleRegister();
};
const handleRegister = async () => {
try {
const res = await fetch("http://localhost:8080/api/v1/auth/register",
{method:"POST",body:JSON.stringify({
firstname:firstname,
lastname:lastname,
email:email,
password:password})})
const {token} = await res.json();
console.log(token);
} catch (error:any) {
console.error('Fehler bei der Anmeldung:', error.message);
}
};
return (
<main>
<div>
<h2>Login</h2>
<form>
<div>
<label>Vorname:</label>
<input
type="text"
value={firstname}
onChange={(e) => setFirstname(e.target.value)}
/>
</div>
<div>
<label>Nachname:</label>
<input
type="text"
value={lastname}
onChange={(e) => setLastname(e.target.value)}
/>
</div>
<div>
<label>Benutzername oder E-Mail:</label>
<input
type="text"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>
<div>
<label>Passwort:</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<div>
<form onSubmit={handleSubmit}>
<button type="submit">Anmelden</button>
</form>
</div>
</form>
</div>
</main>
);
}
export default Register;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment