Welcome back {user.name} {user.surname}
+ {} +-
+
diff --git a/examples/middleware/src/middleware.ts b/examples/middleware/src/middleware.ts
index f92b64d440..4854105cae 100644
--- a/examples/middleware/src/middleware.ts
+++ b/examples/middleware/src/middleware.ts
@@ -56,16 +56,27 @@ const validation = defineMiddleware(async (context, next) => {
} else if (context.request.url.endsWith('/api/login')) {
const response = await next();
// the login endpoint will return to us a JSON with username and password
- const data = await response.json();
- // we naively check if username and password are equals to some string
- if (data.username === 'astro' && data.password === 'astro') {
- // we store the token somewhere outside of locals because the `locals` object is attached to the request
- // and when doing a redirect, we lose that information
- loginInfo.token = 'loggedIn';
- loginInfo.currentTime = new Date().getTime();
- return context.redirect('/admin');
- }
- }
+ if (response.headers.get('content-type') === 'application/json') {
+ const data = await response.json();
+ // we naively check if username and password are equals to some string
+ if (data.username === 'astro' && data.password === 'astro') {
+ // we store the token somewhere outside of locals because the `locals` object is attached to the request
+ // and when doing a redirect, we lose that information
+ loginInfo.token = 'loggedIn';
+ loginInfo.currentTime = new Date().getTime();
+ return context.redirect('/admin');
+ }
+ }
+ return response;
+ } else if (context.request.url.endsWith('/api/logout')) {
+ const response = await next();
+ if (response.ok) {
+ loginInfo.token = undefined;
+ loginInfo.currentTime = undefined;
+ return context.redirect('/login');
+ }
+ return response;
+ }
return next();
});
diff --git a/examples/middleware/src/pages/admin.astro b/examples/middleware/src/pages/admin.astro
index 028fd6b080..921758228d 100644
--- a/examples/middleware/src/pages/admin.astro
+++ b/examples/middleware/src/pages/admin.astro
@@ -1,11 +1,16 @@
---
import Layout from '../layouts/Layout.astro';
+import Card from '../components/Card.astro';
const user = Astro.locals.user;
---
Welcome back {user.name} {user.surname}
+ {}
+
+
To get started, open the directory src/pages
in your project.
Code Challenge: Tweak the "Welcome to Astro" message above.
+ Login with: Username: astro
Password: astro