Implementing login with Prisma
Below is the source code of my api but I figure out that any one entering credentials when an entered email resemble that of the one in the database, the user is authenticated without checking the corresponding password, how can I fix this, as the stored password is hashed?
12 Replies
You hash the input and compare it with your hash in your database. Modern hash algorithms come with special functions to compare the hash to a value.
Reason being that configuration data and the salt is stored with the hash.
I tried but didn't work for me as bycrpt produces different values
How did you compare?
compare takes the plaintext password as an input, not a hash. You compare the input to the hash stored in the database.
Btw. probably not the best idea to implement authentication yourself.
I compared the hash
As the user input is hashed then the hashed value is compared with the hashed Password in the database
Then what is the best idea as I want to implement authentication which can be used in both platforms
Yep, that's the mistake. Check the docs for bcrypt.compare. Should be self-explanatory.
Thanks let me check it
Use a service (Clerk, Firebase, Auth0, ...) or a library (NextAuth, Lucia, ...)
Nothing wrong with implementing it yourself to understand it. But the deeper you go the more complex it gets.
Next-auth only accessible in Nextjs
I once used it but when tried consuming the API in for login in another frameworks it didn't work out
NextAuth only offers a library for Next.js, true. Services usually offer libraries for multiple languages.
@xchristoph the comparison works thanks