@newageerp/node-sessions
v1.0.18
Published
```bash npm install @newageerp/node-sessions ```
Readme
Install
npm install @newageerp/node-sessionsENV variables
- CRM_SESSION_BACKEND_V3
- SESSION_PROJECT
.env / .env.stg example
CRM_SESSION_BACKEND_V3=https://session.crm.apidata.app- SESSION_PROJECT - unique project name with locale (if not EN)
- todayistheday
- todayistheday-es
- mellowflow
- mellowflow-es
- ...
.env.prod example
- CRM_SESSION_BACKEND_V3 - replace apidata.app with project domain (if this is a test/RnD project, then use the domain of the main project, whose payments are used)
- SESSION_PROJECT - as .env/.env.stg
middleware.ts
import { sessionNextSrv } from '@newageerp/node-sessions'
export async function middleware(req: NextRequest) {
const sessionResp = await sessionNextSrv.checkForRoutes(req)
if (sessionResp) {
return sessionResp
}
}
export const config = {
matcher: '/((?!_next/static|_next/image|favicon.ico).*)',
}
Usage session
set data
in examples you can see sessionBrowser or sessionSrv calls, make sure you are using the correct method depending on the context.
Almost all methods are available in both environments
create [- IMPORTANT -]
most often after completing the quiz or when we receive the user’s email (whichever comes first)
const sessionId = await sessionBrowser.create({
extraData: {
currency: currency,
quiz: quizResults.answers.raw,
},
})
append [- IMPORTANT -]
if there was no mail when creating session, then add it as soon as we received it
await sessionBrowser.append({
id: uuid,
extraData: {
email: email.trim(),
},
});get data
in examples you can see sessionBrowser or sessionSrv calls, make sure you are using the correct method depending on the context.
Almost all methods are available in both environments
get session data
const sessionData = await sessionBrowser.get({ id })
get quiz data
const sessionData = await sessionSrv.getQuiz({ id })
Usage key/value storage
it is supposed to be used when you need to save a value on the front (for example, a parameter passed via query string) yet have neither an uuid nor an email and then later receive this value on the backend or frontend
set data
add key value by session ID
import { keyValueBrowser } from '@newageerp/node-sessions'
keyValueBrowser.addKeyValueByAnalyticsIdBrowser({
dataKey: 'unique key',
dataValue: {}
})add key value by order ID
import { keyValueBrowser } from '@newageerp/node-sessions'
keyValueBrowser.addKeyValueByUuidBrowser({
uuid,
dataKey: 'unique key',
dataValue: {}
})get data
get value by session ID
import { keyValueBrowser } from '@newageerp/node-sessions'
const dataValue = await keyValueBrowser.getKeyValueByAnalyticsIdBrowser({
dataKey: 'unique key',
})get value by order ID
import { keyValueBrowser } from '@newageerp/node-sessions'
const dataValue = keyValueBrowser.getKeyValueByUuidBrowser({
uuid,
dataKey: 'unique key',
})get value by session ID (server version)
import { keyValueNext } from '@newageerp/node-sessions'
const dataValue = await keyValueNext.getKeyValueByAnalyticsIdNext({
dataKey: 'unique key',
})