Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

fix: account stats own visitor (#5595) #5626

Merged
merged 3 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pages/[username].js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import Page from "@components/Page";
import UserPage from "@components/user/UserPage";

export async function getServerSideProps(context) {
const { req } = context;
const { req, res } = context;
const username = context.query.username;
const log = logger.child({
username: username,
ip: requestIp.getClientIp(req),
});

const { status, profile } = await getUserApi(username);
const { status, profile } = await getUserApi(req, res, username);
if (status !== 200) {
log.error(
profile.error,
Expand Down
7 changes: 6 additions & 1 deletion pages/account/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export async function getServerSideProps(context) {
let profile = {};
try {
const resUser = await fetch(
`${process.env.NEXT_PUBLIC_BASE_URL}/api/users/${username}`
`${process.env.NEXT_PUBLIC_BASE_URL}/api/users/${username}`,
{
headers: {
cookie: context.req.headers.cookie || "",
},
}
);
profile = await resUser.json();
} catch (e) {
Expand Down
20 changes: 14 additions & 6 deletions pages/api/users/[username]/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { authOptions } from "../../auth/[...nextauth]";
import { unstable_getServerSession } from 'next-auth/next';

import connectMongo from "@config/mongo";
import logger from "@config/logger";

Expand All @@ -14,12 +17,17 @@ export default async function handler(req, res) {
.status(400)
.json({ error: "Invalid request: GET request required" });
}

const { status, profile } = await getUserApi(req.query.username);
const { status, profile } = await getUserApi(req, res, req.query.username);
return res.status(status).json(profile);
}

export async function getUserApi(username) {
export async function getUserApi(req, res, username) {
let sameUser = false;
const session = await unstable_getServerSession(req, res, authOptions);
if (session && session.username === username) {
sameUser = true;
}
await connectMongo();

const log = logger.child({ username: username });
Expand Down Expand Up @@ -66,7 +74,7 @@ export async function getUserApi(username) {
}
}

if (getProfile) {
if (getProfile && !sameUser) {
try {
await Profile.updateOne(
{
Expand All @@ -89,7 +97,7 @@ export async function getUserApi(username) {
username: username,
date: date,
});
if (getProfileStats) {
if (getProfileStats && !sameUser) {
try {
await ProfileStats.updateOne(
{
Expand Down Expand Up @@ -124,7 +132,7 @@ export async function getUserApi(username) {
}

const getPlatformStats = await Stats.findOne({ date });
if (getPlatformStats) {
if (getPlatformStats && !sameUser) {
try {
await Stats.updateOne(
{
Expand Down