|
@@ -3,7 +3,7 @@ import {
|
|
|
GetResourceResponse,
|
|
|
} from "@server/routers/resource";
|
|
|
import ResourceAuthPortal from "./components/ResourceAuthPortal";
|
|
|
-import { internal } from "@app/api";
|
|
|
+import { internal, priv } from "@app/api";
|
|
|
import { AxiosResponse } from "axios";
|
|
|
import { authCookieHeader } from "@app/api/cookies";
|
|
|
import { cache } from "react";
|
|
@@ -11,10 +11,12 @@ import { verifySession } from "@app/lib/auth/verifySession";
|
|
|
import { redirect } from "next/navigation";
|
|
|
import ResourceNotFound from "./components/ResourceNotFound";
|
|
|
import ResourceAccessDenied from "./components/ResourceAccessDenied";
|
|
|
+import { cookies } from "next/headers";
|
|
|
+import { CheckResourceSessionResponse } from "@server/routers/auth";
|
|
|
|
|
|
export default async function ResourceAuthPage(props: {
|
|
|
params: Promise<{ resourceId: number }>;
|
|
|
- searchParams: Promise<{ redirect: string }>;
|
|
|
+ searchParams: Promise<{ redirect: string | undefined }>;
|
|
|
}) {
|
|
|
const params = await props.params;
|
|
|
const searchParams = await props.searchParams;
|
|
@@ -46,6 +48,32 @@ export default async function ResourceAuthPage(props: {
|
|
|
|
|
|
const redirectUrl = searchParams.redirect || authInfo.url;
|
|
|
|
|
|
+ const allCookies = await cookies();
|
|
|
+ const cookieName =
|
|
|
+ process.env.RESOURCE_SESSION_COOKIE_NAME + `_${params.resourceId}`;
|
|
|
+ const sessionId = allCookies.get(cookieName)?.value ?? null;
|
|
|
+
|
|
|
+ if (sessionId) {
|
|
|
+ let doRedirect = false;
|
|
|
+ try {
|
|
|
+ const res = await priv.get<
|
|
|
+ AxiosResponse<CheckResourceSessionResponse>
|
|
|
+ >(`/resource-session/${params.resourceId}/${sessionId}`);
|
|
|
+
|
|
|
+ console.log("resource session already exists and is valid");
|
|
|
+
|
|
|
+ if (res && res.data.data.valid) {
|
|
|
+ doRedirect = true;
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (doRedirect) {
|
|
|
+ redirect(redirectUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (!hasAuth) {
|
|
|
// no authentication so always go straight to the resource
|
|
|
redirect(redirectUrl);
|
|
@@ -94,9 +122,6 @@ export default async function ResourceAuthPage(props: {
|
|
|
id: authInfo.resourceId,
|
|
|
}}
|
|
|
redirect={redirectUrl}
|
|
|
- queryParamName={
|
|
|
- process.env.RESOURCE_SESSION_QUERY_PARAM_NAME!
|
|
|
- }
|
|
|
/>
|
|
|
</div>
|
|
|
</>
|