Auto populating new tables on user sign in
I created two models that are related to User in the same way Account and Session are. When a new user signs in User Session and Account all get populated with data, but GameData and UpgradeData dont seem to even with default values supplied. Ive looked through trpc.ts and auth.ts to see if I need to add anything in those files but I dont see anything related.
8 Replies
do i need to create a new interface in the 'next-auth' module inside auth.ts?
nextauth only populates the 4 default models
what you can do is on signin event check if its a new user
if it is then you can call prisma and insert a new row with user data
it would be something like these
events: {
signIn: async (message) => {
if (message.isNewUser) {
await prisma.Gamedata.create({
data: {
id: message.user.id,
userId: message.user.id,
},
});
}
},
},
Would be done in a routers file or in like authOptions within auth.ts'
authoptions
I dont have a great grasp on how data flows yet trying to work through it
ahh okay I see I get autocomplete for an events option within authOptions. Will look in the docs to read about it
you can see more here https://next-auth.js.org/configuration/events
Events | NextAuth.js
Events are asynchronous functions that do not return a response, they are useful for audit logs / reporting or handling any other side-effects.
Okay got it working and another piece of the puzzle clicked into place for me. Thanks a lot
youre welcome