|
|
@@ -4,14 +4,14 @@ import { NextRequest, NextResponse } from "next/server";
|
|
|
import {
|
|
|
BagistoCreateUserOperation,
|
|
|
BagistoProductInfo,
|
|
|
- BagistoSession,
|
|
|
BagistoUser,
|
|
|
ImageInfo,
|
|
|
} from "@/types/types";
|
|
|
import {
|
|
|
HIDDEN_PRODUCT_TAG,
|
|
|
} from "../constants";
|
|
|
-import { getServerSession } from "next-auth";
|
|
|
+import { auth } from "@/utils/auth/auth-helper";
|
|
|
+// import { getToken,decode } from "next-auth/jwt";
|
|
|
import {
|
|
|
CUSTOMER_REGISTRATION,
|
|
|
FORGET_PASSWORD,
|
|
|
@@ -89,9 +89,24 @@ async function getBaseHeader() {
|
|
|
export async function getAuthorizationToken(): Promise<{token: string | null; isGuest: boolean;}> {
|
|
|
|
|
|
// 登录用户从nextAuth里获取
|
|
|
- const authSession = (await getServerSession(
|
|
|
- authOptions,
|
|
|
- )) as BagistoSession | null;
|
|
|
+ /*
|
|
|
+ const tokenCookie =
|
|
|
+ cookieStore.get("next-auth.session-token")
|
|
|
+ ?? cookieStore.get("__Secure-next-auth.session-token");
|
|
|
+
|
|
|
+ if (tokenCookie?.value) {
|
|
|
+ const token = await decode({
|
|
|
+ token: tokenCookie.value,
|
|
|
+ secret: process.env.NEXTAUTH_SECRET!,
|
|
|
+ });
|
|
|
+ console.log('getAuthorizationToken decode ===============',token);
|
|
|
+ return {
|
|
|
+ token: token?.accessToken ?? '',
|
|
|
+ isGuest: false,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ */
|
|
|
+ const authSession = await auth();
|
|
|
const accessToken = authSession?.user?.accessToken;
|
|
|
if (accessToken) {
|
|
|
return {
|
|
|
@@ -118,7 +133,9 @@ export async function restApiFetch<T>({
|
|
|
headers,
|
|
|
tags,
|
|
|
variables,
|
|
|
+ isRoute = true,
|
|
|
revalidate = 60,
|
|
|
+ takeAuthorization = true, // 是否携带token,默认携带
|
|
|
}: {
|
|
|
api: string;
|
|
|
method: "POST" | "GET" | "PUT" | "DELETE";
|
|
|
@@ -126,37 +143,25 @@ export async function restApiFetch<T>({
|
|
|
headers?: HeadersInit | Record<string, string>;
|
|
|
tags?: string[];
|
|
|
variables?: ExtractVariables<T>;
|
|
|
- isCookies?: boolean;
|
|
|
+ isRoute?: boolean; // 是否是在route handle中调用
|
|
|
guestToken?: string;
|
|
|
revalidate?: number;
|
|
|
+ takeAuthorization?: boolean;
|
|
|
}): Promise<{ status: number; body: ExtractRestFulData<T> } | never> {
|
|
|
try {
|
|
|
const apiUrl = api.startsWith("http") ? api : `${REST_API_URL}${api}`;
|
|
|
const url = new URL(apiUrl);
|
|
|
|
|
|
- const tokenRes = await getAuthorizationToken();
|
|
|
+
|
|
|
|
|
|
const headerRes = await getBaseHeader();
|
|
|
const baseHeaders: Record<string, string> = {...headerRes};
|
|
|
- /*
|
|
|
- let accessToken: string | undefined = undefined;
|
|
|
-
|
|
|
- if (isCookies) {
|
|
|
- const sessions = (await getServerSession(
|
|
|
- authOptions,
|
|
|
- )) as BagistoSession | null;
|
|
|
- accessToken = sessions?.user?.accessToken;
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
- if (accessToken) {
|
|
|
- baseHeaders.Authorization = `Bearer ${accessToken}`;
|
|
|
- } else if (guestToken) {
|
|
|
- baseHeaders.Authorization = `Bearer ${guestToken}`;
|
|
|
- }
|
|
|
- */
|
|
|
- if (tokenRes.token) {
|
|
|
- baseHeaders.Authorization = `Bearer ${tokenRes.token}`;
|
|
|
+ if(takeAuthorization) {
|
|
|
+ const tokenRes = await getAuthorizationToken();
|
|
|
+ if (tokenRes.token) {
|
|
|
+ baseHeaders.Authorization = `Bearer ${tokenRes.token}`;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -193,6 +198,14 @@ export async function restApiFetch<T>({
|
|
|
|
|
|
const body = await result.json();
|
|
|
console.log('restApiFetch --- body:', body)
|
|
|
+ if(!isRoute) {
|
|
|
+ if(result.status === 401) {
|
|
|
+ const err = new Error('Authorization Bearer token is required. Please Login.');//new Error(body.message || 'Authorization Bearer token is required');
|
|
|
+ err.name = "UnauthorizedError";
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
return { status: result.status, body };
|
|
|
} catch (e) {
|
|
|
throw e;
|
|
|
@@ -257,11 +270,17 @@ export async function serverGraphqlFetch<
|
|
|
});
|
|
|
|
|
|
const body = await result.json();
|
|
|
- console.log('serverGraphqlFetch --- body:',body);
|
|
|
+ const err = body.errors?.[0] ?? null;
|
|
|
+ if(err && err.extensions.status === 401) {
|
|
|
+ // "UNAUTHENTICATED"
|
|
|
+ const err = new Error('Authorization Bearer token is required. Please Login.');
|
|
|
+ err.name = "UnauthorizedError";
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
return {
|
|
|
status:result.status,
|
|
|
data:body.data ?? null,
|
|
|
- error:body.errors?.[0] ?? null
|
|
|
+ error: err
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
@@ -299,23 +318,7 @@ export async function bagistoFetch<T>({
|
|
|
|
|
|
const headerRes = await getBaseHeader();
|
|
|
const baseHeaders: Record<string, string> = {...headerRes};
|
|
|
- /*
|
|
|
- let accessToken: string | undefined = undefined;
|
|
|
|
|
|
- if (isCookies) {
|
|
|
-
|
|
|
- const sessions = (await getServerSession(
|
|
|
- authOptions,
|
|
|
- )) as BagistoSession | null;
|
|
|
- accessToken = sessions?.user?.accessToken;
|
|
|
- }
|
|
|
-
|
|
|
- if (accessToken) {
|
|
|
- baseHeaders.Authorization = `Bearer ${accessToken}`;
|
|
|
- } else if (guestToken) {
|
|
|
- baseHeaders.Authorization = `Bearer ${guestToken}`;
|
|
|
- }
|
|
|
- */
|
|
|
if(takeAuthorization) {
|
|
|
const tokenRes = await getAuthorizationToken();
|
|
|
if (tokenRes.token) {
|