zwischenstand
This commit is contained in:
parent
b8abc47097
commit
f8738c5bef
9 changed files with 153 additions and 22 deletions
47
blog/package-lock.json
generated
47
blog/package-lock.json
generated
|
|
@ -11,8 +11,12 @@
|
|||
"@astrojs/preact": "^6.0.5",
|
||||
"@astrojs/rss": "^4.0.19",
|
||||
"astro": "^7.2.2",
|
||||
"jwt-decode": "^4.0.0",
|
||||
"preact": "^10.29.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jsonwebtoken": "^9.0.10"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=22.12.0"
|
||||
}
|
||||
|
|
@ -2407,6 +2411,17 @@
|
|||
"@types/unist": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/jsonwebtoken": {
|
||||
"version": "9.0.10",
|
||||
"resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.10.tgz",
|
||||
"integrity": "sha512-asx5hIG9Qmf/1oStypjanR7iKTv0gXQ1Ov/jfrX6kS/EO0OFni8orbmGCn0672NHR3kXHwpAwR+B368ZGN/2rA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@types/ms": "*",
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/mdast": {
|
||||
"version": "4.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-4.0.4.tgz",
|
||||
|
|
@ -2437,6 +2452,16 @@
|
|||
"@types/unist": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "26.6.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-26.6.4.tgz",
|
||||
"integrity": "sha512-ldVPDCzj7fsaGZrLB0NuHuTvJcsNasysBAqMolr/cgxrLd1xbqxIr3XJiPnHHJUCxj5sNF1vnRj9aWnrVh5Jcg==",
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"undici-types": "~8.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/unist": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.3.tgz",
|
||||
|
|
@ -3998,9 +4023,9 @@
|
|||
}
|
||||
},
|
||||
"node_modules/http-cache-semantics": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.2.0.tgz",
|
||||
"integrity": "sha512-dTxcvPXqPvXBQpq5dUr6mEMJX4oIEFv6bwom3FDwKRDsuIjjJGANqhBuoAn9c1RQJIdAKav33ED65E2ys+87QQ==",
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.3.0.tgz",
|
||||
"integrity": "sha512-M5t5LlJpS1UHMjvwRQVdFHvPISGeLAxNcrWuJkeGh0KxsqCHZ1O3NXZU/8x7cD0BDcGW8kapxMKTvwlqrNkHkA==",
|
||||
"license": "BSD-2-Clause"
|
||||
},
|
||||
"node_modules/inline-style-parser": {
|
||||
|
|
@ -4147,6 +4172,15 @@
|
|||
"integrity": "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/jwt-decode": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz",
|
||||
"integrity": "sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/kolorist": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmjs.org/kolorist/-/kolorist-1.8.0.tgz",
|
||||
|
|
@ -6569,6 +6603,13 @@
|
|||
"node": ">=22.19.0"
|
||||
}
|
||||
},
|
||||
"node_modules/undici-types": {
|
||||
"version": "8.9.0",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.9.0.tgz",
|
||||
"integrity": "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg==",
|
||||
"devOptional": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/unified": {
|
||||
"version": "11.0.5",
|
||||
"resolved": "https://registry.npmjs.org/unified/-/unified-11.0.5.tgz",
|
||||
|
|
|
|||
|
|
@ -16,6 +16,10 @@
|
|||
"@astrojs/preact": "^6.0.5",
|
||||
"@astrojs/rss": "^4.0.19",
|
||||
"astro": "^7.2.2",
|
||||
"jwt-decode": "^4.0.0",
|
||||
"preact": "^10.29.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jsonwebtoken": "^9.0.10"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import Logo from "./Logo.astro";
|
|||
<a href="/">Startseite</a>
|
||||
</div>
|
||||
<div class="nav-right">
|
||||
<UserAction client:only/>
|
||||
<UserAction client:load/>
|
||||
<a href="/impressum" class="menu-nav">Impressum</a>
|
||||
<a href="/privacy-policy" class="menu-nav">Datenschutz</a>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,43 +1,42 @@
|
|||
import { useEffect, useState } from 'preact/hooks';
|
||||
import type { JSX } from 'preact';
|
||||
import { api } from '../lib/api';
|
||||
import { api, type User } from '../lib/api';
|
||||
import React from 'preact/compat';
|
||||
import { getUser } from './Util';
|
||||
|
||||
export default function ToggleSubscription() {
|
||||
const [subscription, setSubscription] = React.useState<boolean>(false);
|
||||
const [error, setError] = useState('');
|
||||
let user: User | null = null
|
||||
|
||||
useEffect(() => {
|
||||
const fetchUser = async () => {
|
||||
const user = await api.getUserInfo();
|
||||
setSubscription(user.subscription);
|
||||
};
|
||||
fetchUser(); // TODO: Ersetzen mit getUser()
|
||||
user = getUser()
|
||||
if (!user) navigation.back()
|
||||
}, []);
|
||||
|
||||
const handleSubscription = async (e: JSX.TargetedEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
const handleSubscription = async () => {
|
||||
setError('');
|
||||
|
||||
try {
|
||||
api.toggleSubscription(subscription)
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Registrierung fehlgeschlagen');
|
||||
setError(err instanceof Error ? err.message : 'Update');
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener('submit-toggle-subscription', handleSubscription);
|
||||
return () => window.removeEventListener('submit-toggle-subscription', handleSubscription);
|
||||
}, [subscription]);
|
||||
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2>Toggle Newsletter Subscription</h2>
|
||||
<form onSubmit={handleSubscription}>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={subscription}
|
||||
onChange={e => { setSubscription(e.currentTarget.checked) }}
|
||||
/>
|
||||
<button type="submit">
|
||||
Absenden
|
||||
</button>
|
||||
{error && <p style={{ color: 'red' }}>{error}</p>}
|
||||
</form>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,20 @@
|
|||
import { getUser } from "./Util"
|
||||
import { useEffect, useState } from "preact/hooks"
|
||||
import { checkJWTValidity, getUser } from "./Util"
|
||||
import React from "preact/compat";
|
||||
import type { User } from "../lib/api";
|
||||
|
||||
export default function UserAction() {
|
||||
const [user, setUser] = React.useState<User>({ 'email': '', 'name': '', 'subscription': true });
|
||||
|
||||
let text = "⚓︎ Login"
|
||||
let link = "/login"
|
||||
|
||||
const user = getUser()
|
||||
useEffect(() => {
|
||||
checkJWTValidity()
|
||||
const user = getUser()
|
||||
if (user) setUser(user)
|
||||
}, [])
|
||||
|
||||
|
||||
if (user) {
|
||||
text = `⚓︎ ${user.name}`
|
||||
|
|
|
|||
21
blog/src/islands/UserDetails.tsx
Normal file
21
blog/src/islands/UserDetails.tsx
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import React, { useEffect } from "preact/compat";
|
||||
import { getUser } from "./Util"
|
||||
import type { User } from "../lib/api";
|
||||
|
||||
export default function UserDetails() {
|
||||
const [user, setUser] = React.useState<User>({ 'email': '', 'name': '', 'subscription': true });
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
const user = getUser()
|
||||
if (user) setUser(user)
|
||||
}, [])
|
||||
|
||||
|
||||
return (
|
||||
<div>
|
||||
<span>Nutzername: {user?.name}</span>
|
||||
<span>E-Mail Adresse: {user?.email}</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -1,16 +1,42 @@
|
|||
import { USER_KEY, type User } from "../lib/api"
|
||||
import { jwtDecode } from "jwt-decode"
|
||||
import { AUTH_TOKEN_KEY, USER_KEY, type User } from "../lib/api"
|
||||
|
||||
export function saveUser(user: User) {
|
||||
localStorage.setItem(USER_KEY, JSON.stringify(user))
|
||||
}
|
||||
|
||||
export function getUser(): User | null {
|
||||
checkJWTValidity()
|
||||
|
||||
const user_string = localStorage.getItem(USER_KEY)
|
||||
if (user_string) return JSON.parse(user_string) as User
|
||||
else logout()
|
||||
return null
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
localStorage.removeItem(USER_KEY)
|
||||
localStorage.removeItem(AUTH_TOKEN_KEY)
|
||||
}
|
||||
|
||||
export function isEmailValid(email: string): boolean {
|
||||
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
||||
return emailRegex.test(email);
|
||||
};
|
||||
}
|
||||
|
||||
export function checkJWTValidity() {
|
||||
const token = localStorage.getItem(AUTH_TOKEN_KEY)
|
||||
if (!token || isTokenExpired(token)) {
|
||||
logout()
|
||||
}
|
||||
}
|
||||
|
||||
export function isTokenExpired(token: string): boolean {
|
||||
try {
|
||||
const { exp } = jwtDecode<{ exp: number }>(token)
|
||||
const expirationDatetimeInSeconds = exp * 1000
|
||||
return Date.now() >= expirationDatetimeInSeconds
|
||||
} catch {
|
||||
return true
|
||||
}
|
||||
}
|
||||
13
blog/src/islands/auth.server.tsx
Normal file
13
blog/src/islands/auth.server.tsx
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import jwt from 'jsonwebtoken';
|
||||
|
||||
const isTokenExpired = (token: string): boolean => {
|
||||
try {
|
||||
const { exp } = jwt.decode(token) as { exp: number };
|
||||
const expirationDatetimeInSeconds = exp * 1000;
|
||||
return Date.now() >= expirationDatetimeInSeconds;
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
export { isTokenExpired };
|
||||
17
blog/src/pages/user.astro
Normal file
17
blog/src/pages/user.astro
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
---
|
||||
import ToggleSubscription from "../islands/ToggleSubscription";
|
||||
import UserDetails from "../islands/UserDetails";
|
||||
import Form from "../layouts/Form.astro";
|
||||
|
||||
---
|
||||
|
||||
<Form
|
||||
title="Moin moin!"
|
||||
subtitle=""
|
||||
text="Hier kannst du dein Konto verwalten."
|
||||
buttonText="Speichern"
|
||||
eventString="submit-toggle-subscription"
|
||||
>
|
||||
<UserDetails client:load />
|
||||
<ToggleSubscription client:load/>
|
||||
</Form>
|
||||
Loading…
Add table
Reference in a new issue