Skip to content

Commit

Permalink
fix: add session checker (#12)
Browse files Browse the repository at this point in the history
* fix: add session checker

* fix: path
  • Loading branch information
alarv authored Sep 27, 2024
1 parent 6840912 commit 5afc907
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
28 changes: 28 additions & 0 deletions app/api/auth/validate/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NextRequest, NextResponse } from 'next/server';
import {
ApiResponse,
errorResponse,
handleApiResponse,
} from '@/app/util/response';
import { auth } from '@/auth';
import { isAuthenticated } from '@/lib/utils';

export async function GET(
request: NextRequest,
): Promise<NextResponse<ApiResponse>> {
const session = await auth();
if (!isAuthenticated(session)) {
return errorResponse(
'You need to be authenticated to access this endpoint',
401,
);
}
const res = await fetch(`${process.env.API_URL}/v1/auth/validate`, {
headers: {
Authorization: `Bearer ${session!.token}`,
'Content-Type': 'application/json',
},
});

return handleApiResponse(res);
}
22 changes: 13 additions & 9 deletions app/api/leads/route.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import { auth } from "@/auth";
import { NextResponse } from "next/server";
import { isAuthenticated } from "@/lib/utils";
import { ApiResponse, errorResponse, handleApiResponse } from "@/lib/response";
import { auth } from '@/auth';
import { NextResponse } from 'next/server';
import { isAuthenticated } from '@/lib/utils';
import {
ApiResponse,
errorResponse,
handleApiResponse,
} from '@/app/util/response';

export async function POST(
request: Request,
): Promise<NextResponse<ApiResponse>> {
const session = await auth();
if (!isAuthenticated(session)) {
return errorResponse(
"You need to be authenticated to access this endpoint",
'You need to be authenticated to access this endpoint',
401,
);
}

const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/v1/leads`, {
method: "POST",
method: 'POST',
headers: {
Authorization: `Bearer ${session!.token}`,
"Content-Type": "application/json",
'Content-Type': 'application/json',
},
});

const apiResponse = await handleApiResponse(res);
const data = await apiResponse.json();

if (data.success) {
const leadUrl = res.headers.get("Location")!;
const leadUrlParts = leadUrl.split("/");
const leadUrl = res.headers.get('Location')!;
const leadUrlParts = leadUrl.split('/');
const leadId = leadUrlParts[leadUrlParts.length - 1];

return NextResponse.json(
Expand Down
18 changes: 18 additions & 0 deletions app/components/SessionChecker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use client';

import { useEffect } from 'react';
import { signOut } from 'next-auth/react';

export default function SessionChecker() {
useEffect(() => {
fetch(`/api/auth/validate`)
.then(async (res) => {
if (!res.ok && res.status === 401) {
await signOut({ redirect: false });
}
})
.catch(() => signOut({ redirect: false }));
});

return null;
}
2 changes: 1 addition & 1 deletion app/pricing/components/Pricing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button } from '@nextui-org/button';
import { signIn, useSession } from 'next-auth/react';
import { isAuthenticated } from '@/lib/utils';
import toast from 'react-hot-toast';
import { ApiResponse } from '@/lib/response';
import { ApiResponse } from '@/app/util/response';
import { useCallback, useState } from 'react';

export default function Pricing() {
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import { NextUIProvider } from '@nextui-org/react';
import { useRouter } from 'next/navigation';
import { Toaster } from 'react-hot-toast';
import SessionChecker from '@/app/components/SessionChecker';

export function Providers({ children }: { children: React.ReactNode }) {
const router = useRouter();

return (
<>
<NextUIProvider navigate={router.push}>
<SessionChecker />
<div>
<Toaster toastOptions={{ duration: 7500 }} />
</div>
Expand Down

0 comments on commit 5afc907

Please sign in to comment.