Signed cookie session strategy for prismy
npm i prismy-session prismy-cookie prismy-session-strategy-signed-cookie
import { prismy, Method, BaseHandler, UrlEncodedBody } from 'prismy'
import createSession, { SessionState } from 'prismy-session'
import SignedCookieStrategy from 'prismy-session-strategy-signed-cookie'
const { Session, SessionMiddleware } = createSession(
new SignedCookieStrategy({
secret: 'RANDOM_HASH'
})
)
class MyHandler extends BaseHandler {
async handle(
@Method() method: string,
@Session() session: SessionState,
@UrlEncodedBody() body: any
) {
if (method === 'POST') {
// Update session data
session.data = { message: body.message }
return this.redirect('/')
} else {
// Get session data
const { data } = session
return [
'<!DOCTYPE html>',
'<body>',
`<p>Message: ${data != null ? data.message : 'NULL'}</p>`,
'<form action="/" method="post">',
'<input name="message">',
'<button type="submit">Send</button>',
'</form>',
'</body>'
].join('')
}
}
}
export default prismy([SessionMiddleware, MyHandler])