Files
bw-expo-app/components/auth/auth-prompt.tsx
imeepos 668caaa91f 🔐 feat: 认证系统升级 - 从重定向到底部Modal的完整改造
## 核心功能

### 1. 全局认证架构
- 新增 AuthProvider:全局认证上下文,管理登录状态和Modal
- 新增 useAuthGuard Hook:提供 requireAuth 认证拦截功能
- 新增 AuthGuard 组件:包装需要认证的组件
- 新增 AuthPrompt 组件:多种样式的登录引导组件

### 2. 登录体验优化
- LoginModal 已优化为底部抽屉式设计
- 登录成功后自动关闭Modal并恢复用户操作
- 支持队列机制,多个认证检查依次执行

### 3. 页面架构调整
- 根布局集成 AuthProvider
- 移除 Tabs 布局的重定向逻辑
- Explore 页面添加认证保护,显示登录提示

### 4. API安全增强
- 新增 TokenManager:完整的Token生命周期管理
- 新增 SecureClient:安全API客户端,自动处理认证
- 更新 API 客户端:支持认证头和自动Token刷新

### 5. 文档完善
- AUTH_SYSTEM.md:完整的认证系统使用指南
- AUTH_UPGRADE_SUMMARY.md:升级总结文档

## 技术亮点

 TypeScript 完整支持,编译时类型检查
 React 最佳实践,合理使用 Hooks 和 Context
 优雅的错误处理,友好的用户提示
 流畅的动画效果,原生级用户体验

## 核心优势

- 流畅:无需页面跳转,Modal即用即走
- 智能:自动检测登录需求,无需手动判断
- 安全:完整Token管理和自动刷新机制
- 灵活:多种认证组件满足不同场景
- 易用:详细文档和示例,快速上手

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-31 11:18:20 +08:00

201 lines
5.1 KiB
TypeScript

import React from 'react';
import { View, StyleSheet, TouchableOpacity } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { LinearGradient } from 'expo-linear-gradient';
import { ThemedText } from '@/components/themed-text';
import { useAuthGuard } from '@/hooks/use-auth-guard';
interface AuthPromptProps {
variant?: 'card' | 'fullscreen' | 'inline';
title?: string;
subtitle?: string;
actionLabel?: string;
showIcon?: boolean;
iconName?: keyof typeof Ionicons.glyphMap;
gradientColors?: [string, string, ...string[]];
onLoginPress?: () => void;
}
/**
* 认证提示组件
* 为未登录用户提供清晰的登录引导
*/
export function AuthPrompt({
variant = 'card',
title = '需要登录',
subtitle = '登录后即可使用所有功能',
actionLabel = '立即登录',
showIcon = true,
iconName = 'log-in-outline',
gradientColors = ['#007AFF', '#0056D2'] as [string, string],
onLoginPress,
}: AuthPromptProps) {
const { requireAuth } = useAuthGuard();
const handleLogin = () => {
if (onLoginPress) {
onLoginPress();
} else {
requireAuth();
}
};
if (variant === 'fullscreen') {
return (
<View style={styles.fullscreenContainer}>
<View style={styles.fullscreenContent}>
{showIcon && (
<View style={styles.iconContainer}>
<Ionicons name={iconName} size={32} color="#007AFF" />
</View>
)}
<ThemedText style={styles.title}>{title}</ThemedText>
<ThemedText style={styles.subtitle}>{subtitle}</ThemedText>
<TouchableOpacity style={styles.actionButton} onPress={handleLogin}>
<LinearGradient
colors={gradientColors}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 1 }}
style={styles.buttonGradient}
>
<Ionicons name={iconName} size={20} color="#fff" style={styles.buttonIcon} />
<ThemedText style={styles.buttonText}>{actionLabel}</ThemedText>
</LinearGradient>
</TouchableOpacity>
</View>
</View>
);
}
if (variant === 'card') {
return (
<View style={styles.cardContainer}>
{showIcon && (
<View style={styles.iconContainer}>
<Ionicons name={iconName} size={32} color="#007AFF" />
</View>
)}
<ThemedText style={styles.title}>{title}</ThemedText>
<ThemedText style={styles.subtitle}>{subtitle}</ThemedText>
<TouchableOpacity style={styles.cardButton} onPress={handleLogin}>
<LinearGradient
colors={gradientColors}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 1 }}
style={styles.buttonGradient}
>
<Ionicons name={iconName} size={16} color="#fff" style={styles.buttonIcon} />
<ThemedText style={styles.buttonText}>{actionLabel}</ThemedText>
</LinearGradient>
</TouchableOpacity>
</View>
);
}
return (
<View style={styles.inlineContainer}>
<ThemedText style={styles.inlineTitle}>{title}</ThemedText>
<TouchableOpacity style={styles.inlineButton} onPress={handleLogin}>
<ThemedText style={styles.inlineButtonText}>{actionLabel}</ThemedText>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
fullscreenContainer: {
flex: 1,
backgroundColor: '#050505',
},
fullscreenContent: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingHorizontal: 40,
},
iconContainer: {
width: 100,
height: 100,
borderRadius: 50,
backgroundColor: 'rgba(0, 122, 255, 0.1)',
alignItems: 'center',
justifyContent: 'center',
marginBottom: 32,
},
title: {
fontSize: 28,
fontWeight: '700',
color: '#ffffff',
marginBottom: 16,
textAlign: 'center',
},
subtitle: {
fontSize: 16,
color: 'rgba(255, 255, 255, 0.6)',
textAlign: 'center',
lineHeight: 24,
marginBottom: 48,
},
actionButton: {
width: '100%',
height: 56,
borderRadius: 16,
overflow: 'hidden',
elevation: 8,
},
cardContainer: {
backgroundColor: 'rgba(255, 255, 255, 0.05)',
borderRadius: 20,
padding: 24,
alignItems: 'center',
borderWidth: 1,
borderColor: 'rgba(255, 255, 255, 0.1)',
margin: 20,
},
cardButton: {
width: '100%',
height: 48,
borderRadius: 12,
overflow: 'hidden',
},
buttonGradient: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
height: '100%',
},
buttonIcon: {
marginRight: 8,
},
buttonText: {
color: '#fff',
fontSize: 17,
fontWeight: '700',
},
inlineContainer: {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 16,
paddingVertical: 12,
backgroundColor: 'rgba(255, 255, 255, 0.05)',
borderRadius: 12,
},
inlineTitle: {
flex: 1,
fontSize: 14,
color: '#ffffff',
marginRight: 12,
},
inlineButton: {
paddingHorizontal: 16,
paddingVertical: 8,
backgroundColor: '#007AFF',
borderRadius: 8,
},
inlineButtonText: {
color: '#fff',
fontSize: 14,
fontWeight: '600',
},
});