fix: redirect user if not logged in

This commit is contained in:
Sonny
2023-06-04 23:54:51 +02:00
parent f1326bc06e
commit 9d4c3cbdd9
12 changed files with 67 additions and 22 deletions

View File

@@ -4,7 +4,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import { Session } from "next-auth";
import getUserOrThrow from "lib/user/getUserOrThrow";
import { getSessionOrThrow } from "utils/session";
import { getSession } from "utils/session";
type ApiHandlerMethod = ({
req,
@@ -36,7 +36,7 @@ export function apiHandler(handler: {
.json({ error: `Method ${req.method} Not Allowed` });
try {
const session = await getSessionOrThrow(req, res);
const session = await getSession(req, res);
const user = await getUserOrThrow(session);
await handler[method]({ req, res, session, user });

View File

@@ -11,6 +11,9 @@ export default async function getUserCategories(user: User) {
where: {
authorId: user?.id,
},
include: {
category: true,
},
},
},
});

View File

@@ -2,6 +2,9 @@ import { Session } from "next-auth";
import prisma from "utils/prisma";
export default async function getUser(session: Session) {
if (!session?.user) {
return null;
}
return await prisma.user.findFirst({
where: {
email: session?.user?.email,

View File

@@ -81,6 +81,13 @@ export default CreateCategory;
export async function getServerSideProps({ req, res }) {
const session = await getSession(req, res);
const user = await getUser(session);
if (!user) {
return {
redirect: {
destination: PATHS.HOME,
},
};
}
const categoriesCount = await getUserCategoriesCount(user);
return {

View File

@@ -81,6 +81,13 @@ export async function getServerSideProps({ req, res, query }) {
const session = await getSession(req, res);
const user = await getUser(session);
if (!user) {
return {
redirect: {
destination: PATHS.HOME,
},
};
}
const category = await getUserCategory(user, Number(cid));
if (!category) {

View File

@@ -88,6 +88,13 @@ export async function getServerSideProps({ req, res, query }) {
const session = await getSession(req, res);
const user = await getUser(session);
if (!user) {
return {
redirect: {
destination: PATHS.HOME,
},
};
}
const category = await getUserCategory(user, Number(cid));
if (!category) {

View File

@@ -187,10 +187,18 @@ function Home(props: HomePageProps) {
}
export async function getServerSideProps({ req, res, query }) {
const session = await getSession(req, res);
const queryCategoryId = (query?.categoryId as string) || "";
const session = await getSession(req, res);
const user = await getUser(session);
if (!user) {
return {
redirect: {
destination: PATHS.LOGIN,
},
};
}
const categories = await getUserCategories(user);
if (categories.length === 0) {
return {

View File

@@ -116,6 +116,13 @@ export default CreateLink;
export async function getServerSideProps({ req, res }) {
const session = await getSession(req, res);
const user = await getUser(session);
if (!user) {
return {
redirect: {
destination: PATHS.HOME,
},
};
}
const categories = await getUserCategories(user);
if (categories.length === 0) {

View File

@@ -12,13 +12,13 @@ import TextBox from "components/TextBox";
import useAutoFocus from "hooks/useAutoFocus";
import { Category, Link } from "types";
import { HandleAxiosError, IsValidURL } from "utils/front";
import { getSessionOrThrow } from "utils/session";
import { getSession } from "utils/session";
import getUserCategories from "lib/category/getUserCategories";
import getUserLink from "lib/link/getUserLink";
import getUserOrThrow from "lib/user/getUserOrThrow";
import PATHS from "constants/paths";
import getUser from "lib/user/getUser";
import styles from "styles/create.module.scss";
function EditLink({
@@ -136,10 +136,17 @@ export default EditLink;
export async function getServerSideProps({ req, res, query }) {
const { lid } = query;
const session = await getSessionOrThrow(req, res);
const user = await getUserOrThrow(session);
const categories = await getUserCategories(user);
const session = await getSession(req, res);
const user = await getUser(session);
if (!user) {
return {
redirect: {
destination: PATHS.HOME,
},
};
}
const categories = await getUserCategories(user);
const link = await getUserLink(user, Number(lid));
if (!link) {
return {

View File

@@ -102,6 +102,13 @@ export async function getServerSideProps({ req, res, query }) {
const session = await getSession(req, res);
const user = await getUser(session);
if (!user) {
return {
redirect: {
destination: PATHS.HOME,
},
};
}
const link = await getUserLink(user, Number(lid));
if (!link) {

View File

@@ -7,6 +7,7 @@ import MessageManager from "components/MessageManager/MessageManager";
import PATHS from "constants/paths";
import { getSession } from "utils/session";
import getUser from "lib/user/getUser";
import styles from "styles/login.module.scss";
interface SignInProps {
@@ -39,7 +40,8 @@ export default function SignIn({ providers }: SignInProps) {
export async function getServerSideProps({ req, res }) {
const session = await getSession(req, res);
if (session) {
const user = await getUser(session);
if (user) {
return {
redirect: {
destination: PATHS.HOME,

View File

@@ -6,16 +6,3 @@ import { authOptions } from "pages/api/auth/[...nextauth]";
export async function getSession(req: NextApiRequest, res: NextApiResponse) {
return await getServerSession(req, res, authOptions);
}
export async function getSessionOrThrow(
req: NextApiRequest,
res: NextApiResponse
) {
const session = await getSession(req, res);
if (!session) {
throw new Error("You must be connected");
}
return session;
}