11import { APIGatewayProxyEvent , APIGatewayProxyResult } from 'aws-lambda' ;
22import { PrivyClient , User } from '@privy-io/server-auth' ;
33
4- // Define types for social media usernames
5- type SocialUsernames = {
4+ // Define types for all identifiers
5+ type UserIdentifiers = {
6+ email ?: string ;
67 google ?: string ;
78 apple ?: string ;
89 twitter ?: string ;
@@ -13,14 +14,9 @@ type SocialUsernames = {
1314 linkedin ?: string ;
1415} ;
1516
16- // Define the user identifiers interface
17- interface UserIdentifiers {
18- email ?: string ;
19- usernames : SocialUsernames ;
20- }
21-
2217// Define social account types for type safety
23- type SocialAccountType =
18+ type SocialAccountType =
19+ | 'email'
2420 | 'google_oauth'
2521 | 'apple_oauth'
2622 | 'twitter_oauth'
@@ -30,49 +26,38 @@ type SocialAccountType =
3026 | 'instagram_oauth'
3127 | 'linkedin_oauth' ;
3228
33- // Map social account types to their username field names
34- const SOCIAL_ACCOUNT_MAPPINGS : Record < SocialAccountType , keyof SocialUsernames > = {
35- 'google_oauth' : 'google' ,
36- 'apple_oauth' : 'apple' ,
37- 'twitter_oauth' : 'twitter' ,
38- 'discord_oauth' : 'discord' ,
39- 'github_oauth' : 'github' ,
40- 'telegram' : 'telegram' ,
41- 'instagram_oauth' : 'instagram' ,
42- 'linkedin_oauth' : 'linkedin'
43- } ;
44-
4529/**
4630 * Extracts user identifiers (email and social media usernames) from a Privy user
4731 * @param user - The Privy user object
48- * @returns UserIdentifiers object containing email and social media usernames
32+ * @returns UserIdentifiers object containing all identifiers
4933*/
5034function getUserIdentifiers ( user : User ) : UserIdentifiers {
51- const identifiers : UserIdentifiers = {
52- usernames : { }
53- } ;
54-
35+ const identifiers : UserIdentifiers = { } ;
36+
5537 // Get email if available
5638 if ( user . email ?. address ) {
57- identifiers . email = user . email . address ;
39+ identifiers . email = user . email . address ;
5840 }
59-
41+
6042 // Get usernames from linked accounts
6143 user . linkedAccounts . forEach ( account => {
6244 const accountType = account . type as SocialAccountType ;
63- const usernameField = SOCIAL_ACCOUNT_MAPPINGS [ accountType ] ;
45+ // Remove '_oauth' suffix to get the field name
46+ const field = accountType . replace ( '_oauth' , '' ) as keyof UserIdentifiers ;
6447
65- if ( usernameField ) {
66- // Handle email-based accounts (google, apple)
67- if ( accountType === 'google_oauth' || accountType === 'apple_oauth' || accountType === 'linkedin_oauth' ) {
68- const emailAccount = account as { email ?: string } ;
69- identifiers . usernames [ usernameField ] = emailAccount . email || undefined ;
70- }
71- // Handle username-based accounts
72- else {
73- const usernameAccount = account as { username ?: string } ;
74- identifiers . usernames [ usernameField ] = usernameAccount . username || undefined ;
48+ // Handle email-based accounts (email, google, apple, linkedin)
49+ if ( accountType === 'email' || accountType === 'google_oauth' || accountType === 'apple_oauth' || accountType === 'linkedin_oauth' ) {
50+ const emailAccount = account as { email ?: string } | { address ?: string } ;
51+ if ( 'email' in emailAccount ) {
52+ identifiers [ field ] = emailAccount . email ;
53+ } else if ( 'address' in emailAccount ) {
54+ identifiers [ field ] = emailAccount . address ;
7555 }
56+ }
57+ // Handle username-based accounts
58+ else {
59+ const usernameAccount = account as { username ?: string } ;
60+ identifiers [ field ] = usernameAccount . username || undefined ;
7661 }
7762 } ) ;
7863 return identifiers ;
0 commit comments