@lastbrain/module-auth 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ -- Function to get detailed user information by ID
2
+ -- Module: @lastbrain/module-auth
3
+
4
+ -- =====================================================
5
+ -- Function: get_admin_user_details
6
+ -- =====================================================
7
+ -- RPC function for admins to get detailed user data by ID
8
+ CREATE OR REPLACE FUNCTION public.get_admin_user_details(user_id UUID)
9
+ RETURNS JSON
10
+ LANGUAGE plpgsql
11
+ SECURITY DEFINER
12
+ AS $$
13
+ DECLARE
14
+ result JSON;
15
+ BEGIN
16
+ -- Check if user is superadmin
17
+ IF NOT is_superadmin(auth.uid()) THEN
18
+ RAISE EXCEPTION 'Access denied. Superadmin required.';
19
+ END IF;
20
+
21
+ -- Build the detailed user result JSON
22
+ SELECT json_build_object(
23
+ 'id', p.owner_id,
24
+ 'email', COALESCE(au.email, 'N/A'),
25
+ 'created_at', COALESCE(au.created_at, p.created_at),
26
+ 'email_confirmed_at', au.email_confirmed_at,
27
+ 'last_sign_in_at', au.last_sign_in_at,
28
+ 'role', COALESCE(au.raw_app_meta_data->'roles'->>0, 'user'),
29
+ 'full_name', COALESCE(
30
+ TRIM(CONCAT(COALESCE(p.first_name, ''), ' ', COALESCE(p.last_name, ''))),
31
+ au.raw_user_meta_data->>'full_name'
32
+ ),
33
+ 'avatar_url', COALESCE(p.avatar_url, au.raw_user_meta_data->>'avatar'),
34
+ 'raw_app_meta_data', COALESCE(au.raw_app_meta_data, '{}'::jsonb),
35
+ 'raw_user_meta_data', COALESCE(au.raw_user_meta_data, '{}'::jsonb),
36
+ 'profile', json_build_object(
37
+ 'first_name', p.first_name,
38
+ 'last_name', p.last_name,
39
+ 'bio', p.bio,
40
+ 'phone', p.phone,
41
+ 'company', p.company,
42
+ 'website', p.website,
43
+ 'location', p.location,
44
+ 'language', p.language,
45
+ 'timezone', p.timezone,
46
+ 'preferences', p.preferences,
47
+ 'avatar_url', p.avatar_url,
48
+ 'created_at', p.created_at,
49
+ 'updated_at', p.updated_at
50
+ )
51
+ ) INTO result
52
+ FROM public.user_profil p
53
+ LEFT JOIN auth.users au ON p.owner_id = au.id
54
+ WHERE p.owner_id = user_id;
55
+
56
+ -- Return null if user not found
57
+ IF result IS NULL THEN
58
+ RETURN NULL;
59
+ END IF;
60
+
61
+ RETURN result;
62
+ END;
63
+ $$;