Spaces:
Running
Running
Okay now add nitro as ssr and add an express backend with a simple login and authentication system that creates session id and stores session id as http only cookie and in the ssr middleware for all routes we do have a check for the http only cookie to check if is present or expired to redirect to home page or to login page
d502d70 verified Download server/api/session.get.ts from jaivial/nitro-auth-express-flow: direct link, hf CLI and curl.
- Browser
- Download file 625 Bytes
-
https://huggingface.co/spaces/jaivial/nitro-auth-express-flow/resolve/main/server/api/session.get.ts
- Command line
-
hf download hf://spaces/jaivial/nitro-auth-express-flow/server/api/session.get.ts
-
curl -L -o session.get.ts https://huggingface.co/spaces/jaivial/nitro-auth-express-flow/resolve/main/server/api/session.get.ts
625 Bytes
| ```typescript | |
| import { eventHandler, getCookie } from 'h3' | |
| import { sessionStore } from '../middleware/auth' | |
| export default eventHandler((event) => { | |
| const sessionId = getCookie(event, 'session_id') | |
| if (!sessionId || !sessionStore.has(sessionId)) { | |
| return { authenticated: false } | |
| } | |
| const session = sessionStore.get(sessionId)! | |
| const now = Date.now() | |
| if (now - session.createdAt > 24 * 60 * 60 * 1000) { | |
| sessionStore.delete(sessionId) | |
| return { authenticated: false } | |
| } | |
| return { | |
| authenticated: true, | |
| user: { | |
| id: session.userId, | |
| email: session.email | |
| } | |
| } | |
| }) | |
| ``` |