Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: check if the user exists when checking the auth on api routes #44

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/app/api/auth/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ResponseBody {

export const POST = async (request: NextRequest) => {
const { token, username } = await request.json();
const userId = getUserId(token);
const userId = await getUserId(token);

if (!userId) {
return NotAuthenticatedResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/claim-reward/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isGameConcluded } from "@/lib/game.config";
export const POST = async (request: NextRequest) => {
const { address } = await request.json();
const token = request.cookies.get(TOKEN_COOKIE)?.value;
const userId = getUserId(token);
const userId = await getUserId(token);

if (!userId) {
return NotAuthenticatedResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/connect/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const decryptData = async (
export const POST = async (request: NextRequest) => {
const { id, question_id, choice } = await request.json();
const token = request.cookies.get(TOKEN_COOKIE)?.value;
const userId = getUserId(token);
const userId = await getUserId(token);

if (!userId) {
return NotAuthenticatedResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/question/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const decryptData = async (
export const GET = async (request: NextRequest) => {
const id = new URL(request.url).searchParams.get("id");
const token = request.cookies.get(TOKEN_COOKIE)?.value;
const userId = getUserId(token);
const userId = await getUserId(token);

if (!userId) {
return NotAuthenticatedResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/userstats/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getUserStats } from "@/lib/supabase/queries";

export const GET = async (request: NextRequest) => {
const token = request.cookies.get(TOKEN_COOKIE)?.value;
const userId = getUserId(token);
const userId = await getUserId(token);

if (!userId) {
return NotAuthenticatedResponse;
Expand Down
10 changes: 8 additions & 2 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { NextResponse } from "next/server";
import jwt, { JwtPayload } from "jsonwebtoken";
import { UUID } from "crypto";
import { checkUserExists } from "@/lib/supabase/queries";

const JWT_SECRET_KEY = process.env.SECRET_KEY ?? "";
export const TOKEN_COOKIE = "token";

export const getUserId: (arg0?: string) => UUID | false = (token) => {
export const getUserId: (arg0?: string) => Promise<UUID | false> = async (token) => {
if (typeof(token) === "undefined") return false;
try {
const payload = jwt.verify(token, JWT_SECRET_KEY) as JwtPayload;
return payload.user_id;
const userId = payload.user_id;
const userExists = await checkUserExists(userId);
if (!userExists) {
throw new Error("User not registered yet.")
}
return userId;
} catch {
return false;
}
Expand Down
Loading