refactor(auth): migrate user center logout and refactor callback logic
- Migrate from static `Modal.confirm` to `App.useModal` in UserCenter for better context support. - Refactor Logto Callback page: - Remove unused `useEffect` dependency on `isAuthenticated`. - Inline redirect logic directly into the `onSuccess` callback of `useHandleSignInCallback`. - Preserve existing redirect flow logic while simplifying the component structure.
This commit is contained in:
parent
2032cec715
commit
73354d0394
@ -1,28 +1,20 @@
|
|||||||
import { useHandleSignInCallback } from '@logto/react';
|
import { useHandleSignInCallback } from '@logto/react';
|
||||||
import { Spin } from 'antd';
|
import { Spin } from 'antd';
|
||||||
import { useEffect } from 'react';
|
import { useNavigate } from 'react-router';
|
||||||
import { useLocation, useNavigate } from 'react-router';
|
|
||||||
|
|
||||||
export const CallbackPage = () => {
|
export const CallbackPage = () => {
|
||||||
|
const navigate = useNavigate();
|
||||||
const { isLoading, isAuthenticated, error } = useHandleSignInCallback(() => {
|
const { isLoading, isAuthenticated, error } = useHandleSignInCallback(() => {
|
||||||
// Navigate to root path when finished
|
// Navigate to root path when finished
|
||||||
});
|
|
||||||
|
|
||||||
const navigate = useNavigate();
|
|
||||||
const location = useLocation();
|
|
||||||
useEffect(() => {
|
|
||||||
if (isAuthenticated) {
|
|
||||||
console.debug({ isLoading, isAuthenticated, error });
|
|
||||||
const redirect = sessionStorage.getItem('redirect');
|
const redirect = sessionStorage.getItem('redirect');
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
navigate(redirect);
|
|
||||||
sessionStorage.removeItem('redirect');
|
sessionStorage.removeItem('redirect');
|
||||||
|
navigate(redirect);
|
||||||
} else {
|
} else {
|
||||||
navigate('/user-center');
|
navigate('/user-center');
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}, [isAuthenticated]);
|
|
||||||
// When it's working in progress
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
<Spin spinning>
|
<Spin spinning>
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { EditOutlined, FileTextOutlined, HomeOutlined, KeyOutlined, LockOutlined, LoginOutlined, LogoutOutlined, MailOutlined, MobileOutlined } from "@ant-design/icons";
|
import { EditOutlined, FileTextOutlined, HomeOutlined, KeyOutlined, LockOutlined, LoginOutlined, LogoutOutlined, MailOutlined, MobileOutlined } from "@ant-design/icons";
|
||||||
import { useLogto, type IdTokenClaims } from "@logto/react";
|
import { useLogto, type IdTokenClaims } from "@logto/react";
|
||||||
import { Avatar, Button, Divider, Flex, Modal, Typography } from "antd";
|
import { App, Avatar, Button, Divider, Flex, Modal, Typography } from "antd";
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
import { useLocation, useNavigate } from "react-router";
|
import { useLocation, useNavigate } from "react-router";
|
||||||
|
|
||||||
@ -39,6 +39,7 @@ export const UserCenter = () => {
|
|||||||
const handleModifyInfo = useCallback((url: string) => {
|
const handleModifyInfo = useCallback((url: string) => {
|
||||||
window.location.href = `${logto}${url}?redirect=${redirect}`;
|
window.location.href = `${logto}${url}?redirect=${redirect}`;
|
||||||
}, []);
|
}, []);
|
||||||
|
const [modal, contextHolder] = Modal.useModal();
|
||||||
if (!isAuthenticated) {
|
if (!isAuthenticated) {
|
||||||
return (
|
return (
|
||||||
<div className="app">
|
<div className="app">
|
||||||
@ -54,8 +55,9 @@ export const UserCenter = () => {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
console.debug(user);
|
|
||||||
return (
|
return (
|
||||||
|
<>
|
||||||
|
{contextHolder}
|
||||||
<Flex className="app" gap={12} vertical align="center" style={{ maxWidth: 600 }}>
|
<Flex className="app" gap={12} vertical align="center" style={{ maxWidth: 600 }}>
|
||||||
<Avatar size={128} src={user?.picture ?? user?.name} />
|
<Avatar size={128} src={user?.picture ?? user?.name} />
|
||||||
<Flex>
|
<Flex>
|
||||||
@ -75,11 +77,11 @@ export const UserCenter = () => {
|
|||||||
block
|
block
|
||||||
danger
|
danger
|
||||||
icon={<LoginOutlined />}
|
icon={<LoginOutlined />}
|
||||||
onClick={() => Modal.confirm({
|
onClick={() => modal.confirm({
|
||||||
|
maskClosable: true,
|
||||||
title: '确认登出?',
|
title: '确认登出?',
|
||||||
cancelText: '回到首页',
|
cancelText: '保持登录',
|
||||||
cancelButtonProps: { icon: <HomeOutlined /> },
|
cancelButtonProps: { icon: <HomeOutlined /> },
|
||||||
onCancel: () => navigate('/'),
|
|
||||||
okText: '确认登出',
|
okText: '确认登出',
|
||||||
okButtonProps: {
|
okButtonProps: {
|
||||||
icon: <LogoutOutlined />,
|
icon: <LogoutOutlined />,
|
||||||
@ -91,5 +93,6 @@ export const UserCenter = () => {
|
|||||||
登出
|
登出
|
||||||
</Button>
|
</Button>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user