support for bearer tokens

This commit is contained in:
daniel31x13
2023-11-02 14:59:31 -04:00
parent b458fad567
commit ae1889e757
27 changed files with 148 additions and 436 deletions
+21 -11
View File
@@ -1,21 +1,31 @@
import { NextApiRequest } from "next";
import { NextApiRequest, NextApiResponse } from "next";
import { getToken } from "next-auth/jwt";
import { prisma } from "./db";
import { User } from "@prisma/client";
type Props = {
req: NextApiRequest;
res: NextApiResponse;
};
export default async function authenticateUser({ req }: Props) {
export default async function authenticateUser({
req,
res,
}: Props): Promise<User | null> {
const token = await getToken({ req });
const userId = token?.id;
if (!token?.id) {
return { response: "You must be logged in.", status: 401 };
} else if (token.isSubscriber === false)
return {
response:
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app in case of any issues.",
status: 401,
};
if (!userId) {
res.status(401).json({ message: "You must be logged in." });
return null;
} else if (token.isSubscriber === false) {
res.status(401).json({
message:
"You are not a subscriber, feel free to reach out to us at support@linkwarden.app if you think this is an issue.",
});
return null;
}
return token;
const user = await prisma.user.findUnique({ where: { id: userId } });
return user;
}
@@ -10,12 +10,7 @@ const emailEnabled =
process.env.EMAIL_FROM && process.env.EMAIL_SERVER ? true : false;
export default async function updateUserById(
sessionUser: {
id: number;
username: string;
email: string;
isSubscriber: boolean;
},
userId: number,
data: AccountSettings
) {
if (emailEnabled && !data.email)
@@ -49,7 +44,7 @@ export default async function updateUserById(
const userIsTaken = await prisma.user.findFirst({
where: {
id: { not: sessionUser.id },
id: { not: userId },
OR: emailEnabled
? [
{
@@ -97,7 +92,7 @@ export default async function updateUserById(
createFolder({ filePath: `uploads/avatar` });
await createFile({
filePath: `uploads/avatar/${sessionUser.id}.jpg`,
filePath: `uploads/avatar/${userId}.jpg`,
data: base64Data,
isBase64: true,
});
@@ -112,9 +107,13 @@ export default async function updateUserById(
};
}
} else if (data.image == "") {
removeFile({ filePath: `uploads/avatar/${sessionUser.id}.jpg` });
removeFile({ filePath: `uploads/avatar/${userId}.jpg` });
}
const previousEmail = (
await prisma.user.findUnique({ where: { id: userId } })
)?.email;
// Other settings
const saltRounds = 10;
@@ -122,14 +121,14 @@ export default async function updateUserById(
const updatedUser = await prisma.user.update({
where: {
id: sessionUser.id,
id: userId,
},
data: {
name: data.name,
username: data.username.toLowerCase().trim(),
email: data.email?.toLowerCase().trim(),
isPrivate: data.isPrivate,
image: data.image ? `uploads/avatar/${sessionUser.id}.jpg` : "",
image: data.image ? `uploads/avatar/${userId}.jpg` : "",
archiveAsScreenshot: data.archiveAsScreenshot,
archiveAsPDF: data.archiveAsPDF,
archiveAsWaybackMachine: data.archiveAsWaybackMachine,
@@ -167,7 +166,7 @@ export default async function updateUserById(
// Delete whitelistedUsers that are not present in the new list
await prisma.whitelistedUser.deleteMany({
where: {
userId: sessionUser.id,
userId: userId,
username: {
in: usernamesToDelete,
},
@@ -179,17 +178,17 @@ export default async function updateUserById(
await prisma.whitelistedUser.create({
data: {
username,
userId: sessionUser.id,
userId: userId,
},
});
}
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
if (STRIPE_SECRET_KEY && emailEnabled && sessionUser.email !== data.email)
if (STRIPE_SECRET_KEY && emailEnabled && previousEmail !== data.email)
await updateCustomerEmail(
STRIPE_SECRET_KEY,
sessionUser.email,
previousEmail as string,
data.email as string
);