@@ -9,7 +9,6 @@ import { logAndThrowError, makeError, printLog, SPECIFIC_ERRORS } from "../lib/e
9
9
import { LogLevel } from "../types/enums"
10
10
11
11
dotenv . config ( )
12
-
13
12
/**
14
13
* Record the authenticated user information inside the Firestore DB upon authentication.
15
14
* @dev the data is recorded in a new document in the `users` collection.
@@ -24,31 +23,24 @@ export const registerAuthUser = functions
24
23
. onCreate ( async ( user : UserRecord ) => {
25
24
// Get DB.
26
25
const firestore = admin . firestore ( )
27
-
28
26
// Get user information.
29
27
if ( ! user . uid ) logAndThrowError ( SPECIFIC_ERRORS . SE_AUTH_NO_CURRENT_AUTH_USER )
30
-
31
28
// The user object has basic properties such as display name, email, etc.
32
29
const { displayName } = user
33
30
const { email } = user
34
31
const { photoURL } = user
35
32
const { emailVerified } = user
36
-
37
33
// Metadata.
38
34
const { creationTime } = user . metadata
39
35
const { lastSignInTime } = user . metadata
40
-
41
36
// The user's ID, unique to the Firebase project. Do NOT use
42
37
// this value to authenticate with your backend server, if
43
38
// you have one. Use User.getToken() instead.
44
39
const { uid } = user
45
-
46
40
// Reference to a document using uid.
47
41
const userRef = firestore . collection ( commonTerms . collections . users . name ) . doc ( uid )
48
-
49
42
// html encode the display name
50
43
const encodedDisplayName = encode ( displayName )
51
-
52
44
// we only do reputation check if the user is not a coordinator
53
45
if (
54
46
! (
@@ -60,18 +52,18 @@ export const registerAuthUser = functions
60
52
// if provider == github.com let's use our functions to check the user's reputation
61
53
if ( user . providerData [ 0 ] . providerId === "github.com" ) {
62
54
const vars = getGitHubVariables ( )
55
+
63
56
// this return true or false
64
57
try {
65
58
const res = await githubReputation (
66
- user . displayName ! ,
59
+ user . providerData [ 0 ] . uid ,
67
60
vars . minimumFollowing ,
68
61
vars . minimumFollowers ,
69
62
vars . minimumPublicRepos
70
63
)
71
64
if ( ! res ) {
72
65
// Delete user
73
66
await auth . deleteUser ( user . uid )
74
-
75
67
// Throw error
76
68
logAndThrowError (
77
69
makeError (
@@ -89,13 +81,12 @@ export const registerAuthUser = functions
89
81
makeError (
90
82
"permission-denied" ,
91
83
"There was an error while checking the user's Github reputation." ,
92
- `There was an error while checking the user's Github reputation. This is likely due to GitHub rate limiting. Please contact the administrator if you think this is a mistake. `
84
+ `${ error } `
93
85
)
94
86
)
95
87
}
96
88
}
97
89
}
98
-
99
90
// Set document (nb. we refer to providerData[0] because we use Github OAuth provider only).
100
91
await userRef . set ( {
101
92
name : encodedDisplayName ,
@@ -109,10 +100,8 @@ export const registerAuthUser = functions
109
100
photoURL : photoURL || "" ,
110
101
lastUpdated : getCurrentServerTimestampInMillis ( )
111
102
} )
112
-
113
103
printLog ( `Authenticated user document with identifier ${ uid } has been correctly stored` , LogLevel . DEBUG )
114
104
} )
115
-
116
105
/**
117
106
* Set custom claims for role-based access control on the newly created user.
118
107
* @notice this method is automatically triggered upon user authentication in the Firebase app
@@ -126,32 +115,26 @@ export const processSignUpWithCustomClaims = functions
126
115
. onCreate ( async ( user : UserRecord ) => {
127
116
// Get user information.
128
117
if ( ! user . uid ) logAndThrowError ( SPECIFIC_ERRORS . SE_AUTH_NO_CURRENT_AUTH_USER )
129
-
130
118
// Prepare state.
131
119
let customClaims : any
132
-
133
120
// Check if user meets role criteria to be a coordinator.
134
121
if (
135
122
user . email &&
136
123
( user . email . endsWith ( `@${ process . env . CUSTOM_CLAIMS_COORDINATOR_EMAIL_ADDRESS_OR_DOMAIN } ` ) ||
137
124
user . email === process . env . CUSTOM_CLAIMS_COORDINATOR_EMAIL_ADDRESS_OR_DOMAIN )
138
125
) {
139
126
customClaims = { coordinator : true }
140
-
141
127
printLog ( `Authenticated user ${ user . uid } has been identified as coordinator` , LogLevel . DEBUG )
142
128
} else {
143
129
customClaims = { participant : true }
144
-
145
130
printLog ( `Authenticated user ${ user . uid } has been identified as participant` , LogLevel . DEBUG )
146
131
}
147
-
148
132
try {
149
133
// Set custom user claims on this newly created user.
150
134
await admin . auth ( ) . setCustomUserClaims ( user . uid , customClaims )
151
135
} catch ( error : any ) {
152
136
const specificError = SPECIFIC_ERRORS . SE_AUTH_SET_CUSTOM_USER_CLAIMS_FAIL
153
137
const additionalDetails = error . toString ( )
154
-
155
138
logAndThrowError ( makeError ( specificError . code , specificError . message , additionalDetails ) )
156
139
}
157
140
} )
0 commit comments