replaced email with username
This commit is contained in:
+7
-1
@@ -5,8 +5,14 @@ import type { AppProps } from "next/app";
|
||||
import Head from "next/head";
|
||||
import AuthRedirect from "@/layouts/AuthRedirect";
|
||||
import { Toaster } from "react-hot-toast";
|
||||
import { Session } from "next-auth";
|
||||
|
||||
export default function App({ Component, pageProps }: AppProps) {
|
||||
export default function App({
|
||||
Component,
|
||||
pageProps,
|
||||
}: AppProps<{
|
||||
session: Session;
|
||||
}>) {
|
||||
return (
|
||||
<SessionProvider session={pageProps.session}>
|
||||
<Head>
|
||||
|
||||
@@ -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);
|
||||
|
||||
+8
-3
@@ -22,18 +22,23 @@ export default function Dashboard() {
|
||||
const [numberOfLinks, setNumberOfLinks] = useState(0);
|
||||
|
||||
const [tagPinDisclosure, setTagPinDisclosure] = useState<boolean>(() => {
|
||||
const storedValue = localStorage.getItem("tagPinDisclosure");
|
||||
const storedValue =
|
||||
typeof window !== "undefined" && localStorage.getItem("tagPinDisclosure");
|
||||
return storedValue ? storedValue === "true" : true;
|
||||
});
|
||||
|
||||
const [collectionPinDisclosure, setCollectionPinDisclosure] =
|
||||
useState<boolean>(() => {
|
||||
const storedValue = localStorage.getItem("collectionPinDisclosure");
|
||||
const storedValue =
|
||||
typeof window !== "undefined" &&
|
||||
localStorage.getItem("collectionPinDisclosure");
|
||||
return storedValue ? storedValue === "true" : true;
|
||||
});
|
||||
|
||||
const [linkPinDisclosure, setLinkPinDisclosure] = useState<boolean>(() => {
|
||||
const storedValue = localStorage.getItem("linkPinDisclosure");
|
||||
const storedValue =
|
||||
typeof window !== "undefined" &&
|
||||
localStorage.getItem("linkPinDisclosure");
|
||||
return storedValue ? storedValue === "true" : true;
|
||||
});
|
||||
|
||||
|
||||
+7
-7
@@ -5,7 +5,7 @@ import { useState } from "react";
|
||||
import { toast } from "react-hot-toast";
|
||||
|
||||
interface FormData {
|
||||
email: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
@@ -13,18 +13,18 @@ export default function Login() {
|
||||
const [submitLoader, setSubmitLoader] = useState(false);
|
||||
|
||||
const [form, setForm] = useState<FormData>({
|
||||
email: "",
|
||||
username: "",
|
||||
password: "",
|
||||
});
|
||||
|
||||
async function loginUser() {
|
||||
if (form.email !== "" && form.password !== "") {
|
||||
if (form.username !== "" && form.password !== "") {
|
||||
setSubmitLoader(true);
|
||||
|
||||
const load = toast.loading("Authenticating...");
|
||||
|
||||
const res = await signIn("credentials", {
|
||||
email: form.email,
|
||||
username: form.username,
|
||||
password: form.password,
|
||||
redirect: false,
|
||||
});
|
||||
@@ -54,13 +54,13 @@ export default function Login() {
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<p className="text-sm text-sky-500 w-fit font-semibold">Email</p>
|
||||
<p className="text-sm text-sky-500 w-fit font-semibold">Username</p>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
placeholder="johnny@example.com"
|
||||
value={form.email}
|
||||
onChange={(e) => setForm({ ...form, email: e.target.value })}
|
||||
value={form.username}
|
||||
onChange={(e) => setForm({ ...form, username: e.target.value })}
|
||||
className="w-full rounded-md p-3 mx-auto border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"
|
||||
/>
|
||||
|
||||
|
||||
+7
-7
@@ -6,7 +6,7 @@ import SubmitButton from "@/components/SubmitButton";
|
||||
|
||||
interface FormData {
|
||||
name: string;
|
||||
email: string;
|
||||
username: string;
|
||||
password: string;
|
||||
passwordConfirmation: string;
|
||||
}
|
||||
@@ -18,7 +18,7 @@ export default function Register() {
|
||||
|
||||
const [form, setForm] = useState<FormData>({
|
||||
name: "",
|
||||
email: "",
|
||||
username: "",
|
||||
password: "",
|
||||
passwordConfirmation: "",
|
||||
});
|
||||
@@ -26,7 +26,7 @@ export default function Register() {
|
||||
async function registerUser() {
|
||||
if (
|
||||
form.name !== "" &&
|
||||
form.email !== "" &&
|
||||
form.username !== "" &&
|
||||
form.password !== "" &&
|
||||
form.passwordConfirmation !== ""
|
||||
) {
|
||||
@@ -54,7 +54,7 @@ export default function Register() {
|
||||
if (response.ok) {
|
||||
setForm({
|
||||
name: "",
|
||||
email: "",
|
||||
username: "",
|
||||
password: "",
|
||||
passwordConfirmation: "",
|
||||
});
|
||||
@@ -96,13 +96,13 @@ export default function Register() {
|
||||
className="w-full rounded-md p-3 mx-auto border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"
|
||||
/>
|
||||
|
||||
<p className="text-sm text-sky-500 w-fit font-semibold">Email</p>
|
||||
<p className="text-sm text-sky-500 w-fit font-semibold">Username</p>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
placeholder="johnny@example.com"
|
||||
value={form.email}
|
||||
onChange={(e) => setForm({ ...form, email: e.target.value })}
|
||||
value={form.username}
|
||||
onChange={(e) => setForm({ ...form, username: e.target.value })}
|
||||
className="w-full rounded-md p-3 mx-auto border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"
|
||||
/>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user