Skip to content

prismyland/prismy-session-strategy-signed-cookie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prismy-session-strategy-signed-cookie

Signed cookie session strategy for prismy

Build Status codecov NPM download Language grade: JavaScript

npm i prismy-session prismy-cookie prismy-session-strategy-signed-cookie

Example

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])