Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: noauth bugs #82

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/app/[locale]/agent/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { Button, Col, Row, Spin, Tooltip } from 'antd';
import classNames from 'classnames';
import { useTranslations } from 'next-intl';
import Image from 'next/image';
import Link from 'next/link';
import { useRouter } from 'next/navigation';
import { useQueryState } from 'nuqs';
import React, { useState } from 'react';

import TitleCom from '@/components/Title';
import { useAuthContext } from '@/layout/AuthLayout';
import { setLoginRedirect } from '@/utils/client';

import TagContent from './TagContent';
import { useStyles } from './styles';
Expand Down Expand Up @@ -98,11 +100,24 @@ const Agent = React.memo<AgentProps>(({ agentData, cateData }) => {
<Row gutter={[16, 16]}>
{(ListData?.GPT?.listGPT?.nodes || []).map((item, index) => (
<Col {...layout} key={index}>
<a
<Link
className={styles.card}
href={`/chat/new?appNamespace=${item.name?.split(
'/'
)?.[0]}&appName=${item.name?.split('/')?.[1]}`}
href={
authed
? `/chat/new?appNamespace=${item.name?.split(
'/'
)?.[0]}&appName=${item.name?.split('/')?.[1]}`
: '/oidc/auth'
}
onClick={() => {
if (!authed) {
setLoginRedirect(
`/chat/new?appNamespace=${item.name?.split(
'/'
)?.[0]}&appName=${item.name?.split('/')?.[1]}`
);
}
}}
>
<div className={styles.left}>
<Image alt={item.displayName} height={72} src={item.icon} width={72} />
Expand All @@ -119,7 +134,7 @@ const Agent = React.memo<AgentProps>(({ agentData, cateData }) => {
<span className={styles.creator}>@{item.creator}</span>
</div>
</div>
</a>
</Link>
</Col>
))}
</Row>
Expand Down
10 changes: 10 additions & 0 deletions src/app/[locale]/chat/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
'use client';

import { redirect } from 'next/navigation';
import { ReactNode } from 'react';

import { useAuthContext } from '@/layout/AuthLayout';

type Props = {
children: ReactNode;
};

// Since we have a `not-found.tsx` page on the root, a layout file
// is required, even if it's just passing children through.
export default function RootLayout({ children }: Props) {
const { authed } = useAuthContext();
if (authed === false) {
// ๆ— ๆ•ˆ่ฎค่ฏ
redirect('/agent');
}
return children;
}
7 changes: 6 additions & 1 deletion src/app/[locale]/oidc/callback/Callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import { useRouter } from 'next/navigation';
import React from 'react';
import { useDispatch } from 'react-redux';

import { delLoginRedirect, getLoginRedirect } from '@/utils/client';
import { AUTH_DATA } from '@/utils/constants';

export default function Callback({ data: res }: { data: any }) {
const dispatch = useDispatch();
const t = useTranslations('callback');
const router = useRouter();
const saveAuth = async () => {
const redirectUrl: string = getLoginRedirect(document.cookie);
if (redirectUrl) {
delLoginRedirect();
}
if (res?.data?.errors || !res?.data) {
console.warn(res?.data?.errors);
notification.warning({
Expand All @@ -34,7 +39,7 @@ export default function Callback({ data: res }: { data: any }) {
type: 'SAVE_AUTH_DATA',
authData: res.data,
});
router.push('/');
router.push(redirectUrl || '/');
}
};
React.useEffect(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/app/[locale]/oidc/logout/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import { useRouter } from 'next/navigation';
import React from 'react';

import { setAxiosHooksWithoutAuth } from '@/utils/axios';
import { AUTH_DATA } from '@/utils/constants';

export default function Logout() {
const router = useRouter();
React.useEffect(() => {
setAxiosHooksWithoutAuth();
window.localStorage.removeItem(AUTH_DATA);
// router.push('/oidc/remove-auth-and-login'); // ๆš‚ๆ—ถๅฑ่”ฝ็™ปๅ‡บ, ๅ‚่€ƒ, ่ทณ่ฝฌๅˆฐๆœช็™ปๅฝ•็š„็Šถๆ€
router.push('/');
Expand Down
2 changes: 1 addition & 1 deletion src/layout/AppNoAuthLayout/SideBar/SideBarHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const SidebarHeader = () => {
return (
<Flex className={styles.sidebarHeader} vertical>
<div className={styles.logo}>
<Link href="/chat">
<Link href="/agent">
<Logo height={32} />
</Link>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/layout/GlobalLayout/ThemeLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useSelector } from 'react-redux';
import { useAuthContext } from '@/layout/AuthLayout';
import { GlobalStyle } from '@/styles';
import { dark, default_theme, light } from '@/theme/themeConfig';
import { initAxiosHooks } from '@/utils/axios';
import { setAxiosHooksWithAuth } from '@/utils/axios';
import { isTokenExpired, setCookie } from '@/utils/client';
import { AUTH_DATA } from '@/utils/constants';

Expand Down Expand Up @@ -65,7 +65,7 @@ const ThemeLayout = React.memo<Props>(
}
setAuthed(true);
if (!isAxiosConfigured) {
setAxiosConfigured(initAxiosHooks());
setAxiosConfigured(setAxiosHooksWithAuth());
}
}, [pathname]);

Expand Down
6 changes: 5 additions & 1 deletion src/utils/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ export const createCustomAxios = (configs?: CreateAxiosDefaults): AxiosInstance
return Axios.create(_configs);
};

export const initAxiosHooks = () => {
export const setAxiosHooksWithAuth = () => {
const axios = createCustomAxios();
configure({ axios });
return true; // ๅˆๅง‹ๅŒ–ๆˆๅŠŸ ่ฟ”ๅ›žๆ ‡่ฎฐ
};

export const setAxiosHooksWithoutAuth = () => {
configure({ axios: Axios.create({}) });
};

export const useAxiosRequest = (
config: AxiosRequestConfig,
options?: Options,
Expand Down
24 changes: 24 additions & 0 deletions src/utils/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use client';

import { LOGIN_REDIRECT } from './constants';

export { sdk as bff } from '@yuntijs/arcadia-bff-sdk';
export { sdk as bffClient } from '@yuntijs/bff-client';

Expand Down Expand Up @@ -91,3 +93,25 @@ export function isTokenExpired(id_token?: string): boolean {
const expiredTimestampInMs = parseToken(id_token_split_arr).exp * 1000;
return Date.now() >= expiredTimestampInMs;
}

/**
* ่ฎพ็ฝฎ็™ปๅฝ• redirect
* @param {string} redirectUrl: e.g. '/chat'.
*/
export function setLoginRedirect(redirectUrl: string) {
setCookie(LOGIN_REDIRECT, redirectUrl);
}

/**
* ่Žทๅ–็™ปๅฝ• redirect
*/
export function getLoginRedirect(cookieString: string) {
return getCookie(cookieString, LOGIN_REDIRECT);
}

/**
* ็งป้™ค็™ปๅฝ• redirect
*/
export function delLoginRedirect() {
setCookie(LOGIN_REDIRECT, '', -1);
}
1 change: 1 addition & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const AUTH_DATA = 'authData';
export const DEFAULT_CHAT = 'default_chat';
export const LOCALE = 'NEXT_LOCALE';
export const LOGIN_REDIRECT = 'LOGIN_REDIRECT';
export const AGENT_CATEGORY_INDEXES = [
'components.index.tuiJian',
'utils.constants.youXiDongMan',
Expand Down
Loading