diff --git a/blog/package-lock.json b/blog/package-lock.json
index 324cd67..1b574bd 100644
--- a/blog/package-lock.json
+++ b/blog/package-lock.json
@@ -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",
diff --git a/blog/package.json b/blog/package.json
index 26a5d70..15173cc 100644
--- a/blog/package.json
+++ b/blog/package.json
@@ -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"
}
}
diff --git a/blog/src/components/Header.astro b/blog/src/components/Header.astro
index 2f9e7fc..8d90185 100644
--- a/blog/src/components/Header.astro
+++ b/blog/src/components/Header.astro
@@ -14,7 +14,7 @@ import Logo from "./Logo.astro";
Startseite
-
+
diff --git a/blog/src/islands/ToggleSubscription.tsx b/blog/src/islands/ToggleSubscription.tsx
index 0df891a..87f0465 100644
--- a/blog/src/islands/ToggleSubscription.tsx
+++ b/blog/src/islands/ToggleSubscription.tsx
@@ -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(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) => {
- 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 (
-
Toggle Newsletter Subscription
diff --git a/blog/src/islands/UserAction.tsx b/blog/src/islands/UserAction.tsx
index 3387737..baf5dad 100644
--- a/blog/src/islands/UserAction.tsx
+++ b/blog/src/islands/UserAction.tsx
@@ -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({ '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}`
diff --git a/blog/src/islands/UserDetails.tsx b/blog/src/islands/UserDetails.tsx
new file mode 100644
index 0000000..4069e8c
--- /dev/null
+++ b/blog/src/islands/UserDetails.tsx
@@ -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({ 'email': '', 'name': '', 'subscription': true });
+
+
+ useEffect(() => {
+ const user = getUser()
+ if (user) setUser(user)
+ }, [])
+
+
+ return (
+
+ Nutzername: {user?.name}
+ E-Mail Adresse: {user?.email}
+
+ )
+}
\ No newline at end of file
diff --git a/blog/src/islands/Util.tsx b/blog/src/islands/Util.tsx
index ef4b289..558e829 100644
--- a/blog/src/islands/Util.tsx
+++ b/blog/src/islands/Util.tsx
@@ -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);
-};
\ No newline at end of file
+}
+
+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
+ }
+}
\ No newline at end of file
diff --git a/blog/src/islands/auth.server.tsx b/blog/src/islands/auth.server.tsx
new file mode 100644
index 0000000..eb2ed9c
--- /dev/null
+++ b/blog/src/islands/auth.server.tsx
@@ -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 };
\ No newline at end of file
diff --git a/blog/src/pages/user.astro b/blog/src/pages/user.astro
new file mode 100644
index 0000000..ac5681f
--- /dev/null
+++ b/blog/src/pages/user.astro
@@ -0,0 +1,17 @@
+---
+import ToggleSubscription from "../islands/ToggleSubscription";
+import UserDetails from "../islands/UserDetails";
+import Form from "../layouts/Form.astro";
+
+---
+
+