replaced email with username
This commit is contained in:
@@ -13,7 +13,7 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
|
||||
if (!session?.user?.email)
|
||||
if (!session?.user?.username)
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
|
||||
const collectionIsAccessible = await getPermission(
|
||||
|
||||
@@ -3,39 +3,66 @@ import NextAuth from "next-auth/next";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import { AuthOptions } from "next-auth";
|
||||
import bcrypt from "bcrypt";
|
||||
import EmailProvider from "next-auth/providers/email";
|
||||
|
||||
export const authOptions: AuthOptions = {
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
},
|
||||
providers: [
|
||||
// EmailProvider({
|
||||
// server: process.env.EMAIL_SERVER,
|
||||
// from: process.env.EMAIL_FROM,
|
||||
// }),
|
||||
CredentialsProvider({
|
||||
type: "credentials",
|
||||
credentials: {},
|
||||
credentials: {
|
||||
username: {
|
||||
label: "Username",
|
||||
type: "text",
|
||||
},
|
||||
password: {
|
||||
label: "Password",
|
||||
type: "password",
|
||||
},
|
||||
},
|
||||
async authorize(credentials, req) {
|
||||
const { email, password } = credentials as {
|
||||
id: number;
|
||||
email: string;
|
||||
password: string;
|
||||
};
|
||||
if (!credentials) return null;
|
||||
|
||||
// const { username, password } = credentials as {
|
||||
// id: number;
|
||||
// username: string;
|
||||
// password: string;
|
||||
// };
|
||||
|
||||
console.log(credentials);
|
||||
|
||||
const findUser = await prisma.user.findFirst({
|
||||
where: {
|
||||
email: email.toLowerCase(),
|
||||
username: credentials.username.toLowerCase(),
|
||||
},
|
||||
});
|
||||
|
||||
let passwordMatches: boolean = false;
|
||||
|
||||
if (findUser?.password) {
|
||||
passwordMatches = bcrypt.compareSync(password, findUser.password);
|
||||
passwordMatches = bcrypt.compareSync(
|
||||
credentials.password,
|
||||
findUser.password
|
||||
);
|
||||
}
|
||||
|
||||
console.log({
|
||||
id: findUser?.id,
|
||||
name: findUser?.name,
|
||||
username: findUser?.username.toLowerCase(),
|
||||
});
|
||||
|
||||
if (passwordMatches) {
|
||||
return {
|
||||
id: findUser?.id,
|
||||
name: findUser?.name,
|
||||
email: findUser?.email.toLowerCase(),
|
||||
email: findUser?.username.toLowerCase(),
|
||||
};
|
||||
} else return null as any;
|
||||
},
|
||||
@@ -46,15 +73,19 @@ export const authOptions: AuthOptions = {
|
||||
},
|
||||
callbacks: {
|
||||
session: async ({ session, token }) => {
|
||||
console.log("TOKEN:", token);
|
||||
session.user.id = parseInt(token?.sub as any);
|
||||
session.user.username = session.user.email;
|
||||
console.log("SESSION:", session);
|
||||
|
||||
return session;
|
||||
},
|
||||
// Using the `...rest` parameter to be able to narrow down the type based on `trigger`
|
||||
jwt({ token, trigger, session }) {
|
||||
if (trigger === "update" && session?.name && session?.email) {
|
||||
if (trigger === "update" && session?.name && session?.username) {
|
||||
// Note, that `session` can be any arbitrary object, remember to validate it!
|
||||
token.name = session.name;
|
||||
token.username = session.username.toLowerCase();
|
||||
token.email = session.email.toLowerCase();
|
||||
}
|
||||
return token;
|
||||
|
||||
@@ -8,7 +8,7 @@ interface Data {
|
||||
|
||||
interface User {
|
||||
name: string;
|
||||
email: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
@@ -18,14 +18,14 @@ export default async function Index(
|
||||
) {
|
||||
const body: User = req.body;
|
||||
|
||||
if (!body.email || !body.password || !body.name)
|
||||
if (!body.username || !body.password || !body.name)
|
||||
return res
|
||||
.status(400)
|
||||
.json({ response: "Please fill out all the fields." });
|
||||
|
||||
const checkIfUserExists = await prisma.user.findFirst({
|
||||
where: {
|
||||
email: body.email.toLowerCase(),
|
||||
username: body.username.toLowerCase(),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -37,7 +37,7 @@ export default async function Index(
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
email: body.email.toLowerCase(),
|
||||
username: body.username.toLowerCase(),
|
||||
password: hashedPassword,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -8,7 +8,7 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
|
||||
const userId = session?.user.id;
|
||||
const userEmail = session?.user.email?.toLowerCase();
|
||||
const userName = session?.user.username?.toLowerCase();
|
||||
const queryId = Number(req.query.id);
|
||||
|
||||
if (!queryId)
|
||||
@@ -17,7 +17,7 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||
.status(401)
|
||||
.send("Invalid parameters.");
|
||||
|
||||
if (!userId || !userEmail)
|
||||
if (!userId || !userName)
|
||||
return res
|
||||
.setHeader("Content-Type", "text/plain")
|
||||
.status(401)
|
||||
@@ -32,7 +32,7 @@ export default async function Index(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
if (
|
||||
targetUser?.isPrivate &&
|
||||
!targetUser.whitelistedUsers.includes(userEmail)
|
||||
!targetUser.whitelistedUsers.includes(userName)
|
||||
) {
|
||||
return res
|
||||
.setHeader("Content-Type", "text/plain")
|
||||
|
||||
@@ -12,7 +12,7 @@ export default async function collections(
|
||||
) {
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
|
||||
if (!session?.user?.email) {
|
||||
if (!session?.user?.username) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import updateLink from "@/lib/api/controllers/links/updateLink";
|
||||
export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
|
||||
if (!session?.user?.email) {
|
||||
if (!session?.user?.username) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import getTags from "@/lib/api/controllers/tags/getTags";
|
||||
export default async function tags(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
|
||||
if (!session?.user?.email) {
|
||||
if (!session?.user?.username) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
}
|
||||
|
||||
|
||||
@@ -7,15 +7,15 @@ import updateUser from "@/lib/api/controllers/users/updateUser";
|
||||
export default async function users(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
|
||||
if (!session?.user?.email) {
|
||||
if (!session?.user.username) {
|
||||
return res.status(401).json({ response: "You must be logged in." });
|
||||
}
|
||||
|
||||
const lookupEmail = req.query.email as string;
|
||||
const isSelf = session.user.email === lookupEmail ? true : false;
|
||||
const lookupUsername = req.query.username as string;
|
||||
const isSelf = session.user.username === lookupUsername ? true : false;
|
||||
|
||||
if (req.method === "GET") {
|
||||
const users = await getUsers(lookupEmail, isSelf, session.user.email);
|
||||
const users = await getUsers(lookupUsername, isSelf, session.user.username);
|
||||
return res.status(users.status).json({ response: users.response });
|
||||
} else if (req.method === "PUT" && !req.body.password) {
|
||||
const updated = await updateUser(req.body, session.user.id);
|
||||
|
||||
Reference in New Issue
Block a user