浏览代码

update authoption

production_process
MSI\derek 2 个月前
父节点
当前提交
a9b818aa6f
共有 2 个文件被更改,包括 7 次插入6 次删除
  1. +4
    -4
      src/app/(main)/layout.tsx
  2. +3
    -2
      src/config/authConfig.ts

+ 4
- 4
src/app/(main)/layout.tsx 查看文件

@@ -1,6 +1,6 @@
import AppBar from "@/components/AppBar"; import AppBar from "@/components/AppBar";
import { getServerSession } from "next-auth";
import { authOptions } from "@/config/authConfig";
import { AuthOptions, getServerSession } from "next-auth";
import { authOptions, SessionWithTokens } from "@/config/authConfig";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import Box from "@mui/material/Box"; import Box from "@mui/material/Box";
import { NAVIGATION_CONTENT_WIDTH } from "@/config/uiConfig"; import { NAVIGATION_CONTENT_WIDTH } from "@/config/uiConfig";
@@ -16,14 +16,14 @@ export default async function MainLayout({
}: { }: {
children: React.ReactNode; children: React.ReactNode;
}) { }) {
const session = await getServerSession(authOptions);
const session = await getServerSession<AuthOptions, SessionWithTokens>(authOptions);


if (!session?.user) { if (!session?.user) {
redirect("/login"); redirect("/login");
} }


if (session) { if (session) {
SetupAxiosInterceptors(session?.accessToken);
SetupAxiosInterceptors(session.accessToken);
} }


return ( return (


+ 3
- 2
src/config/authConfig.ts 查看文件

@@ -3,11 +3,12 @@ import CredentialsProvider from "next-auth/providers/credentials";
import { LOGIN_API_PATH } from "./api"; import { LOGIN_API_PATH } from "./api";


export interface SessionWithTokens extends Session { export interface SessionWithTokens extends Session {
accessToken?: string;
accessToken: string | null;
refreshToken?: string; refreshToken?: string;
abilities: string[] abilities: string[]
} }



export const authOptions: AuthOptions = { export const authOptions: AuthOptions = {
debug: process.env.NODE_ENV === "development", debug: process.env.NODE_ENV === "development",
providers: [ providers: [
@@ -49,7 +50,7 @@ export const authOptions: AuthOptions = {
const sessionWithToken: SessionWithTokens = { const sessionWithToken: SessionWithTokens = {
...session, ...session,
// Add the data from the token to the session // Add the data from the token to the session
accessToken: token.accessToken as string | undefined,
accessToken: token.accessToken as string | null,
refreshToken: token.refreshToken as string | undefined, refreshToken: token.refreshToken as string | undefined,
abilities: token.abilities as string[] abilities: token.abilities as string[]
}; };


正在加载...
取消
保存