import React, { useState, useEffect } from 'react' import { useSearchParams, useNavigate } from 'react-router-dom' import { useAuth } from '../contexts/AuthContext' import { Container, Paper, Typography, Box, Button, Grid, Card, CardContent, List, ListItem, ListItemIcon, ListItemText, Chip, Alert, CircularProgress } from '@mui/material' import { Security, CheckCircle, Cancel, Person, Email, CalendarToday, Visibility, VisibilityOff } from '@mui/icons-material' import axios from 'axios' const OAuthAuthorize = () => { const { user } = useAuth() const [searchParams] = useSearchParams() const navigate = useNavigate() const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [clientInfo, setClientInfo] = useState(null) const clientId = searchParams.get('client_id') const redirectUri = searchParams.get('redirect_uri') const scope = searchParams.get('scope') const state = searchParams.get('state') const responseType = searchParams.get('response_type') useEffect(() => { if (!user) { navigate('/login') return } if (!clientId || !redirectUri || responseType !== 'code') { setError('无效的授权请求') return } // 这里可以添加客户端信息获取逻辑 // 为了演示,我们使用默认信息 setClientInfo({ name: '第三方应用', description: '请求访问您的账户信息', scopes: scope ? scope.split(' ') : ['read', 'write'] }) }, [user, clientId, redirectUri, scope, responseType, navigate]) const handleAuthorize = async () => { setLoading(true) try { const params = new URLSearchParams({ response_type: 'code', client_id: clientId, redirect_uri: redirectUri, scope: scope || 'read write', state: state || '' }) const response = await axios.get(`/api/oauth/authorize?${params}`, { headers: { Authorization: `Bearer ${localStorage.getItem('token')}` } }) if (response.data.success) { const { redirect_url } = response.data.data window.location.href = redirect_url } } catch (error) { setError(error.response?.data?.message || '授权失败') } setLoading(false) } const handleDeny = () => { // 拒绝授权,重定向回应用 const denyUrl = new URL(redirectUri) denyUrl.searchParams.set('error', 'access_denied') if (state) { denyUrl.searchParams.set('state', state) } window.location.href = denyUrl.toString() } if (!user) { return ( ) } return ( 授权请求 第三方应用请求访问您的账户 {error && ( {error} )} {clientInfo && ( <> {/* 应用信息 */} {clientInfo.name} {clientInfo.description} 请求的权限: {clientInfo.scopes.map((scope) => ( ))} {/* 用户信息 */} 您的账户信息 {/* 操作按钮 */} )} ) } export default OAuthAuthorize