9
9
updateUserProfileInfo ,
10
10
} from "~/datasources/queries/users" ;
11
11
import { getUsername } from "~/datasources/queries/utils/createUsername" ;
12
- import { unauthorizedError } from "~/errors" ;
12
+ import { applicationError , ServiceErrors , unauthorizedError } from "~/errors" ;
13
+
14
+ const preventUserUpdate = new Set < "retool" > ( [ "retool" ] ) ;
13
15
14
16
// Obtener el token de autorización de la solicitud, ya sea del encabezado de
15
17
// autorización o de la cookie "community-os-access-token"
@@ -25,12 +27,12 @@ const getAuthToken = (request: Request) => {
25
27
return null ;
26
28
} ;
27
29
28
- export const createAuthToken = async ( user : USER , SECRET : string ) => {
30
+ export const createMinimalAuthToken = async ( user : USER , SECRET : string ) => {
29
31
const payload = {
30
- audience : "retool-autenticated " ,
31
- id : user . id ,
32
- email : user . email ,
33
- user_metadata : user ,
32
+ audience : "retool" ,
33
+ user_metadata : {
34
+ sub : user . id ,
35
+ } ,
34
36
exp : Date . now ( ) + 60 * 60 * 24 * 1000 /* 24 hours */ ,
35
37
} ;
36
38
@@ -136,26 +138,39 @@ export const upsertUserFromRequest = async ({
136
138
throw unauthorizedError ( "Token expired" , logger ) ;
137
139
}
138
140
139
- const { avatar_url, name, user_name, email_verified, sub, picture } =
140
- payload . user_metadata ;
141
- const profileInfo = insertUsersSchema . safeParse ( {
142
- email : payload . email . toLowerCase ( ) ,
143
- isEmailVerified : email_verified ,
144
- imageUrl : avatar_url ? avatar_url : picture ? picture : "" ,
145
- externalId : sub ,
146
- name,
147
- username : user_name ?? getUsername ( ) ,
148
- publicMetadata : payload ,
149
- } ) ;
150
-
151
- if ( profileInfo . success === false ) {
152
- logger . error ( "Could not parse profile info" , profileInfo . error ) ;
153
- throw new Error ( "Could not parse profile info" , profileInfo . error ) ;
154
- }
141
+ if ( payload . audience && preventUserUpdate . has ( payload . audience ) ) {
142
+ const userId = payload . user_metadata . sub ;
143
+
144
+ logger . info ( `Preventing update for user ID: ${ userId } ` ) ;
145
+ const user = await findUserByID ( DB , userId ) ;
146
+
147
+ if ( ! user ) {
148
+ throw applicationError ( "User not found" , ServiceErrors . FORBIDDEN , logger ) ;
149
+ }
155
150
156
- logger . info ( `Updating profile Info for user ID: ${ sub } ` ) ;
151
+ return user ;
152
+ } else {
153
+ const { avatar_url, name, user_name, email_verified, sub, picture } =
154
+ payload . user_metadata ;
155
+ const profileInfo = insertUsersSchema . safeParse ( {
156
+ email : payload . email . toLowerCase ( ) ,
157
+ isEmailVerified : email_verified ,
158
+ imageUrl : avatar_url ? avatar_url : picture ? picture : "" ,
159
+ externalId : sub ,
160
+ name,
161
+ username : user_name ?? getUsername ( ) ,
162
+ publicMetadata : payload ,
163
+ } ) ;
164
+
165
+ if ( profileInfo . success === false ) {
166
+ logger . error ( "Could not parse profile info" , profileInfo . error ) ;
167
+ throw new Error ( "Could not parse profile info" , profileInfo . error ) ;
168
+ }
169
+
170
+ logger . info ( `Updating profile Info for user ID: ${ sub } ` ) ;
157
171
158
- return updateUserProfileInfo ( DB , profileInfo . data , logger ) ;
172
+ return updateUserProfileInfo ( DB , profileInfo . data , logger ) ;
173
+ }
159
174
} ;
160
175
161
176
export const logPossibleUserIdFromJWT = (
0 commit comments