Compare commits
2 commits
f6346f8165
...
6a658ef747
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a658ef747 | |||
| dd61486b13 |
10 changed files with 175 additions and 43 deletions
|
|
@ -1,14 +0,0 @@
|
||||||
---
|
|
||||||
---
|
|
||||||
|
|
||||||
<a href={Astro.request.headers.get('referer') || '/'}>
|
|
||||||
<button class="rounded">zurück</button>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
button {
|
|
||||||
padding: 12px 24px;
|
|
||||||
margin: 8px 0 16px 0px;
|
|
||||||
border-style: none;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
@ -1,44 +1,56 @@
|
||||||
import { useState } from 'preact/hooks';
|
import { useEffect, useState } from 'preact/hooks';
|
||||||
import type { JSX } from 'preact';
|
import { api, AUTH_TOKEN_KEY, USER_KEY, type User } from '../lib/api';
|
||||||
import { api, AUTH_TOKEN_KEY } from '../lib/api';
|
import { saveUser } from './Util';
|
||||||
|
|
||||||
export default function LoginForm() {
|
export default function LoginForm() {
|
||||||
const [userCode, setUserCode] = useState('');
|
const [userCode, setUserCode] = useState('');
|
||||||
const [loading, setLoading] = useState(false);
|
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
|
|
||||||
const handleLogin = async (e: JSX.TargetedEvent<HTMLFormElement>) => {
|
const handleLogin = async () => {
|
||||||
e.preventDefault();
|
|
||||||
setLoading(true);
|
|
||||||
setError('');
|
setError('');
|
||||||
|
let hasError = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const jwt = await api.login(userCode)
|
const jwt = await api.login(userCode);
|
||||||
localStorage.setItem(AUTH_TOKEN_KEY, jwt)
|
localStorage.setItem(AUTH_TOKEN_KEY, jwt);
|
||||||
// window.location.href = '/';
|
|
||||||
|
const user = await api.getUserInfo()
|
||||||
|
saveUser(user)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : 'Login failed');
|
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
|
||||||
|
if (errorMessage.includes('Network')) {
|
||||||
|
setError("Backend nicht erreichbar")
|
||||||
|
} else if (errorMessage.includes('401')) {
|
||||||
|
setError("Das war leider nicht richtig")
|
||||||
|
} else if (errorMessage.includes('422')) {
|
||||||
|
setError("Musst schon was eingeben!")
|
||||||
|
} else if (errorMessage.includes('429')) {
|
||||||
|
setError("Timeout! Warte mal ein bisschen")
|
||||||
|
}
|
||||||
|
hasError = true
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
if (!hasError) navigation.back()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener('submit-login', handleLogin);
|
||||||
|
return () => window.removeEventListener('submit-login', handleLogin);
|
||||||
|
}, [userCode]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h2>Login</h2>
|
<form onSubmit={(e) => { e.preventDefault(); handleLogin(); }}>
|
||||||
<form onSubmit={handleLogin}>
|
<label>Code: </label>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
value={userCode}
|
value={userCode}
|
||||||
onChange={(e) => setUserCode(e.currentTarget.value)}
|
onChange={(e) => setUserCode(e.currentTarget.value)}
|
||||||
placeholder="Enter your code"
|
|
||||||
required
|
required
|
||||||
|
class="user-input"
|
||||||
/>
|
/>
|
||||||
<button type="submit" disabled={loading}>
|
|
||||||
{loading ? 'Logging in...' : 'Login'}
|
|
||||||
</button>
|
|
||||||
{error && <p style={{ color: 'red' }}>{error}</p>}
|
{error && <p style={{ color: 'red' }}>{error}</p>}
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
10
blog/src/islands/NavBack.tsx
Normal file
10
blog/src/islands/NavBack.tsx
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
export default function NavBack() {
|
||||||
|
|
||||||
|
function take_me_to_the_moon() {
|
||||||
|
navigation.back();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button class="back-btn" onClick={take_me_to_the_moon}>Zurück</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
export default function LoginForm() {
|
import { getUser } from "./Util"
|
||||||
|
|
||||||
|
export default function UserAction() {
|
||||||
let text = "⚓︎ Login"
|
let text = "⚓︎ Login"
|
||||||
let link = "/login"
|
let link = "/login"
|
||||||
|
|
||||||
const username = localStorage.getItem("username")
|
const user = getUser()
|
||||||
|
|
||||||
if (username) {
|
if (user) {
|
||||||
text = `⚓︎ ${username}`
|
text = `⚓︎ ${user.name}`
|
||||||
link = "/user"
|
link = "/user"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
11
blog/src/islands/Util.tsx
Normal file
11
blog/src/islands/Util.tsx
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { USER_KEY, type User } from "../lib/api"
|
||||||
|
|
||||||
|
export function saveUser(user: User) {
|
||||||
|
localStorage.setItem(USER_KEY, JSON.stringify(user))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getUser(): User | null {
|
||||||
|
const user_string = localStorage.getItem(USER_KEY)
|
||||||
|
if (user_string) return JSON.parse(user_string) as User
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
---
|
---
|
||||||
import { Image } from "astro:assets";
|
import { Image } from "astro:assets";
|
||||||
import BackNav from "../components/BackNav.astro";
|
|
||||||
import { findImageByPath } from "../lib/findImage";
|
import { findImageByPath } from "../lib/findImage";
|
||||||
import Main from "./Main.astro";
|
import Main from "./Main.astro";
|
||||||
import { formatLocalizedDateString } from "../lib/formatDate";
|
import { formatLocalizedDateString } from "../lib/formatDate";
|
||||||
import type { PostData } from "../content.config";
|
import type { PostData } from "../content.config";
|
||||||
|
import NavBack from "../islands/NavBack";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
postData: PostData;
|
postData: PostData;
|
||||||
|
|
@ -30,7 +30,7 @@ const localeDate = formatLocalizedDateString(postData.date);
|
||||||
<span>{localeDate}</span>
|
<span>{localeDate}</span>
|
||||||
</div>
|
</div>
|
||||||
<slot />
|
<slot />
|
||||||
<BackNav />
|
<NavBack client:only />
|
||||||
<p class="disclaimer">
|
<p class="disclaimer">
|
||||||
Disclaimer: Dieser Artikel ist ausschließlich privat und wird nicht durch
|
Disclaimer: Dieser Artikel ist ausschließlich privat und wird nicht durch
|
||||||
Produktplatzierung, Affiliate-Links oder ähnliches Unterstützt. Links
|
Produktplatzierung, Affiliate-Links oder ähnliches Unterstützt. Links
|
||||||
|
|
@ -106,6 +106,7 @@ const localeDate = formatLocalizedDateString(postData.date);
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@media only screen and (max-width: 800px) {
|
@media only screen and (max-width: 800px) {
|
||||||
.markdown-content {
|
.markdown-content {
|
||||||
font-size: 1.25rem;
|
font-size: 1.25rem;
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import "../styles/global.css";
|
||||||
<style>
|
<style>
|
||||||
main {
|
main {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
margin: 32px auto;
|
width: 100%;
|
||||||
|
margin: 0;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@ export interface User {
|
||||||
|
|
||||||
const API_BASE = import.meta.env.PUBLIC_API_URL;
|
const API_BASE = import.meta.env.PUBLIC_API_URL;
|
||||||
export const AUTH_TOKEN_KEY = 'authToken'
|
export const AUTH_TOKEN_KEY = 'authToken'
|
||||||
|
export const USER_KEY = 'user'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class BlauwasserAPI {
|
class BlauwasserAPI {
|
||||||
private async fetchAPI<T>(
|
private async fetchAPI<T>(
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,81 @@
|
||||||
---
|
---
|
||||||
|
import LoginForm from "../islands/LoginForm";
|
||||||
|
import NavBack from "../islands/NavBack";
|
||||||
import Main from "../layouts/Main.astro";
|
import Main from "../layouts/Main.astro";
|
||||||
---
|
---
|
||||||
|
|
||||||
<Main>
|
<Main>
|
||||||
|
<div id="background">
|
||||||
|
<div id="content">
|
||||||
|
<img src="../..//blog_img/england/titelbild_england.webp" />
|
||||||
|
<div id="form">
|
||||||
|
<h1>Login</h1>
|
||||||
|
<span>Gib deinen persönlichen Code ein</span>
|
||||||
|
<p>
|
||||||
|
Meld dich an, um deine Daten zu bearbeiten. Gib dafür den
|
||||||
|
Code ein, denn du nach dem registrieren via E-Mail erhalten
|
||||||
|
hast.
|
||||||
|
</p>
|
||||||
|
<LoginForm client:load />
|
||||||
|
|
||||||
|
<div id="buttons">
|
||||||
</Main>
|
<NavBack client:load />
|
||||||
|
<button
|
||||||
|
onclick="window.dispatchEvent(new Event('submit-login'))"
|
||||||
|
class="submit-btn"
|
||||||
|
>
|
||||||
|
⚓︎ Anmelden
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Main>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#background {
|
||||||
|
background-image: linear-gradient(
|
||||||
|
to bottom,
|
||||||
|
var(--primary-dark),
|
||||||
|
var(--primary-color)
|
||||||
|
);
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
padding: 0 20%;
|
||||||
|
}
|
||||||
|
|
||||||
|
#content {
|
||||||
|
background-color: white;
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
img {
|
||||||
|
width: 100%;
|
||||||
|
height: 300px;
|
||||||
|
object-fit: cover;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
#form {
|
||||||
|
padding: 32px 64px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#buttons {
|
||||||
|
display: flex;
|
||||||
|
width: 100%;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: end;
|
||||||
|
padding-top: 32px;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
margin: 0;
|
||||||
|
padding-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
span {
|
||||||
|
font-size: 1.25rem;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
:root {
|
:root {
|
||||||
--primary-color: #416A79;
|
--primary-color: #3c6a7b;
|
||||||
|
--primary-dark: #1d3238;
|
||||||
--primary-text: #454545;
|
--primary-text: #454545;
|
||||||
|
--accent-color: #ffff00;
|
||||||
}
|
}
|
||||||
|
|
||||||
* {
|
* {
|
||||||
|
|
@ -48,6 +50,37 @@ body {
|
||||||
color: black;
|
color: black;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.back-btn {
|
||||||
|
border: none;
|
||||||
|
background-color: transparent;
|
||||||
|
font-weight: bolder;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 1rem;
|
||||||
|
padding: 12px 12px 12px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.submit-btn {
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
background-color: var(--primary-color);
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 12px;
|
||||||
|
font-weight: bolder;
|
||||||
|
font-size: 1rem;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.submit-btn:hover {
|
||||||
|
background-color: var(--primary-dark);
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-input {
|
||||||
|
border: none;
|
||||||
|
background-color: #e9e9e9;
|
||||||
|
padding: 4px;
|
||||||
|
border-radius: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
.headline {
|
.headline {
|
||||||
margin-top: 8px;
|
margin-top: 8px;
|
||||||
margin-bottom: 0;
|
margin-bottom: 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue