add profile
This commit is contained in:
54
server/routers/kfu-m-24-1/sber_mobile/profile.js
Normal file
54
server/routers/kfu-m-24-1/sber_mobile/profile.js
Normal file
@@ -0,0 +1,54 @@
|
||||
const router = require('express').Router();
|
||||
const { getSupabaseClient } = require('./supabaseClient');
|
||||
|
||||
// GET /profile
|
||||
router.get('/profile', async (req, res) => {
|
||||
const { user_id } = req.body;
|
||||
const supabase = getSupabaseClient();
|
||||
let { data: userData, error: userError } = await supabase.auth.admin.getUserById(user_id);
|
||||
|
||||
if (userError) return res.status(400).json({ error: userError.message });
|
||||
|
||||
let { data: profileData, error: profileError } = await supabase.from('user_profiles').select(`
|
||||
id,
|
||||
full_name,
|
||||
avatar_url,
|
||||
updated_at
|
||||
`).eq('id', user_id).single();
|
||||
|
||||
if (profileError) return res.status(400).json({ error: profileError.message });
|
||||
|
||||
res.json({
|
||||
id: profileData.id,
|
||||
username: profileData.full_name,
|
||||
avatar_url: profileData.avatar_url,
|
||||
phone: userData.user.phone,
|
||||
apartment: '9',
|
||||
updated_at: profileData.updated_at
|
||||
});
|
||||
});
|
||||
|
||||
// POST /profile
|
||||
router.post('/profile', async (req, res) => {
|
||||
const { user_id, data } = req.body;
|
||||
const supabase = getSupabaseClient();
|
||||
|
||||
const { data: userData, error: userError } = await supabase.auth.admin.updateUserById(
|
||||
user_id,
|
||||
{ phone: data.phone }
|
||||
)
|
||||
|
||||
if (userError) return res.status(400).json({ error: userError.message });
|
||||
|
||||
let { error: profileError } = await supabase.from('user_profiles').update({
|
||||
full_name: data.username,
|
||||
avatar_url: data.avatar_url,
|
||||
// apartment: data.apartment
|
||||
}).eq('id', user_id).single();
|
||||
|
||||
if (profileError) return res.status(400).json({ error: profileError.message });
|
||||
|
||||
res.json({ success: true });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user