platform/server/token/src/token.ts
Denis Bykhov 0d70ba8363
UBER-221 Confirm registration (#3254)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
2023-05-29 13:20:59 +07:00

34 lines
927 B
TypeScript

import { getWorkspaceId, WorkspaceId } from '@hcengineering/core'
import { getMetadata } from '@hcengineering/platform'
import { decode, encode } from 'jwt-simple'
import serverPlugin from './plugin'
/**
* @public
*/
export interface Token {
email: string
workspace: WorkspaceId
extra?: Record<string, any>
}
const getSecret = (): string => {
return getMetadata(serverPlugin.metadata.Secret) ?? 'secret'
}
/**
* @public
*/
export function generateToken (email: string, workspace: WorkspaceId, extra?: Record<string, string>): string {
return encode({ ...(extra ?? {}), email, workspace: workspace.name, productId: workspace.productId }, getSecret())
}
/**
* @public
*/
export function decodeToken (token: string): Token {
const value = decode(token, getSecret(), false)
const { email, workspace, productId, ...extra } = value
return { email, workspace: getWorkspaceId(workspace, productId), extra }
}