fixed the auth issue
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { StatusCodes } from "http-status-codes";
|
import { StatusCodes } from "http-status-codes";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: NextRequest) {
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const code = searchParams.get("code");
|
const code = searchParams.get("code");
|
||||||
|
|
||||||
|
console.log(request.url, request.nextUrl);
|
||||||
|
|
||||||
if (!code) {
|
if (!code) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "No code provided" },
|
{ error: "No code provided" },
|
||||||
{ status: StatusCodes.BAD_REQUEST }
|
{ status: StatusCodes.BAD_REQUEST },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +28,7 @@ export async function GET(request: Request) {
|
|||||||
headers: {
|
headers: {
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const accessToken = response.data.access_token;
|
const accessToken = response.data.access_token;
|
||||||
@@ -38,12 +40,12 @@ export async function GET(request: Request) {
|
|||||||
secure: process.env.NODE_ENV === "production",
|
secure: process.env.NODE_ENV === "production",
|
||||||
});
|
});
|
||||||
|
|
||||||
return NextResponse.redirect(new URL("/overview", request.url));
|
return NextResponse.redirect(`${request.nextUrl.origin}/overview`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Error exchanging code for access token" },
|
{ error: "Error exchanging code for access token" },
|
||||||
{ status: StatusCodes.INTERNAL_SERVER_ERROR }
|
{ status: StatusCodes.INTERNAL_SERVER_ERROR },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,11 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { client } from "@/core/auth/oauth";
|
import { client } from "@/core/auth/oauth";
|
||||||
import { cookies, headers } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
export async function redirectAuth() {
|
export async function redirectAuth() {
|
||||||
// Get headers of the incoming request
|
|
||||||
const hdrs = await headers();
|
|
||||||
|
|
||||||
// Get protocol and host
|
|
||||||
const host = hdrs.get("host");
|
|
||||||
const proto = hdrs.get("x-forwarded-proto") || "http";
|
|
||||||
|
|
||||||
const baseUrl = `${proto}://${host}`;
|
|
||||||
|
|
||||||
const authorizationUri = client.authorizeURL({
|
const authorizationUri = client.authorizeURL({
|
||||||
redirect_uri: `${baseUrl}/api/auth/callback`,
|
|
||||||
scope: "user:email",
|
scope: "user:email",
|
||||||
state: "random_state_string",
|
state: "random_state_string",
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user