Server-Side Kotlin with Ktor · Sessions Part 2 | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2885892-server-side-kotlin-with-ktor/lessons/22

Why do you check db.user(userId) != null both before and after trying to create the db record? Wouldn’t the first check (where you’re checking username/password validity suffice?

@macsimus Can you please help with this when you get a chance? Thank you - much appreciated! :]

Thanks for the question @rajohns. The first one is for when the user is filling out the screen, checking on whether that username already exists and redirecting back to the signup screen if it does.

The second one handles an edge case where a different user created a user with the same username after the first user hit submit on the signup form, in which case the createUser call would fail in the try block and cause an exception. It’s an unlikely edge case for sure, but good to handle in any case. That would show an error to the user and direct them to choose a new username. Thanks again!

1 Like

@macsimus In the sign up “post” you checked whether the session already exists, but in the sign in “post” you didn’t. What was the reason?

@macsimus Do you have any feedback about this? Thank you - much appreciated! :]