This commit is contained in:
JIANG
2026-02-11 18:58:10 +08:00
parent 9d06226cb4
commit 66f2390078
13 changed files with 307 additions and 62 deletions

View File

@@ -8,13 +8,14 @@ import {
} from "@refinedev/mui";
import { SessionProvider, signIn, signOut, useSession } from "next-auth/react";
import { usePathname } from "next/navigation";
import React from "react";
import React, { useEffect } from "react";
import routerProvider from "@refinedev/nextjs-router";
import { ColorModeContextProvider } from "@contexts/color-mode";
import { dataProvider } from "@providers/data-provider";
import { ProjectProvider } from "@/contexts/ProjectContext";
import { useAuthStore } from "@/store/authStore";
import { LiaNetworkWiredSolid } from "react-icons/lia";
import { TbDatabaseEdit } from "react-icons/tb";
@@ -47,6 +48,11 @@ type AppProps = {
const App = (props: React.PropsWithChildren<AppProps>) => {
const { data, status } = useSession();
const to = usePathname();
const setAccessToken = useAuthStore((state) => state.setAccessToken);
useEffect(() => {
setAccessToken(typeof data?.accessToken === "string" ? data.accessToken : null);
}, [data?.accessToken, setAccessToken]);
if (status === "loading") {
return <span>loading...</span>;
@@ -103,6 +109,7 @@ const App = (props: React.PropsWithChildren<AppProps>) => {
if (data?.user) {
const { user } = data;
return {
id: user.id,
name: user.name,
avatar: user.image,
};

View File

@@ -1,7 +1,8 @@
import { NextAuthOptions } from "next-auth";
import KeycloakProvider from "next-auth/providers/keycloak";
import Avatar from "@assets/avatar/avatar-small.jpeg";
const authOptions = {
const authOptions: NextAuthOptions = {
// Configure one or more authentication providers
providers: [
KeycloakProvider({
@@ -19,6 +20,26 @@ const authOptions = {
}),
],
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
jwt: async ({ token, profile, account }) => {
if (profile?.sub) {
token.sub = profile.sub;
}
if (account?.access_token) {
token.accessToken = account.access_token;
}
return token;
},
session: async ({ session, token }) => {
if (session.user && token.sub) {
session.user.id = token.sub;
}
if (token.accessToken) {
session.accessToken = token.accessToken;
}
return session;
},
},
};
export default authOptions;