Can't make onAfterLogin work

Hello, I updated wasp because I need this specific hook in my project but I really can't make it work. Here is my app/src/auth/hooks.ts file:
import type {
OnAfterLoginHook
} from 'wasp/server/auth';

export const onAfterLogin: OnAfterLoginHook = async ({
providerId,
user,
oauth,
prisma,
req,
}) => {
console.log('test');

try {
await prisma.logs.create({
data: {
message: 'test',
level: 'test onAfterLogin',
},
});
} catch (error) {
console.error('Error updating Log.', error);
}

try {
await prisma.user.update({
where: {
id: user.id,
},
data: {
username: 'vdssd'
}
});
} catch (error) {
console.error('Error updating user.', error);
}
};
import type {
OnAfterLoginHook
} from 'wasp/server/auth';

export const onAfterLogin: OnAfterLoginHook = async ({
providerId,
user,
oauth,
prisma,
req,
}) => {
console.log('test');

try {
await prisma.logs.create({
data: {
message: 'test',
level: 'test onAfterLogin',
},
});
} catch (error) {
console.error('Error updating Log.', error);
}

try {
await prisma.user.update({
where: {
id: user.id,
},
data: {
username: 'vdssd'
}
});
} catch (error) {
console.error('Error updating user.', error);
}
};
Nothing appears in the console and the database is not updated. Could you please help me?
4 Replies
kapa.ai
kapa.ai2w ago
Hi @Hastur. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry for the confusion, but based on the provided knowledge sources, there is no direct information about the OnAfterLoginHook or how to use it in Wasp. However, there is a mention of a feature request for hooks after login or sign-in to execute custom server code in Github Issue #1556 (https://github.com/wasp-lang/wasp/issues/1556). This suggests that this feature might not be fully implemented or documented yet. For debugging purposes, you might want to check if the onAfterLogin function is being called at all. You can do this by adding more logging statements at the beginning of the function. If you're still having trouble, I would recommend reaching out to the Wasp community or the maintainers directly, possibly through their Github Issues (https://github.com/wasp-lang/wasp/issues) or Discord (https://discord.com/channels/686873244791210014/1215945587841831033) for more specific guidance. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Hastur, 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?
Hastur
Hastur2w ago
I precise that I also have a onAfterSignup function in this file that is working perfectly.
YuehLin
YuehLin2w ago
Did you add the onAfterLogin in main.wasp? You will need to add it in the auth section
Hastur
Hastur2w ago
Oh my. I can't believe I didn't add it to the main file. I forgot I added it for the onAfterSignup and just looked at the "API Reference" sections of the doc to see what I did wrong but not the "Using hooks" parts. Anyway, thank you for your help!
Want results from more Discord servers?
Add your server