Skip to content

Commit

Permalink
fix an error with mismatched variables causing a login issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jacc committed Jun 16, 2024
1 parent 90dc366 commit 0dc8636
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
53 changes: 29 additions & 24 deletions src/pages/api/oauth/callback.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as schema from '$drizzle/schema';
import { db } from '@/db';
import * as schema from "$drizzle/schema";
import { db } from "@/db";
import { getCookie, setCookie } from "cookies-next";
import crypto from "crypto";
import { eq } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next";
import { createToken } from '../saves';
import { createToken } from "../saves";

type Data = Record<string, any>;

Expand All @@ -22,6 +22,7 @@ export default async function handler(
}

const uid = getCookie("uid", { req });

if (!uid || typeof uid !== "string") {
res.status(400).end();
res.redirect("/");
Expand Down Expand Up @@ -80,12 +81,21 @@ export default async function handler(

const discordUserData = await discordUser.json();

let [user] = await db.select().from(schema.users).where(eq(schema.users.id, uid)).limit(1);
let [user] = await db
.select()
.from(schema.users)
.where(eq(schema.users.id, uid))
.limit(1);

let cookieSecret =
user?.cookie_secret ?? crypto.randomBytes(16).toString("hex");

if (!user) {
let [discordUser] = await db.select().from(schema.users).where(eq(schema.users.id, discordData.uid)).limit(1);
let [discordUser] = await db
.select()
.from(schema.users)
.where(eq(schema.users.discord_id, discordUserData.id))
.limit(1);

if (discordUser) {
user = discordUser;
Expand All @@ -97,10 +107,6 @@ export default async function handler(
.update(schema.users)
.set({ discord_name: discordUserData.username })
.where(eq(schema.saves.user_id, discordUserData.id));
// const r = await conn.execute(
// "UPDATE Users SET discord_name = ? WHERE discord_id = ?",
// [discordUserData.username, discordUserData.id],
// );
}

// update discord avatar if the avatar hash changed
Expand All @@ -109,26 +115,25 @@ export default async function handler(
.update(schema.users)
.set({ discord_avatar: discordUserData.avatar })
.where(eq(schema.saves.user_id, discordUserData.id));
// const r = await conn.execute(
// "UPDATE Users SET discord_avatar = ? WHERE discord_id = ?",
// [discordUserData.avatar, discordUserData.id],
// );
}
} else {
await db.insert(schema.users).values({
id: uid,
discord_id: discordUserData.id,
discord_name: discordUserData.username,
discord_avatar: discordUserData.avatar,
cookie_secret: cookieSecret
}).onDuplicateKeyUpdate({
set: {
await db
.insert(schema.users)
.values({
id: uid,
discord_id: discordUserData.id,
discord_name: discordUserData.username,
discord_avatar: discordUserData.avatar,
cookie_secret: cookieSecret
}
});
cookie_secret: cookieSecret,
})
.onDuplicateKeyUpdate({
set: {
discord_id: discordUserData.id,
discord_name: discordUserData.username,
discord_avatar: discordUserData.avatar,
cookie_secret: cookieSecret,
},
});
// await conn.execute(
// "INSERT INTO Users (id, discord_id, discord_name, discord_avatar, cookie_secret) VALUES (?, ?, ?, ?, ?)",
// [
Expand Down
49 changes: 23 additions & 26 deletions src/pages/api/saves/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { db } from '$db';
import * as schema from '$drizzle/schema';
import { db } from "$db";
import * as schema from "$drizzle/schema";
import { getCookie, setCookie } from "cookies-next";
import crypto from "crypto";
import { and, eq } from "drizzle-orm";
import { NextApiRequest, NextApiResponse } from "next";


type Data = Record<string, any>;

export interface SqlUser {
Expand Down Expand Up @@ -38,17 +37,15 @@ export async function getUID(
req: NextApiRequest,
res: NextApiResponse<Data>,
): Promise<string> {
// console.log("Getting UID from cookie...");
let uid = getCookie("uid", { req, res });
// console.log("UID: ", uid);
if (uid && typeof uid === "string") {
// console.log("Found UID...");
// uids can be anonymous, so we need to check if the user exists


// yeah this is correct now but eq needs to come from drizzle-orm/mysql-core or sm no its fine its cuz cookies are weird
// one secn
const [user] = await db.select().from(schema.users).where(eq(schema.users.id, uid)).limit(1);
const [user] = await db
.select()
.from(schema.users)
.where(eq(schema.users.id, uid))
.limit(1);

if (user) {
// user exists, so we check if the user is authenticated
Expand All @@ -71,7 +68,7 @@ export async function getUID(
// everything is ok, so we return the uid
return uid as string;
} else {
// console.log("Generating new UID...");
console.log("Generating new UID...");
// no uid, so we create an anonymous one
uid = crypto.randomBytes(16).toString("hex");
setCookie("uid", uid, {
Expand Down Expand Up @@ -117,10 +114,11 @@ export const verifyToken = (token: string, key: string) => {
};

async function get(req: NextApiRequest, res: NextApiResponse) {
// console.log("Getting...");
const uid = await getUID(req, res);
// console.log("uid: ", uid);
const players = await db.select().from(schema.saves).where(eq(schema.saves.user_id, uid));
const players = await db
.select()
.from(schema.saves)
.where(eq(schema.saves.user_id, uid));
res.json(players);
}

Expand All @@ -132,11 +130,14 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
for (const player of players) {
try {
if (player._id) {
await db.insert(schema.saves).values({
_id: player._id,
user_id: uid,
...player
}).onDuplicateKeyUpdate({ set: player });
await db
.insert(schema.saves)
.values({
_id: player._id,
user_id: uid,
...player,
})
.onDuplicateKeyUpdate({ set: player });
}
res.status(200).end();
} catch (e) {
Expand Down Expand Up @@ -164,13 +165,9 @@ async function _delete(req: NextApiRequest, res: NextApiResponse) {
if (type === "player") {
// delete a single player
const { _id } = JSON.parse(req.body);
await db.delete(schema.saves)
.where(
and(
eq(schema.saves.user_id, uid),
eq(schema.saves._id, _id)
)
);
await db
.delete(schema.saves)
.where(and(eq(schema.saves.user_id, uid), eq(schema.saves._id, _id)));

// const result = await conn.execute(
// "DELETE FROM Saves WHERE user_id = ? AND _id = ?",
Expand Down

0 comments on commit 0dc8636

Please sign in to comment.