I want to add a row to another model when a user signs up besides User

How could i add another row to another model related to the User model when a user signs up succesfully. I wnat to do this whenever the row for a user is created
3 Replies
kapa.ai
kapa.ai2w ago
Hi @Sven. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To add a row to another model related to the User model when a user signs up successfully, you can create a custom signup action in Wasp. This allows you to execute additional logic after the user is created. Here's how you can do it: 1. First, define your custom signup action in your main.wasp file:
action customSignup {
fn: import { signup } from "@src/auth/signup.js",
entities: [User]
}
action customSignup {
fn: import { signup } from "@src/auth/signup.js",
entities: [User]
}
2. Then, implement the custom signup logic in your src/auth/signup.js file:
import {
createProviderId,
sanitizeAndSerializeProviderData,
createUser,
} from 'wasp/server/auth'

export const signup = async (args, { entities }) => {
try {
const providerId = createProviderId('username', args.username)
const providerData = await sanitizeAndSerializeProviderData({
hashedPassword: args.password,
})

const newUser = await createUser(
providerId,
providerData,
// Any additional data you want to store on the User entity
{},
)

// Add a row to another model related to the User
await entities.YourRelatedModel.create({
data: {
// Your related model fields
user: {
connect: { id: newUser.id }
}
}
})

return {
success: true,
message: 'User created successfully',
}
} catch (e) {
return {
success: false,
message: e.message,
}
}
}
import {
createProviderId,
sanitizeAndSerializeProviderData,
createUser,
} from 'wasp/server/auth'

export const signup = async (args, { entities }) => {
try {
const providerId = createProviderId('username', args.username)
const providerData = await sanitizeAndSerializeProviderData({
hashedPassword: args.password,
})

const newUser = await createUser(
providerId,
providerData,
// Any additional data you want to store on the User entity
{},
)

// Add a row to another model related to the User
await entities.YourRelatedModel.create({
data: {
// Your related model fields
user: {
connect: { id: newUser.id }
}
}
})

return {
success: true,
message: 'User created successfully',
}
} catch (e) {
return {
success: false,
message: e.message,
}
}
}
In this example, replace YourRelatedModel with the name of the model you want to create a row for when a user signs up. Make sure to define the relationship between your User model and the related model in your Wasp file. This approach allows you to create a custom signup process where you can add additional logic, such as creating related records, after the user is successfully created. Custom Signup Action (https://wasp-lang.dev/docs/auth/entities#custom-signup-action) Remember to adjust the code according to your specific models and requirements. If you need more detailed information about customizing the signup process, you can refer to the Customizing the Signup Process (https://wasp-lang.dev/docs/auth/overview#customizing-the-signup-process) section in the Wasp documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Sven, 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?
Sven
SvenOP2w ago
what i dont understand
Filip
Filip2w ago
Ignore the custom signup action part, Kapa is halucinating. You should use the onAfterSignup hook. Inside, use prisma to access the db table (model) you want to change. Wasp will call this function whenever a user sucessfully signs up.
Want results from more Discord servers?
Add your server