Skip to content

Commit

Permalink
♻️ refactor: refactor the auth (lobehub#2043)
Browse files Browse the repository at this point in the history
* ♻️ refactor: refactor the auth code

* ♻️ refactor: refactor the auth code

* ♻️ refactor: refactor the auth code

* ♻️ refactor: refactor the auth code

* 🎨 refactor: improve code
  • Loading branch information
arvinxx authored Apr 14, 2024
1 parent 0d37493 commit 37ecb41
Show file tree
Hide file tree
Showing 27 changed files with 133 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { GET, POST } from '../next-auth';
export { GET, POST } from '@/libs/next-auth';

export const runtime = 'edge'; // optional
8 changes: 7 additions & 1 deletion src/app/chat/(desktop)/features/ChatInput/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ const useStyles = createStyles(({ css, prefixCls, token }) => {

const isMac = isMacOS();

const Footer = memo<{ setExpand?: (expand: boolean) => void }>(({ setExpand }) => {
interface FooterProps {
setExpand?: (expand: boolean) => void;
}

const Footer = memo<FooterProps>(({ setExpand }) => {
const { t } = useTranslation('chat');

const { theme, styles } = useStyles();
Expand Down Expand Up @@ -193,4 +197,6 @@ const Footer = memo<{ setExpand?: (expand: boolean) => void }>(({ setExpand }) =
);
});

Footer.displayName = 'Footer';

export default Footer;
8 changes: 7 additions & 1 deletion src/app/chat/(desktop)/features/ChatInput/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ const useStyles = createStyles(({ css }) => {
};
});

const InputArea = memo<{ setExpand?: (expand: boolean) => void }>(({ setExpand }) => {
interface InputAreaProps {
setExpand?: (expand: boolean) => void;
}

const InputArea = memo<InputAreaProps>(({ setExpand }) => {
const { t } = useTranslation('chat');
const { styles } = useStyles();
const ref = useRef<TextAreaRef>(null);
Expand Down Expand Up @@ -119,4 +123,6 @@ const InputArea = memo<{ setExpand?: (expand: boolean) => void }>(({ setExpand }
);
});

InputArea.displayName = 'InputArea';

export default InputArea;
4 changes: 2 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { isRtlLang } from 'rtl-detect';
import Analytics from '@/components/Analytics';
import { DEFAULT_LANG, LOBE_LOCALE_COOKIE } from '@/const/locale';
import AuthProvider from '@/layout/AuthProvider';
import GlobalLayout from '@/layout/GlobalLayout';
import GlobalProvider from '@/layout/GlobalProvider';
import LayoutRoutes from '@/layout/routes';
import { isMobileDevice } from '@/utils/responsive';

const RootLayout = async ({ children }: PropsWithChildren) => {
Expand All @@ -22,7 +22,7 @@ const RootLayout = async ({ children }: PropsWithChildren) => {
<body>
<GlobalProvider>
<AuthProvider>
<GlobalLayout>{children}</GlobalLayout>
<LayoutRoutes>{children}</LayoutRoutes>
</AuthProvider>
</GlobalProvider>
<Analytics />
Expand Down
18 changes: 0 additions & 18 deletions src/config/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@ export const getAppConfig = () => {

PLUGIN_SETTINGS: process.env.PLUGIN_SETTINGS,

ENABLE_OAUTH_SSO: !!process.env.ENABLE_OAUTH_SSO,
SSO_PROVIDERS: process.env.SSO_PROVIDERS || 'auth0',
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID || '',
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET || '',
AUTH0_ISSUER: process.env.AUTH0_ISSUER || '',
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID || '',
GITHUB_CLIENT_SECRET: process.env.GITHUB_CLIENT_SECRET || '',
AZURE_AD_CLIENT_ID: process.env.AZURE_AD_CLIENT_ID || '',
AZURE_AD_CLIENT_SECRET: process.env.AZURE_AD_CLIENT_SECRET || '',
AZURE_AD_TENANT_ID: process.env.AZURE_AD_TENANT_ID || '',
AUTHENTIK_CLIENT_ID: process.env.AUTHENTIK_CLIENT_ID || '',
AUTHENTIK_CLIENT_SECRET: process.env.AUTHENTIK_CLIENT_SECRET || '',
AUTHENTIK_ISSUER: process.env.AUTHENTIK_ISSUER || '',
ZITADEL_CLIENT_ID: process.env.ZITADEL_CLIENT_ID || '',
ZITADEL_CLIENT_SECRET: process.env.ZITADEL_CLIENT_SECRET || '',
ZITADEL_ISSUER: process.env.ZITADEL_ISSUER || '',
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET || '',

ENABLE_LANGFUSE: process.env.ENABLE_LANGFUSE === '1',
LANGFUSE_SECRET_KEY: process.env.LANGFUSE_SECRET_KEY || '',
LANGFUSE_PUBLIC_KEY: process.env.LANGFUSE_PUBLIC_KEY || '',
Expand Down
71 changes: 71 additions & 0 deletions src/config/server/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* eslint-disable sort-keys-fix/sort-keys-fix , typescript-sort-keys/interface */

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface ProcessEnv {
ENABLE_OAUTH_SSO?: string;
SSO_PROVIDERS?: string;

AUTH0_CLIENT_ID?: string;
AUTH0_CLIENT_SECRET?: string;
AUTH0_ISSUER?: string;

// Github
GITHUB_CLIENT_ID?: string;
GITHUB_CLIENT_SECRET?: string;

// Azure AD
AZURE_AD_CLIENT_ID?: string;
AZURE_AD_CLIENT_SECRET?: string;
AZURE_AD_TENANT_ID?: string;

// AUTHENTIK
AUTHENTIK_CLIENT_ID?: string;
AUTHENTIK_CLIENT_SECRET?: string;
AUTHENTIK_ISSUER?: string;

// ZITADEL
ZITADEL_CLIENT_ID?: string;
ZITADEL_CLIENT_SECRET?: string;
ZITADEL_ISSUER?: string;
NEXTAUTH_SECRET?: string;
}
}
}

export const getAuthConfig = () => {
if (typeof process === 'undefined') {
throw new Error('[Server Config] you are importing a server-only module outside of server');
}

return {
ENABLE_OAUTH_SSO: !!process.env.ENABLE_OAUTH_SSO,
SSO_PROVIDERS: process.env.SSO_PROVIDERS || 'auth0',

// Auth0
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID || '',
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET || '',
AUTH0_ISSUER: process.env.AUTH0_ISSUER || '',

// Github
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID || '',
GITHUB_CLIENT_SECRET: process.env.GITHUB_CLIENT_SECRET || '',

// Azure AD
AZURE_AD_CLIENT_ID: process.env.AZURE_AD_CLIENT_ID || '',
AZURE_AD_CLIENT_SECRET: process.env.AZURE_AD_CLIENT_SECRET || '',
AZURE_AD_TENANT_ID: process.env.AZURE_AD_TENANT_ID || '',

// AUTHENTIK
AUTHENTIK_CLIENT_ID: process.env.AUTHENTIK_CLIENT_ID || '',
AUTHENTIK_CLIENT_SECRET: process.env.AUTHENTIK_CLIENT_SECRET || '',
AUTHENTIK_ISSUER: process.env.AUTHENTIK_ISSUER || '',

// ZITADEL
ZITADEL_CLIENT_ID: process.env.ZITADEL_CLIENT_ID || '',
ZITADEL_CLIENT_SECRET: process.env.ZITADEL_CLIENT_SECRET || '',
ZITADEL_ISSUER: process.env.ZITADEL_ISSUER || '',
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET || '',
};
};
4 changes: 3 additions & 1 deletion src/config/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getAnalyticsConfig } from './analytics';
import { getAppConfig } from './app';
import { getAuthConfig } from './auth';
import { getProviderConfig } from './provider';

export const getServerConfig = () => {
Expand All @@ -9,7 +10,8 @@ export const getServerConfig = () => {

const provider = getProviderConfig();
const app = getAppConfig();
const auth = getAuthConfig();
const analytics = getAnalyticsConfig();

return { ...provider, ...app, ...analytics };
return { ...provider, ...app, ...analytics, ...auth };
};
10 changes: 10 additions & 0 deletions src/layout/AuthProvider/NextAuth/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SessionProvider } from 'next-auth/react';
import { PropsWithChildren } from 'react';

import { API_ENDPOINTS } from '@/services/_url';

const NextAuth = ({ children }: PropsWithChildren) => {
return <SessionProvider basePath={API_ENDPOINTS.oauth}>{children}</SessionProvider>;
};

export default NextAuth;
10 changes: 3 additions & 7 deletions src/layout/AuthProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { SessionProvider } from 'next-auth/react';
import { PropsWithChildren } from 'react';

import { getServerConfig } from '@/config/server';
import { API_ENDPOINTS } from '@/services/_url';

import NextAuth from './NextAuth';

const { ENABLE_OAUTH_SSO = false } = getServerConfig();

const AuthProvider = ({ children }: PropsWithChildren) =>
ENABLE_OAUTH_SSO ? (
<SessionProvider basePath={API_ENDPOINTS.oauth}>{children}</SessionProvider>
) : (
children
);
ENABLE_OAUTH_SSO ? <NextAuth>{children}</NextAuth> : children;

export default AuthProvider;
11 changes: 11 additions & 0 deletions src/layout/DefaultLayout/Desktop/SideBar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { memo } from 'react';

import AvatarWithUpload from '@/features/AvatarWithUpload';

const Avatar = memo(() => {
return <AvatarWithUpload id={'avatar'} />;
});

Avatar.displayName = 'Avatar';

export default Avatar;
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SideNav } from '@lobehub/ui';
import { memo } from 'react';

import AvatarWithUpload from '@/features/AvatarWithUpload';
import { SidebarTabKey } from '@/store/global/initialState';

import Avatar from './Avatar';
import BottomActions from './BottomActions';
import TopActions from './TopActions';

Expand All @@ -14,7 +14,7 @@ interface Props {
export default memo<Props>(({ sidebarKey }) => {
return (
<SideNav
avatar={<AvatarWithUpload id={'avatar'} />}
avatar={<Avatar />}
bottomActions={<BottomActions tab={sidebarKey} />}
style={{ height: '100%' }}
topActions={<TopActions tab={sidebarKey} />}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import { type MobileNavBarTitleProps } from '@lobehub/ui';
import { createStyles } from 'antd-style';
import dynamic from 'next/dynamic';
Expand Down
2 changes: 2 additions & 0 deletions src/layout/DefaultLayout/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as DefaultLayoutDesktop } from './Desktop';
export { default as DefaultLayoutMobile } from './Mobile';
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import { usePathname } from 'next/navigation';
import { PropsWithChildren, memo } from 'react';

import Client from './Client';
import { DefaultLayoutDesktop } from '@/layout/DefaultLayout';

const defaultLayoutRoutes = new Set(['/']);

const DesktopLayout = memo<PropsWithChildren>(({ children }) => {
const pathname = usePathname();

if (pathname === '/') return children;
if (defaultLayoutRoutes.has(pathname)) return children;

return <Client>{children}</Client>;
return <DefaultLayoutDesktop>{children}</DefaultLayoutDesktop>;
});

export default DesktopLayout;
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { PropsWithChildren, memo } from 'react';

import { useActiveTabKey } from '@/hooks/useActiveTabKey';
import { useIsSubSlug } from '@/hooks/useIsSubSlug';

import Layout from './Client';
import { DefaultLayoutMobile } from '@/layout/DefaultLayout';

const MobileLayout = memo<PropsWithChildren>(({ children }) => {
const pathname = usePathname();
Expand All @@ -16,9 +15,9 @@ const MobileLayout = memo<PropsWithChildren>(({ children }) => {
if (pathname === '/') return children;

return (
<Layout showTabBar={!isSubPath} tabBarKey={tabBarKey}>
<DefaultLayoutMobile showTabBar={!isSubPath} tabBarKey={tabBarKey}>
{children}
</Layout>
</DefaultLayoutMobile>
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import ServerLayout from '@/components/server/ServerLayout';
import Desktop from './Desktop';
import Mobile from './Mobile';

const GlobalLayout = ServerLayout({ Desktop, Mobile });
const LayoutRoutes = ServerLayout({ Desktop, Mobile });

export default GlobalLayout;
export default LayoutRoutes;
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const nextAuth = NextAuth({
async session({ session, token }) {
// Pick userid from token
if (session.user) {
session.user.id = token.userId ?? session.user.id;
session.user.id = (token.userId ?? session.user.id) as string;
}
return session;
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextResponse } from 'next/server';

import { getServerConfig } from '@/config/server';
import { auth } from '@/libs/next-auth';

import { auth } from './app/api/auth/next-auth';
import { OAUTH_AUTHORIZED } from './const/auth';

export const config = {
Expand Down

0 comments on commit 37ecb41

Please sign in to comment.