diff --git a/app/(private)/layout.tsx b/app/(private)/layout.tsx index d8fe5f6..f2e611b 100644 --- a/app/(private)/layout.tsx +++ b/app/(private)/layout.tsx @@ -10,5 +10,5 @@ interface Props { export default async function RootLayout({ children }: Props) { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) redirect('/auth/login'); - return <>{children}; + return <>{session?.user && children}; } diff --git a/app/api/list/route.ts b/app/api/list/route.ts index c4d5d30..2d7cf70 100644 --- a/app/api/list/route.ts +++ b/app/api/list/route.ts @@ -9,7 +9,7 @@ export async function GET(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json([], { status: 200 }); } await startDb(); let list: any[] = []; diff --git a/app/api/notes/archived/route.ts b/app/api/notes/archived/route.ts index 890bf08..42fddea 100644 --- a/app/api/notes/archived/route.ts +++ b/app/api/notes/archived/route.ts @@ -9,7 +9,7 @@ export async function GET() { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } await startDb(); const note = await Note.find({ diff --git a/app/api/notes/route.ts b/app/api/notes/route.ts index 6eb1435..b25c03c 100644 --- a/app/api/notes/route.ts +++ b/app/api/notes/route.ts @@ -9,7 +9,7 @@ export async function GET() { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } await startDb(); const note = await Note.find({ @@ -27,7 +27,7 @@ export async function POST(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } const body = await req.json(); await startDb(); @@ -42,7 +42,7 @@ export async function PUT(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } const body = await req.json(); await startDb(); @@ -57,7 +57,7 @@ export async function DELETE(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } await startDb(); await Note.findByIdAndDelete(req.nextUrl.searchParams.get('_id')); diff --git a/app/api/notes/trashed/route.ts b/app/api/notes/trashed/route.ts index 4890eaf..b68f94f 100644 --- a/app/api/notes/trashed/route.ts +++ b/app/api/notes/trashed/route.ts @@ -9,7 +9,7 @@ export async function GET() { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } await startDb(); const note = await Note.find({ diff --git a/app/api/todos/route.ts b/app/api/todos/route.ts index 6bfc9f3..9fb0a02 100644 --- a/app/api/todos/route.ts +++ b/app/api/todos/route.ts @@ -19,7 +19,7 @@ export async function GET(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } const user = session?.user._id; await startDb(); @@ -59,7 +59,7 @@ export async function POST(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } const body = await req.json(); await startDb(); @@ -79,7 +79,7 @@ export async function PUT(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } const body = await req.json(); await startDb(); @@ -94,7 +94,7 @@ export async function DELETE(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } await startDb(); await Todo.findByIdAndDelete(req.nextUrl.searchParams.get('_id')); diff --git a/app/api/todos/todo-list/route.ts b/app/api/todos/todo-list/route.ts index a0d9e74..187765b 100644 --- a/app/api/todos/todo-list/route.ts +++ b/app/api/todos/todo-list/route.ts @@ -9,7 +9,7 @@ export async function GET() { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } await startDb(); const todoList = await TodoList.find({ user: session?.user._id }).sort('-updatedAt'); @@ -22,7 +22,7 @@ export async function POST(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } const body = await req.json(); let todoList; @@ -44,7 +44,7 @@ export async function DELETE(req: NextRequest) { try { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); } await startDb(); await TodoList.findByIdAndDelete(req.nextUrl.searchParams.get('_id')); diff --git a/lib/client_functions.tsx b/lib/client_functions.tsx index e0881be..738204c 100644 --- a/lib/client_functions.tsx +++ b/lib/client_functions.tsx @@ -64,10 +64,16 @@ export const apiCall = async (url: string, body?: any, method: string = 'GET') = default: break; } - } catch (error) { - failure('Error while calling API'); + } catch (error: any) { + failure(error?.response?.data.error || 'Error while calling API'); } finally { nprogress.complete(); } return res; }; + +export const getInitials = (name: string | undefined | null) => + name + ?.split(' ') + .map((n) => n[0]) + .join('');