85 lines
2.0 KiB
TypeScript
85 lines
2.0 KiB
TypeScript
|
import type { NextApiRequest, NextApiResponse } from 'next'
|
||
|
import { supabase } from '../../../lib/supabase'
|
||
|
|
||
|
interface UserInfo {
|
||
|
id: string
|
||
|
email: string
|
||
|
name: string
|
||
|
phone?: string
|
||
|
user_type: 'individual' | 'enterprise' | 'admin'
|
||
|
status: 'active' | 'inactive' | 'suspended'
|
||
|
enterprise_id?: string
|
||
|
avatar_url?: string
|
||
|
created_at: string
|
||
|
updated_at: string
|
||
|
}
|
||
|
|
||
|
interface ApiResponse {
|
||
|
success: boolean
|
||
|
data?: UserInfo
|
||
|
error?: string
|
||
|
}
|
||
|
|
||
|
export default async function handler(
|
||
|
req: NextApiRequest,
|
||
|
res: NextApiResponse<ApiResponse>
|
||
|
) {
|
||
|
if (req.method !== 'GET') {
|
||
|
return res.status(405).json({
|
||
|
success: false,
|
||
|
error: '方法不允许'
|
||
|
})
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// 获取授权头
|
||
|
const authHeader = req.headers.authorization
|
||
|
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||
|
return res.status(401).json({
|
||
|
success: false,
|
||
|
error: '未提供有效的授权令牌'
|
||
|
})
|
||
|
}
|
||
|
|
||
|
const token = authHeader.substring(7) // 移除 'Bearer ' 前缀
|
||
|
|
||
|
// 验证JWT令牌
|
||
|
const { data: { user }, error: authError } = await supabase.auth.getUser(token)
|
||
|
|
||
|
if (authError || !user) {
|
||
|
return res.status(401).json({
|
||
|
success: false,
|
||
|
error: '无效的授权令牌'
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// 从数据库获取用户详细信息
|
||
|
const { data: userProfile, error: profileError } = await supabase
|
||
|
.from('users')
|
||
|
.select('*')
|
||
|
.eq('id', user.id)
|
||
|
.single()
|
||
|
|
||
|
if (profileError) {
|
||
|
console.error('Error fetching user profile:', profileError)
|
||
|
return res.status(404).json({
|
||
|
success: false,
|
||
|
error: '用户信息不存在'
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return res.status(200).json({
|
||
|
success: true,
|
||
|
data: userProfile
|
||
|
})
|
||
|
|
||
|
} catch (error) {
|
||
|
console.error('Server error getting user info:', error)
|
||
|
return res.status(500).json({
|
||
|
success: false,
|
||
|
error: process.env.NODE_ENV === 'development'
|
||
|
? `服务器错误: ${error instanceof Error ? error.message : '未知错误'}`
|
||
|
: '服务器内部错误'
|
||
|
})
|
||
|
}
|
||
|
}
|