Skip to content

Commit

Permalink
fix: api error notification | unauthorized error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkondle-dev committed Jun 24, 2024
1 parent 812c523 commit 4f69d6d
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/(private)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}</>;
}
2 changes: 1 addition & 1 deletion app/api/list/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
2 changes: 1 addition & 1 deletion app/api/notes/archived/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 4 additions & 4 deletions app/api/notes/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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'));
Expand Down
2 changes: 1 addition & 1 deletion app/api/notes/trashed/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 4 additions & 4 deletions app/api/todos/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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'));
Expand Down
6 changes: 3 additions & 3 deletions app/api/todos/todo-list/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
Expand All @@ -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'));
Expand Down
10 changes: 8 additions & 2 deletions lib/client_functions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('');

0 comments on commit 4f69d6d

Please sign in to comment.