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

Commit

Permalink
fix: account stats own visitor (#5595) (#5626)
Browse files Browse the repository at this point in the history
* fix: account stats own visitor (#5595)

* added user auth in users api

* removed console

* getUserApi function changed

* Update pages/api/users/[username]/index.js

Co-authored-by: Eddie Jaoude <eddie@jaoudestudios.com>

---------

Co-authored-by: Eddie Jaoude <eddie@jaoudestudios.com>

* fix: profile increment for owner #5311

* fix: removed debug code

---------

Co-authored-by: Akhil Bisht <92508481+Akhilbisht798@users.noreply.github.com>
  • Loading branch information
eddiejaoude and Akhilbisht798 committed Mar 31, 2023
1 parent 0920889 commit 4727d7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 3 additions & 3 deletions pages/[username].js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import requestIp from "request-ip";
import { remark } from "remark";
import strip from "strip-markdown";

import { getUserApi } from "./api/users/[username]/index";
import singleUser from "@config/user.json";
import logger from "@config/logger";
import { getUserApi } from "./api/users/[username]/index";
import Link from "@components/Link";
import PageHead from "@components/PageHead";
import SingleLayout from "@components/layouts/SingleLayout";
Expand All @@ -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
18 changes: 13 additions & 5 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 @@ -15,12 +18,17 @@ export default async function handler(req, res) {
.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) {
await connectMongo();
let isOwner = false;
const session = await unstable_getServerSession(req, res, authOptions);
if (session && session.username === username) {
isOwner = true;
}

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

if (getProfile) {
if (getProfile && !isOwner) {
try {
await Profile.updateOne(
{
Expand All @@ -89,7 +97,7 @@ export async function getUserApi(username) {
username: username,
date: date,
});
if (getProfileStats) {
if (getProfileStats && !isOwner) {
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 && !isOwner) {
try {
await Stats.updateOne(
{
Expand Down

0 comments on commit 4727d7a

Please sign in to comment.