registrieren works
This commit is contained in:
parent
c15c671bf7
commit
b8abc47097
10 changed files with 132 additions and 59 deletions
|
|
@ -18,6 +18,7 @@ dependencies = [
|
||||||
"feedparser==6.0.14",
|
"feedparser==6.0.14",
|
||||||
"PyJWT==2.15.0",
|
"PyJWT==2.15.0",
|
||||||
"python-dotenv>=1.0.0",
|
"python-dotenv>=1.0.0",
|
||||||
|
"validators>=0.35.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR
|
||||||
|
import validators
|
||||||
|
from starlette.status import HTTP_406_NOT_ACCEPTABLE
|
||||||
from mailtrap import Address
|
from mailtrap import Address
|
||||||
from backend.internal.mail_util import send_mail
|
from backend.internal.mail_util import send_mail
|
||||||
from backend.internal.mail_util import create_html_from_template
|
from backend.internal.mail_util import create_html_from_template
|
||||||
|
|
@ -35,6 +38,12 @@ router = APIRouter(
|
||||||
def register_user(new_user: NewUser, session: Session = Depends(get_session)):
|
def register_user(new_user: NewUser, session: Session = Depends(get_session)):
|
||||||
user = User.model_validate(new_user)
|
user = User.model_validate(new_user)
|
||||||
|
|
||||||
|
if (user.name == ''):
|
||||||
|
raise HTTPException(HTTP_406_NOT_ACCEPTABLE, "Nutzername muss einen Wert enthalten")
|
||||||
|
elif(not validators.email(user.email)):
|
||||||
|
raise HTTPException(HTTP_406_NOT_ACCEPTABLE, "Email nicht valide")
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session.add(user)
|
session.add(user)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
@ -50,7 +59,14 @@ def register_user(new_user: NewUser, session: Session = Depends(get_session)):
|
||||||
|
|
||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
raise HTTPException(HTTP_400_BAD_REQUEST)
|
|
||||||
|
if ("UNIQUE" in str(e)):
|
||||||
|
if ("user.name" in str(e) or "user.email" in str(e)):
|
||||||
|
raise HTTPException(HTTP_406_NOT_ACCEPTABLE)
|
||||||
|
if ("user.user_code" in str(e)):
|
||||||
|
raise HTTPException(HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
else:
|
||||||
|
raise HTTPException(HTTP_400_BAD_REQUEST)
|
||||||
except Exception:
|
except Exception:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
|
|
||||||
|
|
|
||||||
11
backend/uv.lock
generated
11
backend/uv.lock
generated
|
|
@ -202,6 +202,7 @@ dependencies = [
|
||||||
{ name = "pytest" },
|
{ name = "pytest" },
|
||||||
{ name = "python-dotenv" },
|
{ name = "python-dotenv" },
|
||||||
{ name = "sqlmodel" },
|
{ name = "sqlmodel" },
|
||||||
|
{ name = "validators" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
|
|
@ -216,6 +217,7 @@ requires-dist = [
|
||||||
{ name = "pytest", specifier = ">=9.1.1" },
|
{ name = "pytest", specifier = ">=9.1.1" },
|
||||||
{ name = "python-dotenv", specifier = ">=1.0.0" },
|
{ name = "python-dotenv", specifier = ">=1.0.0" },
|
||||||
{ name = "sqlmodel", specifier = ">=0.0.42" },
|
{ name = "sqlmodel", specifier = ">=0.0.42" },
|
||||||
|
{ name = "validators", specifier = ">=0.35.0" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -1894,6 +1896,15 @@ wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/e4/16/c1fd27e9549f3c4baf1dc9c20c456cd2f822dbf8de9f463824b0c0357e06/uvloop-0.22.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:6cde23eeda1a25c75b2e07d39970f3374105d5eafbaab2a4482be82f272d5a5e", size = 4296730, upload-time = "2025-10-16T22:17:00.744Z" },
|
{ url = "https://files.pythonhosted.org/packages/e4/16/c1fd27e9549f3c4baf1dc9c20c456cd2f822dbf8de9f463824b0c0357e06/uvloop-0.22.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:6cde23eeda1a25c75b2e07d39970f3374105d5eafbaab2a4482be82f272d5a5e", size = 4296730, upload-time = "2025-10-16T22:17:00.744Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "validators"
|
||||||
|
version = "0.35.0"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/53/66/a435d9ae49850b2f071f7ebd8119dd4e84872b01630d6736761e6e7fd847/validators-0.35.0.tar.gz", hash = "sha256:992d6c48a4e77c81f1b4daba10d16c3a9bb0dbb79b3a19ea847ff0928e70497a", size = 73399, upload-time = "2025-05-01T05:42:06.7Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/fa/6e/3e955517e22cbdd565f2f8b2e73d52528b14b8bcfdb04f62466b071de847/validators-0.35.0-py3-none-any.whl", hash = "sha256:e8c947097eae7892cb3d26868d637f79f47b4a0554bc6b80065dfe5aac3705dd", size = 44712, upload-time = "2025-05-01T05:42:04.203Z" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "watchfiles"
|
name = "watchfiles"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
|
|
|
||||||
|
|
@ -9,27 +9,32 @@ export default function LoginForm() {
|
||||||
const handleLogin = async () => {
|
const handleLogin = async () => {
|
||||||
setError('');
|
setError('');
|
||||||
let hasError = false;
|
let hasError = false;
|
||||||
|
if (userCode == '') {
|
||||||
|
setError("Musst schon was eingeben!")
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const jwt = await api.login(userCode);
|
||||||
|
localStorage.setItem(AUTH_TOKEN_KEY, jwt);
|
||||||
|
|
||||||
try {
|
const user = await api.getUserInfo()
|
||||||
const jwt = await api.login(userCode);
|
saveUser(user)
|
||||||
localStorage.setItem(AUTH_TOKEN_KEY, jwt);
|
} catch (err) {
|
||||||
|
const errorMessage = err instanceof Error ? err.message : 'Unbekannter Fehler';
|
||||||
const user = await api.getUserInfo()
|
if (errorMessage.includes('Network')) {
|
||||||
saveUser(user)
|
setError("Backend nicht erreichbar")
|
||||||
} catch (err) {
|
} else if (errorMessage.includes('401')) {
|
||||||
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
|
setError("Das war leider nicht richtig")
|
||||||
if (errorMessage.includes('Network')) {
|
} else if (errorMessage.includes('422')) {
|
||||||
setError("Backend nicht erreichbar")
|
setError("Musst schon was eingeben!")
|
||||||
} else if (errorMessage.includes('401')) {
|
} else if (errorMessage.includes('429')) {
|
||||||
setError("Das war leider nicht richtig")
|
setError("Timeout! Warte mal ein bisschen")
|
||||||
} else if (errorMessage.includes('422')) {
|
} else {
|
||||||
setError("Musst schon was eingeben!")
|
setError(errorMessage)
|
||||||
} else if (errorMessage.includes('429')) {
|
}
|
||||||
setError("Timeout! Warte mal ein bisschen")
|
hasError = true
|
||||||
|
} finally {
|
||||||
|
if (!hasError) navigation.back()
|
||||||
}
|
}
|
||||||
hasError = true
|
|
||||||
} finally {
|
|
||||||
if (!hasError) navigation.back()
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -45,7 +50,10 @@ export default function LoginForm() {
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
value={userCode}
|
value={userCode}
|
||||||
onChange={(e) => setUserCode(e.currentTarget.value)}
|
onChange={(e) => {
|
||||||
|
setError('')
|
||||||
|
setUserCode(e.currentTarget.value)
|
||||||
|
}}
|
||||||
required
|
required
|
||||||
class="user-input"
|
class="user-input"
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
|
|
@ -1,53 +1,75 @@
|
||||||
import { useState } from 'preact/hooks';
|
import { useEffect, useState } from 'preact/hooks';
|
||||||
import type { JSX } from 'preact';
|
|
||||||
import { api, type User } from '../lib/api';
|
import { api, type User } from '../lib/api';
|
||||||
import React from 'preact/compat';
|
import React from 'preact/compat';
|
||||||
|
import { isEmailValid } from './Util';
|
||||||
|
|
||||||
export default function RegisterForm() {
|
export default function RegisterForm() {
|
||||||
const [user, setUser] = React.useState<User>({ 'email': '', 'name': '', 'subscription': true });
|
const [user, setUser] = React.useState<User>({ 'email': '', 'name': '', 'subscription': true });
|
||||||
const [loading, setLoading] = useState(false);
|
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
|
|
||||||
const handleRegistration = async (e: JSX.TargetedEvent<HTMLFormElement>) => {
|
const handleRegistration = async () => {
|
||||||
e.preventDefault();
|
|
||||||
setLoading(true);
|
|
||||||
setError('');
|
setError('');
|
||||||
|
let hasError = false
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await api.register(user);
|
await api.register(user);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : 'Registrierung fehlgeschlagen');
|
const errorMessage = err instanceof Error ? err.message : 'Unbekannter Fehler';
|
||||||
|
|
||||||
|
if (errorMessage.includes('Network')) {
|
||||||
|
setError("Backend nicht erreichbar")
|
||||||
|
} else if (errorMessage.includes('406') && user.name == '') {
|
||||||
|
setError("Der Name darf nicht leer sein")
|
||||||
|
} else if (errorMessage.includes('406') && !isEmailValid(user.email)) {
|
||||||
|
setError("Trage eine gültige E-Mail Adresse ein")
|
||||||
|
} else if (errorMessage.includes('406')) {
|
||||||
|
setError("Nutzername oder E-Mail Adresse bereits registriert")
|
||||||
|
} else if (errorMessage.includes('429')) {
|
||||||
|
setError("Timeout! Warte mal ein bisschen")
|
||||||
|
} else {
|
||||||
|
setError(errorMessage)
|
||||||
|
}
|
||||||
|
hasError = true
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
if (!hasError) navigation.back()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener('submit-register', handleRegistration);
|
||||||
|
return () => window.removeEventListener('submit-register', handleRegistration);
|
||||||
|
}, [user.name, user.email, user.subscription]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h2>Registrieren</h2>
|
<form onSubmit={(e) => { e.preventDefault(); handleRegistration(); }} style="display: flex; flex-direction: column;" >
|
||||||
<form onSubmit={handleRegistration}>
|
<div class="input-block">
|
||||||
<input
|
<label>Nutzername: </label>
|
||||||
type="email"
|
<input
|
||||||
value={user.email}
|
type="name"
|
||||||
onChange={e => setUser({ ...user, email: e.currentTarget.value })}
|
value={user.name}
|
||||||
placeholder="Email"
|
onChange={e => setUser({ ...user, name: e.currentTarget.value })}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<input
|
</div>
|
||||||
type="text"
|
<div>
|
||||||
value={user.name}
|
<label>E-Mail Adresse: </label>
|
||||||
onChange={e => setUser({ ...user, name: e.currentTarget.value })}
|
<input
|
||||||
placeholder="Name"
|
type="email"
|
||||||
required
|
value={user.email}
|
||||||
/>
|
onChange={e => setUser({ ...user, email: e.currentTarget.value })}
|
||||||
<input
|
required
|
||||||
type="checkbox"
|
/>
|
||||||
checked={user.subscription}
|
</div>
|
||||||
onChange={e => setUser({ ...user, subscription: e.currentTarget.checked })}
|
<div>
|
||||||
/>
|
<input
|
||||||
<button type="submit" disabled={loading}>
|
type="checkbox"
|
||||||
{loading ? 'Logging in...' : 'Login'}
|
checked={user.subscription}
|
||||||
</button>
|
onChange={e => setUser({ ...user, subscription: e.currentTarget.checked })}
|
||||||
|
/>
|
||||||
|
<label>E-Mail Benachrichtigung für neue Artikel erhalten</label>
|
||||||
|
</div>
|
||||||
|
<span style="font-size: 0.8rem">Du kannst deine Daten jederzeit löschen oder die E-Mail Benachrichtigung deaktivieren.</span>
|
||||||
{error && <p style={{ color: 'red' }}>{error}</p>}
|
{error && <p style={{ color: 'red' }}>{error}</p>}
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -9,3 +9,8 @@ export function getUser(): User | null {
|
||||||
if (user_string) return JSON.parse(user_string) as User
|
if (user_string) return JSON.parse(user_string) as User
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isEmailValid(email: string): boolean {
|
||||||
|
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
||||||
|
return emailRegex.test(email);
|
||||||
|
};
|
||||||
|
|
@ -56,6 +56,7 @@ const { title, subtitle, text, buttonText, eventString } = Astro.props;
|
||||||
|
|
||||||
#form {
|
#form {
|
||||||
padding: 32px 64px;
|
padding: 32px 64px;
|
||||||
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
h1 {
|
h1 {
|
||||||
|
|
@ -66,11 +67,12 @@ const { title, subtitle, text, buttonText, eventString } = Astro.props;
|
||||||
span {
|
span {
|
||||||
font-size: 1.25rem;
|
font-size: 1.25rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
#buttons {
|
#buttons {
|
||||||
display: flex;
|
display: flex;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
align-items: end;
|
align-items: end;
|
||||||
padding-top: 32px;
|
padding-top: 64px;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ import Form from "../layouts/Form.astro";
|
||||||
eventString="submit-login"
|
eventString="submit-login"
|
||||||
>
|
>
|
||||||
<LoginForm client:load />
|
<LoginForm client:load />
|
||||||
<a href="/register">Noch keinen Account? Hier Anmelden</a>
|
<a href="/register">Noch keinen Account? Hier registrieren</a>
|
||||||
</Form>
|
</Form>
|
||||||
|
|
||||||
<style></style>
|
<style></style>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
---
|
---
|
||||||
|
import RegisterForm from "../islands/RegisterForm";
|
||||||
import Form from "../layouts/Form.astro";
|
import Form from "../layouts/Form.astro";
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -8,4 +9,6 @@ import Form from "../layouts/Form.astro";
|
||||||
text="Endlich könnt E-Mail Erinnerungen erhalten, wenn wir neue Artikel veröffentlichen. Für die spätere Kommentarfunktion fragen wir gleich den Namen mit ab."
|
text="Endlich könnt E-Mail Erinnerungen erhalten, wenn wir neue Artikel veröffentlichen. Für die spätere Kommentarfunktion fragen wir gleich den Namen mit ab."
|
||||||
buttonText="Registrieren"
|
buttonText="Registrieren"
|
||||||
eventString="submit-register"
|
eventString="submit-register"
|
||||||
/>
|
>
|
||||||
|
<RegisterForm client:load/>
|
||||||
|
</Form>
|
||||||
|
|
|
||||||
|
|
@ -74,11 +74,16 @@ body {
|
||||||
background-color: var(--primary-dark);
|
background-color: var(--primary-dark);
|
||||||
}
|
}
|
||||||
|
|
||||||
.user-input {
|
input {
|
||||||
border: none;
|
border: none;
|
||||||
background-color: #e9e9e9;
|
background-color: #e9e9e9;
|
||||||
padding: 4px;
|
padding: 4px;
|
||||||
border-radius: 2px;
|
border-radius: 2px;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input[type=checkbox] {
|
||||||
|
margin-left: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.headline {
|
.headline {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue