-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
signIn callback returning before promise resolves #897
Comments
Hi there! Since you are using Promises inside signIn: async (user, account, profile) => {
await mongoose.connect(process.env.MONGODB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
const userExists = Boolean(await userModel.findOne({ email: user.email }).exec())
if (!userExists) {
const urlName = user.name.split(" ").join("-") + "-" + short.generate();
await userModel.create({
email: user.email,
name: user.name,
image: user.image,
urlName: urlName,
private: false,
})
}
return true
} Some explanation. I am not entirely sure about the try {
//do some async/await work here
return true // don't forget to return true at the end
} catch(error) {
if(errorIWantToHandle(error)) {
throw "/signin-error"
}
throw error
} (@iaincollins I think the API here could be improved: next-auth/src/server/routes/signin.js Line 62 in 5126f4e
returning a string would probably enough, instead of throwing.) I advocate for using async/await over Promises and callbacks because you will quickly end up with a lot of nested calls, which makes it much harder to read the code and accidentally forget to add a missing return. Also with @wwsalmon, please also note that I THINK (@iaincollins have to back me up here)
The source code suggests that The user marked the docs as incomplete, which I have to agree with because of the above mentioned mismatch. |
@balazsorban44 changing everything to async/await instead of callbacks and returning booleans instead of promises fixed my problem, thanks so much! re: documentation, I'm a fairly new frontend dev who hasn't worked all that much with async/await and promise patterns before, so maybe a more experienced backend dev wouldn't have run into this problem. For me, though, seeing |
Your question
I use the
signIn
callback to access MongoDB before letting the user through. My code looks like this:However, the callback returns true and redirects to the homepage before the promise is even returned. As a minimal reproduction:
does not stop the user, but rather lets them through. Wrapping everything in a promise doesn't work either:
This request just returns a pending promise and never resolves.
How do I make the callback return, and the page redirect, only after the function has fun and the promise has resolved?
Here's my whole project repo for further context: https://github.com/wwsalmon/updately
Feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
The text was updated successfully, but these errors were encountered: