feat: add session cache
This commit is contained in:
parent
a70a360a65
commit
bc06258a1d
@ -3,11 +3,8 @@ import passport from 'passport';
|
||||
import { Handler } from 'express';
|
||||
import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { hashUuid } from '../utils/common';
|
||||
import dayjs from 'dayjs';
|
||||
import { jwtSecret } from '../utils/common';
|
||||
|
||||
export const jwtSecret =
|
||||
process.env.JWT_SECRET || hashUuid(dayjs().format('YYYYMMDD'));
|
||||
export const jwtIssuer = process.env.JWT_ISSUER || 'tianji.msgbyte.com';
|
||||
export const jwtAudience = process.env.JWT_AUDIENCE || 'msgbyte.com';
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Website, WebsiteSession } from '@prisma/client';
|
||||
import { flattenJSON, hashUuid, isUuid } from '../utils/common';
|
||||
import { flattenJSON, hashUuid, isUuid, parseToken } from '../utils/common';
|
||||
import { prisma } from './_client';
|
||||
import { Request } from 'express';
|
||||
import { getClientInfo } from '../utils/detect';
|
||||
@ -41,6 +41,17 @@ export async function findSession(req: Request): Promise<{
|
||||
// Verify payload
|
||||
const { payload } = req.body;
|
||||
|
||||
// Check if cache token is passed
|
||||
const cacheToken = req.headers['x-tianji-cache'] as string;
|
||||
|
||||
if (cacheToken) {
|
||||
const result = parseToken(cacheToken);
|
||||
|
||||
if (result) {
|
||||
return result as any;
|
||||
}
|
||||
}
|
||||
|
||||
const {
|
||||
website: websiteId,
|
||||
hostname,
|
||||
|
@ -7,6 +7,7 @@ import {
|
||||
saveWebsiteEvent,
|
||||
saveWebsiteSessionData,
|
||||
} from '../model/website';
|
||||
import { createToken } from '../utils/common';
|
||||
|
||||
export const websiteRouter = Router();
|
||||
|
||||
@ -101,6 +102,8 @@ websiteRouter.post(
|
||||
});
|
||||
}
|
||||
|
||||
res.send();
|
||||
const token = createToken(session);
|
||||
|
||||
res.send(token);
|
||||
}
|
||||
);
|
||||
|
@ -3,6 +3,7 @@ import crypto from 'crypto';
|
||||
import { DATA_TYPE } from './const';
|
||||
import { DynamicDataType } from './types';
|
||||
import dayjs from 'dayjs';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
export function isUuid(value: string) {
|
||||
return validate(value);
|
||||
@ -129,3 +130,21 @@ function getDataType(value: any): string {
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Secret for auth and cacheTokenGenerate
|
||||
*/
|
||||
export const jwtSecret =
|
||||
process.env.JWT_SECRET || hashUuid(dayjs().format('YYYYMMDD'));
|
||||
|
||||
export function createToken(payload: any, secret = jwtSecret, options?: any) {
|
||||
return jwt.sign(payload, secret, options);
|
||||
}
|
||||
|
||||
export function parseToken(token: string, secret = jwtSecret) {
|
||||
try {
|
||||
return jwt.verify(token, secret);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user