feat(auth): Add authorize page
This commit is contained in:
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function GET() {
|
||||
if (!process.env.SECNEX_API_HOST || !process.env.SECNEX_API_KEY) {
|
||||
if (!process.env.SECNEX_AUTH_API_HOST || !process.env.SECNEX_AUTH_API_KEY) {
|
||||
return NextResponse.json({ success: false, message: "SecNex API host or key is not set" });
|
||||
}
|
||||
const cookieStore = await cookies();
|
||||
@@ -12,12 +12,12 @@ export async function GET() {
|
||||
return NextResponse.json({ success: false, message: "No token found" });
|
||||
}
|
||||
console.log("Token found");
|
||||
const response = await fetch(`${process.env.SECNEX_API_HOST}/logout`, {
|
||||
const response = await fetch(`${process.env.SECNEX_AUTH_API_HOST}/logout`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ token: token.value }),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${process.env.SECNEX_API_KEY}`,
|
||||
"Authorization": `Bearer ${process.env.SECNEX_AUTH_API_KEY}`,
|
||||
},
|
||||
});
|
||||
if (!response.ok) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { cookies } from "next/headers";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function GET() {
|
||||
if (!process.env.SECNEX_API_HOST || !process.env.SECNEX_API_KEY) {
|
||||
if (!process.env.SECNEX_AUTH_API_HOST || !process.env.SECNEX_AUTH_API_KEY) {
|
||||
return NextResponse.json({ success: false, message: "SecNex API host or key is not set" });
|
||||
}
|
||||
const cookieStore = await cookies();
|
||||
@@ -14,12 +14,12 @@ export async function GET() {
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${process.env.SECNEX_API_HOST}/session/info`, {
|
||||
const response = await fetch(`${process.env.SECNEX_AUTH_API_HOST}/session/info`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ token: token.value }),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${process.env.SECNEX_API_KEY}`,
|
||||
"Authorization": `Bearer ${process.env.SECNEX_AUTH_API_KEY}`,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -5,23 +5,40 @@ import { cookies } from "next/headers";
|
||||
|
||||
import { AuthorizeContainer } from "@/components/core/authorize";
|
||||
|
||||
export default async function AuthorizePage({ searchParams }: { searchParams: Promise<{ [key: string]: string | string[] | undefined }> }) {
|
||||
export interface AuthorizeParams {
|
||||
client_id?: string,
|
||||
response_type?: string,
|
||||
redirect_uri?: string,
|
||||
scope?: string,
|
||||
state?: string,
|
||||
}
|
||||
|
||||
export default async function AuthorizePage({ searchParams }: { searchParams: Promise<AuthorizeParams> }) {
|
||||
const params = await searchParams;
|
||||
|
||||
const cookieStore = await cookies();
|
||||
const token = cookieStore.get("token");
|
||||
if (!token) {
|
||||
redirect("/");
|
||||
}
|
||||
|
||||
|
||||
const queryString = new URLSearchParams(
|
||||
Object.entries(params).filter(([, v]) => v !== undefined) as [string, string][]
|
||||
).toString();
|
||||
|
||||
const client_id = params.client_id as string;
|
||||
const redirect_uri = params.redirect_uri as string;
|
||||
const response_type = params.response_type as string || "code";
|
||||
const scope = params.scope as string || "profile email";
|
||||
if (!token) {
|
||||
redirect(`/?returnTo=/authorize?${queryString}`);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex justify-center items-center h-screen">
|
||||
<AuthorizeContainer applicationName="SecNex" applicationUrl="https://secnex.io" client_id={client_id} redirect_uri={redirect_uri} response_type={response_type} scope={scope} />
|
||||
<AuthorizeContainer
|
||||
applicationName="SecNex"
|
||||
applicationUrl="https://secnex.io"
|
||||
client_id={params.client_id || ""}
|
||||
redirect_uri={params.redirect_uri || ""}
|
||||
response_type={params.response_type || "code"}
|
||||
scope={params.scope || "profile email"}
|
||||
returnTo={`/authorize?${queryString}`}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
17
app/page.tsx
17
app/page.tsx
@@ -1,13 +1,26 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { LoginContainer, LoginSuccessContainer } from "@/components/core/login-form";
|
||||
|
||||
// Get the url before redirect to this page
|
||||
export interface HomeParams {
|
||||
returnTo?: string;
|
||||
}
|
||||
|
||||
export default async function Home() {
|
||||
export default async function Home({
|
||||
searchParams
|
||||
}: {
|
||||
searchParams: Promise<HomeParams>
|
||||
}) {
|
||||
const params = await searchParams;
|
||||
const cookieStore = await cookies();
|
||||
const token = cookieStore.get("token");
|
||||
|
||||
// If token exists and we came from a redirect, go back
|
||||
if (token && params.returnTo) {
|
||||
redirect(params.returnTo);
|
||||
}
|
||||
|
||||
if (token) {
|
||||
return (
|
||||
<div className="flex justify-center items-center h-screen">
|
||||
|
||||
Reference in New Issue
Block a user