Populate a database field on SignUp

@kapa.ai When a user signs up, I want my application to automatically populate a field in the database, its called apiKey. I have created API logic GetApiKey and regenerateAPIKey already. What advise can you give to an LLM agent doing this coding.
1 Reply
kapa.ai
kapa.ai2mo ago
Hi @Gareth_Simpson. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To automatically populate a database field (like apiKey) during user signup in Wasp, you should use the userSignupFields mechanism. This allows you to define logic for setting extra fields on your User entity at signup time. Here’s how you can approach this: 1. Add the apiKey field to your User entity in main.wasp:
entity User {=psl
id Int @id @default(autoincrement())
apiKey String
psl=}

entity User {=psl
id Int @id @default(autoincrement())
apiKey String
psl=}

2. Define userSignupFields in your auth logic (e.g., src/auth/signup.js or src/auth/signup.ts). Here, you can call your GetApiKey logic to generate the apiKey value:
import { defineUserSignupFields } from 'wasp/server/auth'
import { GetApiKey } from 'path/to/your/api/logic'

export const userSignupFields = defineUserSignupFields({
apiKey: async (_data) => {
// Call your logic to generate the API key
return await GetApiKey()
}
})

import { defineUserSignupFields } from 'wasp/server/auth'
import { GetApiKey } from 'path/to/your/api/logic'

export const userSignupFields = defineUserSignupFields({
apiKey: async (_data) => {
// Call your logic to generate the API key
return await GetApiKey()
}
})

This function will be called automatically during signup, and the returned value will be saved in the apiKey field for the new user. 3. Wire up userSignupFields in main.wasp:
auth: {
userEntity: User,
methods: {
usernameAndPassword: {
userSignupFields: import { userSignupFields } from "@src/auth/signup",
},
},
onAuthFailedRedirectTo: "/login",
}

auth: {
userEntity: User,
methods: {
usernameAndPassword: {
userSignupFields: import { userSignupFields } from "@src/auth/signup",
},
},
onAuthFailedRedirectTo: "/login",
}

Key points for an LLM agent: - Use userSignupFields to set extra fields at signup (docs reference). - The function for each field can be async and can call any logic, including your GetApiKey. - Do not include sensitive fields like password in userSignupFields; Wasp handles those separately. - Make sure the field exists on your User entity in main.wasp. This approach is the recommended and idiomatic way in Wasp to populate fields like apiKey during signup, and it is fully supported by the framework’s design for customizing the signup process Docs > Auth > Overview > Customizing the Signup Process. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Gareth_Simpson, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?

Did you find this page helpful?