Skip to content

Latest commit

 

History

History
64 lines (54 loc) · 1.94 KB

readme.md

File metadata and controls

64 lines (54 loc) · 1.94 KB

prismy-session

📒 Session for prismy

Build Status codecov NPM download Language grade: JavaScript

npm i prismy-session

Example

import {
  prismy,
  methodSelector,
  createUrlEncodedBodySelector,
  redirect,
  res
} from 'prismy'
import createSession from 'prismy-session'
import JWTCookieStrategy from 'prismy-session-strategy-jwt-cookie'

const { sessionSelector, sessionMiddleware } = createSession(
  new JWTCookieStrategy({
    secret: 'RANDOM_HASH'
  })
)

const urlEncodedBodySelector = createUrlEncodedBodySelector()

export default prismy(
  [methodSelector, sessionSelector, urlEncodedBodySelector],
  (method, session, body) => {
    if (method === 'POST') {
      session.data = { message: body.message }
      return redirect('/')
    } else {
      const { data } = session
      return res(
        [
          '<!DOCTYPE html>',
          '<body>',
          `<p>Message: ${data != null ? (data as any).message : 'NULL'}</p>`,
          '<form action="/" method="post">',
          '<input name="message">',
          '<button type="submit">Send</button>',
          '</form>',
          '</body>'
        ].join('')
      )
    }
  },
  [sessionMiddleware]
)

Session strategies