-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
322 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1 @@ | ||
import { PropsWithChildren, memo } from 'react'; | ||
|
||
import AppLayout from '@/layout/AppLayout'; | ||
|
||
const AppLayoutDesktop = memo<PropsWithChildren>(({ children }) => { | ||
return <AppLayout>{children}</AppLayout>; | ||
}); | ||
|
||
export default AppLayoutDesktop; | ||
export { default } from '@/layout/AppLayoutWithUser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use client'; | ||
|
||
import { useRouter } from 'next/navigation'; | ||
import queryString from 'query-string'; | ||
import React from 'react'; | ||
|
||
import oidc from '@/config/oidc.mjs'; | ||
|
||
const { client, server, AUTH_DATA } = oidc; | ||
const { url } = server; | ||
const { client_id, redirect_uri } = client; | ||
|
||
export default function Auth() { | ||
const router = useRouter(); | ||
React.useEffect(() => { | ||
const authData = localStorage.getItem(AUTH_DATA); | ||
if (authData) { | ||
// todo validate auth | ||
router.push('/chat'); | ||
return; | ||
} | ||
const query = queryString.stringify({ | ||
client_id, | ||
redirect_uri: `${window.location.origin}${redirect_uri}`, | ||
response_type: 'code', | ||
scope: 'openid profile email groups offline_access', | ||
}); | ||
window.location.href = `${url}/oidc/auth?${query}`; | ||
}, []); | ||
return <> </>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use client'; | ||
|
||
import { useRouter, useSearchParams } from 'next/navigation'; | ||
import React from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
import oidc from '@/config/oidc.mjs'; | ||
|
||
const { client, AUTH_DATA } = oidc; | ||
const { redirect_uri } = client; | ||
|
||
export default function Auth() { | ||
const searchParams = useSearchParams(); | ||
const dispatch = useDispatch(); | ||
const router = useRouter(); | ||
const code = searchParams.get('code'); | ||
const fetchAuth = async () => { | ||
fetch(`/oidc/token?code=${code}&redirect_uri=${location.origin}${redirect_uri}`, { | ||
method: 'POST', | ||
}) | ||
.then(res => res.json()) | ||
.then(res => { | ||
if (res.data?.errors) { | ||
console.warn(res.data?.errors); | ||
return; | ||
} | ||
if (res.data) { | ||
localStorage.setItem(AUTH_DATA, JSON.stringify(res.data)); | ||
dispatch({ | ||
type: 'SAVE_AUTH_DATA', | ||
authData: res.data, | ||
}); | ||
router.push('/chat'); | ||
} | ||
}); | ||
}; | ||
React.useEffect(() => { | ||
fetchAuth(); | ||
}, []); | ||
return <> </>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use client'; | ||
|
||
import { Flex, Spin } from 'antd'; | ||
import { usePathname } from 'next/navigation'; | ||
import React from 'react'; | ||
|
||
export default function Layout({ children }: { children: React.ReactNode }) { | ||
const pathname = usePathname(); | ||
const tip = React.useMemo(() => { | ||
switch (pathname) { | ||
case '/oidc/logout': { | ||
return '登出中...'; | ||
} | ||
default: { | ||
return '加载中...'; | ||
} | ||
} | ||
}, [pathname]); | ||
return ( | ||
<Flex | ||
align={'center'} | ||
justify={'center'} | ||
style={{ | ||
height: '100vh', | ||
}} | ||
> | ||
<Spin | ||
spinning | ||
style={{ | ||
minWidth: '60px', | ||
}} | ||
tip={<span>{tip}</span>} | ||
> | ||
{children} | ||
</Spin> | ||
</Flex> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
import oidc from '@/config/oidc.mjs'; | ||
|
||
const { client, AUTH_DATA, server } = oidc; | ||
const { redirect_uri } = client; | ||
const { url } = server; | ||
|
||
export default function Auth() { | ||
React.useEffect(() => { | ||
localStorage.removeItem(AUTH_DATA); | ||
window.location.href = `${url}/oidc/logout/remove-auth-data?redirect=${encodeURIComponent( | ||
`${url}/oidc/auth?redirect_uri=${location.origin}${redirect_uri}&response_type=code&scope=openid+profile+email+groups+offline_access` | ||
)}`; | ||
}, []); | ||
return <> </>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
export default function oidc() { | ||
return <>oidc</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { type NextRequest, NextResponse } from 'next/server'; | ||
|
||
import oidc from '@/config/oidc.mjs'; | ||
import { btoa } from '@/utils'; | ||
|
||
const { client, server } = oidc; | ||
const { url } = server; | ||
const { client_id, client_secret } = client; | ||
|
||
function fetchWithTimeout(url: string, options: RequestInit, timeout = 3000) { | ||
const fetchPromise = fetch(url, options); | ||
const timeoutPromise = new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
reject(new Error('Request timed out')); | ||
}, timeout); | ||
}); | ||
return Promise.race([fetchPromise, timeoutPromise]); | ||
} | ||
|
||
export async function POST(request: NextRequest) { | ||
const searchParams = request.nextUrl.searchParams; | ||
const code = searchParams.get('code'); | ||
const redirect_uri = searchParams.get('redirect_uri'); | ||
const body = { | ||
grant_type: 'authorization_code', | ||
code, | ||
redirect_uri, | ||
}; | ||
const res: any = await fetchWithTimeout( | ||
`${url}/oidc/token`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `Basic ${btoa(`${client_id}:${client_secret}`)}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(body), | ||
}, | ||
10_000 | ||
); | ||
const data = await res.json(); | ||
return NextResponse.json({ data }); | ||
} |
Oops, something went wrong.