mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 22:53:25 +00:00
fix: redirect user if not logged in
This commit is contained in:
@@ -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 });
|
||||
|
||||
@@ -11,6 +11,9 @@ export default async function getUserCategories(user: User) {
|
||||
where: {
|
||||
authorId: user?.id,
|
||||
},
|
||||
include: {
|
||||
category: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user