|
7 | 7 | import express, { Response } from 'express'; |
8 | 8 | import { supabase } from '../config/database'; |
9 | 9 | import { authenticate, AuthenticatedRequest } from '../middleware/auth'; |
| 10 | +import { validateRequest } from '../utils/validation'; |
| 11 | +import { userProfileUpdateSchema } from '../schemas/user-profile'; |
10 | 12 | import logger from '../config/logger'; |
11 | 13 | import { roleService } from '../services/role-service'; |
12 | 14 |
|
@@ -148,4 +150,60 @@ router.delete('/account', async (req: AuthenticatedRequest, res: Response) => { |
148 | 150 | } |
149 | 151 | }); |
150 | 152 |
|
| 153 | +/** |
| 154 | + * GET /api/user/profile |
| 155 | + * Returns the current user's profile data. |
| 156 | + */ |
| 157 | +router.get('/profile', async (req: AuthenticatedRequest, res: Response) => { |
| 158 | + try { |
| 159 | + const userId = req.user!.id; |
| 160 | + const { data: profile, error } = await supabase |
| 161 | + .from('profiles') |
| 162 | + .select('id, display_name, company_name, plan_type, stealth_meta_address, created_at, updated_at') |
| 163 | + .eq('id', userId) |
| 164 | + .single(); |
| 165 | + |
| 166 | + if (error) { |
| 167 | + logger.error('Error fetching user profile:', error); |
| 168 | + return res.status(500).json({ success: false, error: 'Failed to fetch profile' }); |
| 169 | + } |
| 170 | + |
| 171 | + return res.status(200).json({ success: true, data: profile }); |
| 172 | + } catch (error) { |
| 173 | + logger.error('Error fetching user profile:', error); |
| 174 | + return res.status(500).json({ success: false, error: 'Failed to fetch profile' }); |
| 175 | + } |
| 176 | +}); |
| 177 | + |
| 178 | +/** |
| 179 | + * PUT /api/user/profile |
| 180 | + * Update profile fields such as display name, company name, or stealth meta-address. |
| 181 | + */ |
| 182 | +router.put('/profile', async (req: AuthenticatedRequest, res: Response) => { |
| 183 | + try { |
| 184 | + const userId = req.user!.id; |
| 185 | + const validatedData = validateRequest(userProfileUpdateSchema, req.body); |
| 186 | + |
| 187 | + const { data: profile, error } = await supabase |
| 188 | + .from('profiles') |
| 189 | + .update({ |
| 190 | + ...validatedData, |
| 191 | + updated_at: new Date().toISOString(), |
| 192 | + }) |
| 193 | + .eq('id', userId) |
| 194 | + .select() |
| 195 | + .single(); |
| 196 | + |
| 197 | + if (error) { |
| 198 | + logger.error('Error updating user profile:', error); |
| 199 | + return res.status(500).json({ success: false, error: 'Failed to update profile' }); |
| 200 | + } |
| 201 | + |
| 202 | + return res.status(200).json({ success: true, data: profile }); |
| 203 | + } catch (error) { |
| 204 | + logger.error('Error updating user profile:', error); |
| 205 | + return res.status(500).json({ success: false, error: 'Failed to update profile' }); |
| 206 | + } |
| 207 | +}); |
| 208 | + |
151 | 209 | export default router; |
0 commit comments