From 6a4449d93c5055eaff99c3dca1b7b59802866e16 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 1 Jan 2025 23:37:15 +0800 Subject: [PATCH] :sparkles: Auth check :bug: Fix auth related bugs --- src/components/CapAppBar.tsx | 19 +++++++++++++++---- src/pages/auth/login.tsx | 25 ++++++++++++++++++++++++- src/pages/users/me.tsx | 12 ++++++++++++ src/services/auth.ts | 10 ++++++++++ src/services/network.ts | 9 +++++++-- 5 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 src/pages/users/me.tsx diff --git a/src/components/CapAppBar.tsx b/src/components/CapAppBar.tsx index c5752cd..2477f4b 100644 --- a/src/components/CapAppBar.tsx +++ b/src/components/CapAppBar.tsx @@ -1,12 +1,17 @@ -import { AppBar, Box, IconButton, Toolbar, Typography } from '@mui/material' +import { useUserStore } from '@/services/user' +import { AppBar, Avatar, Box, IconButton, Toolbar, Typography, useTheme } from '@mui/material' import MenuIcon from '@mui/icons-material/Menu' import AccountCircle from '@mui/icons-material/AccountCircle' import Link from 'next/link' +import { getAttachmentUrl } from '@/services/network' export function CapAppBar() { + const userStore = useUserStore() + const theme = useTheme() + return ( - + @@ -17,7 +22,7 @@ export function CapAppBar() { - + - + {userStore.account ? ( + + ) : ( + + + + )} diff --git a/src/pages/auth/login.tsx b/src/pages/auth/login.tsx index f9d1863..21444be 100644 --- a/src/pages/auth/login.tsx +++ b/src/pages/auth/login.tsx @@ -2,6 +2,7 @@ import { SnLoginCheckpoint } from '@/components/auth/SnLoginCheckpoint' import { SnLoginRouter } from '@/components/auth/SnLoginRouter' import { SnLoginStart } from '@/components/auth/SnLoginStart' import { SnAuthFactor, SnAuthTicket } from '@/services/auth' +import { useUserStore } from '@/services/user' import { Box, Container, Typography } from '@mui/material' import { useRouter } from 'next/router' import { useState } from 'react' @@ -13,6 +14,27 @@ export default function Login() { const [factor, setFactor] = useState(null) const router = useRouter() + const userStore = useUserStore() + + function doCallback() { + if (router.query['redirect_url']) { + let redirectUrl: string + if (Array.isArray(router.query['redirect_url'])) { + redirectUrl = router.query['redirect_url'][0] + } else { + redirectUrl = router.query['redirect_url'].toString() + } + + if (redirectUrl.startsWith('/')) { + router.push(redirectUrl) + } else { + window.open(redirectUrl, '_self') + } + return + } + + router.push('/users/me') + } function renderForm() { switch (period) { @@ -38,7 +60,8 @@ export default function Login() { setPeriod(1) return } - router.push('/') + userStore.fetchUser() + doCallback() }} /> ) diff --git a/src/pages/users/me.tsx b/src/pages/users/me.tsx new file mode 100644 index 0000000..c4f59dc --- /dev/null +++ b/src/pages/users/me.tsx @@ -0,0 +1,12 @@ +import { checkAuthenticatedClient, redirectToLogin } from '@/services/auth' +import { Container } from '@mui/material' +import { useRouter } from 'next/router' +import { useEffect } from 'react' + +export default function UserItself() { + useEffect(() => { + if (!checkAuthenticatedClient()) redirectToLogin() + }, []) + + return +} diff --git a/src/services/auth.ts b/src/services/auth.ts index 1d3e6ea..414b3d3 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -1,3 +1,5 @@ +import { hasCookie } from 'cookies-next/client' + export interface SnAuthResult { isFinished: boolean ticket: SnAuthTicket @@ -32,3 +34,11 @@ export interface SnAuthFactor { config?: Record | null accountId?: number | null } + +export function checkAuthenticatedClient(): boolean { + return !!hasCookie('nex_user_atk') +} + +export function redirectToLogin() { + window.open('/auth/login?redirect_uri=' + encodeURIComponent(window.location.pathname), '_self') +} diff --git a/src/services/network.ts b/src/services/network.ts index 2af1bd7..098c7d5 100644 --- a/src/services/network.ts +++ b/src/services/network.ts @@ -39,8 +39,8 @@ async function refreshToken(): Promise { }, { baseURL }, ) - const atk: string = resp.data['accessToken'] - const rtk: string = resp.data['refreshToken'] + const atk: string = resp.data['access_token'] + const rtk: string = resp.data['refresh_token'] setCookie('nex_user_atk', atk, { path: '/', maxAge: 2592000 }) setCookie('nex_user_rtk', rtk, { path: '/', maxAge: 2592000 }) @@ -70,3 +70,8 @@ function isTokenExpired(token: string): boolean { return true } } + +export function getAttachmentUrl(identifer: string): string { + if (identifer.startsWith('http')) return identifer + return `${baseURL}/cgi/uc/attachments/${identifer}` +}