Changed getClientInfo method. Refactored send logic.

This commit is contained in:
Mike Cao 2024-04-05 01:48:59 -07:00
parent ada332f174
commit 698f0c6bbd
3 changed files with 9 additions and 9 deletions

View File

@ -123,9 +123,9 @@ export async function getLocation(ip: string, req: NextApiRequestCollect) {
} }
} }
export async function getClientInfo(req: NextApiRequestCollect, { screen }) { export async function getClientInfo(req: NextApiRequestCollect) {
const userAgent = req.headers['user-agent']; const userAgent = req.headers['user-agent'];
const ip = req.body.payload.ip || getIpAddress(req); const ip = req.body?.payload?.ip || getIpAddress(req);
const location = await getLocation(ip, req); const location = await getLocation(ip, req);
const country = location?.country; const country = location?.country;
const subdivision1 = location?.subdivision1; const subdivision1 = location?.subdivision1;
@ -133,7 +133,7 @@ export async function getClientInfo(req: NextApiRequestCollect, { screen }) {
const city = location?.city; const city = location?.city;
const browser = browserName(userAgent); const browser = browserName(userAgent);
const os = detectOS(userAgent); const os = detectOS(userAgent);
const device = getDevice(screen, os); const device = getDevice(req.body?.payload?.screen, os);
return { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device }; return { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device };
} }

View File

@ -65,7 +65,7 @@ export async function findSession(req: NextApiRequestCollect): Promise<{
await checkUserBlock(website.userId); await checkUserBlock(website.userId);
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } = const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
await getClientInfo(req, payload); await getClientInfo(req);
const sessionId = uuid(websiteId, hostname, ip, userAgent); const sessionId = uuid(websiteId, hostname, ip, userAgent);
const visitId = uuid(sessionId, visitSalt()); const visitId = uuid(sessionId, visitSalt());

View File

@ -104,14 +104,14 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
await useSession(req, res); await useSession(req, res);
const session = req.session; const session = req.session;
const iat = Math.floor(new Date().getTime() / 1000);
// expire visitId after 30 minutes // expire visitId after 30 minutes
session.visitId = if (session.iat && iat - session.iat > 1800) {
!!session.iat && Math.floor(new Date().getTime() / 1000) - session.iat > 1800 session.visitId = uuid(session.id, visitSalt());
? uuid(session.id, visitSalt()) }
: session.visitId;
session.iat = Math.floor(new Date().getTime() / 1000); session.iat = iat;
if (type === COLLECTION_TYPE.event) { if (type === COLLECTION_TYPE.event) {
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const