Closed ats1999 closed 3 years ago
I’m getting the same issue on my application when pairing with mongodb. I’ve opened up the IP access, by adding 0.0.0.0/0 but it doesn’t seem to work. Returns 502 after a 10 second timeout.
Could you please provide some source code? (Ideally a link to a reproduction repository)
I have same issue with mail auth too. How should I set database url
prod url looks like this and not working and getting below error
database: process.env.DATABASE_URL
DATABASE_URL=mongodb+srv://dba:*********@cluster0-yaruk.mongodb.net/nextauth
Error: 502: BAD_GATEWAY Code: NO_RESPONSE_FROM_FUNCTION
PS: My mongo db has access from anywhere 0.0.0.0/0
Could you please provide some source code? (Ideally a link to a reproduction repository)
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
// For more information on each option (and a full list of options) go to
// https://next-auth.js.org/configuration/options
const options = {
// https://next-auth.js.org/configuration/providers
providers: [
Providers.GitHub({
clientId: "13333223131.........",
clientSecret: "13132133........."
}),
Providers.Google({
clientId: "123123...........",
clientSecret: "123123.........."
})
],
// Database optional. MySQL, Maria DB, Postgres and MongoDB are supported.
// https://next-auth.js.org/configuration/database
//
// Notes:
// * You must to install an appropriate node_module for your database
// * The Email provider requires a database (OAuth providers do not)
database: process.env.MONGO_URI,
// The secret should be set to a reasonably long random string.
// It is used to sign cookies and to sign and encrypt JSON Web Tokens, unless
// a seperate secret is defined explicitly for encrypting the JWT.
secret: process.env.SECRET,
session: {
// Use JSON Web Tokens for session instead of database sessions.
// This option can be used with or without a database for users/accounts.
// Note: `jwt` is automatically set to `true` if no database is specified.
jwt: true,
// Seconds - How long until an idle session expires and is no longer valid.
// maxAge: 30 * 24 * 60 * 60, // 30 days
// Seconds - Throttle how frequently to write to database to extend a session.
// Use it to limit write operations. Set to 0 to always update the database.
// Note: This option is ignored if using JSON Web Tokens
// updateAge: 24 * 60 * 60, // 24 hours
},
// JSON Web tokens are only used for sessions if the `jwt: true` session
// option is set - or by default if no database is specified.
// https://next-auth.js.org/configuration/options#jwt
jwt: {
// A secret to use for key generation (you should set this explicitly)
// secret: 'INp8IvdIyeMcoGAgFGoA61DdBglwwSqnXJZkgz8PSnw',
// Set to true to use encryption (default: false)
// encryption: true,
// You can define your own encode/decode functions for signing and encryption
// if you want to override the default behaviour.
// encode: async ({ secret, token, maxAge }) => {},
// decode: async ({ secret, token, maxAge }) => {},
},
// You can define custom pages to override the built-in pages.
// The routes shown here are the default URLs that will be used when a custom
// pages is not specified for that route.
// https://next-auth.js.org/configuration/pages
pages: {
// signIn: '/api/auth/signin', // Displays signin buttons
// signOut: '/api/auth/signout', // Displays form with sign out button
// error: '/api/auth/error', // Error code passed in query string as ?error=
// verifyRequest: '/api/auth/verify-request', // Used for check email page
// newUser: null // If set, new users will be directed here on first sign in
},
// Callbacks are asynchronous functions you can use to control what happens
// when an action is performed.
// https://next-auth.js.org/configuration/callbacks
callbacks: {
// signIn: async (user, account, profile) => { return Promise.resolve(true) },
// redirect: async (url, baseUrl) => { return Promise.resolve(baseUrl) },
// session: async (session, user) => { return Promise.resolve(session) },
// jwt: async (token, user, account, profile, isNewUser) => { return Promise.resolve(token) }
},
// Events are useful for logging
// https://next-auth.js.org/configuration/events
events: { },
// Enable debug messages in the console if you are having problems
debug: false,
}
export default (req, res) => NextAuth(req, res, options)
This time, i directly pasted my id and secret into code, It is still not working.
But, the strange is that it is working fine on localhost when i provide the http://localhost:3000
as url.
import {signIn} from "next-auth/client";
// uses
<a href="/api/auth/signin"
onClick={(e)=>{
e.preventDefault();
signIn();
}}
>Oauth</a>
You can see this link in the footer
The authorization callback URL should be
https://bdevg.com/api/auth/callback/github
See: https://next-auth.js.org/getting-started/rest-api#get-apiauthcallbackprovider
I found the solution like
1) npm install typeorm --save 2) npm install reflect-metadata --save 3) npm i typeorm@0.2.28 4)// _app.ts or _app.js import "reflect-metadata"; ...
I found the solution like
- npm install typeorm --save
- npm install reflect-metadata --save
- npm i typeorm@0.2.28 4)// _app.ts or _app.js import "reflect-metadata"; ...
What is this?
I found the solution like
- npm install typeorm --save
- npm install reflect-metadata --save
- npm i typeorm@0.2.28 4)// _app.ts or _app.js import "reflect-metadata"; ...
What is this?
If you have issue on production but localhost. this should work for you. Do not forget to add import _app.js then try. It should work.
502 bad gateway NO_RESPONSE_FROM_FUNCTION suggests that the provider does not get an answer from your application. Possibly because you configured the callback url wrong. The handler callback that will respond to the provider is at
/api/auth/callback/:provider
Please see my previous answer.
I changed Authorization callback URL in my GitHub developer setting to https://bdevg.com/api/auth/callback/github
, but still I'm getting the same error.
Hmm, sorry 😕.
"Invalid state returned from oAuth provider"
Could you try setting state: false
?
https://next-auth.js.org/configuration/providers#oauth-provider-options
okey! I'm trying>
Could you try setting state: false?
This didn't worked, it takes me http://localhost:3000/api/auth/signin/github after clicking on signin with github or google
when i remove, this then it was working as before.
do you have a link to your repo?
do you have a link to your repo?
It's private
OKey: So, i forked your github repo, then i deployed it on https://next-auth-example-iota.vercel.app/
Repo: https://github.com/ats1999/next-auth-example/tree/main/pages
Homepage URL https://next-auth-example-iota.vercel.app Authorization callback URL https://next-auth-example-iota.vercel.app/api/auth/callback/github Now, tell me what should i edit.
This time, I haven't provided NEXTAUTH_URL , because it's looking like optional, https://next-auth.js.org/getting-started/rest-api#get-apiauthproviders
I have looked more into this issue.
I set up an AWS RDS database to test my application. Everything works correctly in production when doing this. So this means the problem must be to do with how NextAuth is connecting to MongoDB Atlas in production.
MongoDB and NextAuth are working correctly in local development.
I have the network access set to 0.0.0.0/0.
I have tried @bkalafat solution, but it still throws the 502 error.
I have also been able to connect to the database using the code found in a Vercel tutorial: https://vercel.com/guides/deploying-a-mongodb-powered-api-with-node-and-vercel
So I have to believe the issue is either with NextAuth or MongoDB Atlas is blocking the connection when the site runs on Vercel.
If anyone is able to provide a solution to this it would be greatly appreciated. Thanks.
Can you share your masked mongodb connection string
Set debug mode to true on your auth options. And be sure typeorm package version is 0.2.28
Set debug mode to true on your auth options. And be sure typeorm package version is 0.2.28
This doesn't logged anything on console.
In my case, mongodb atlas is correctly working,it is an working application. We can communicate with database, there is no db error.
But, still I'm not getting logged in using oauth.
If i was used normal oauth workflow not nextauth.js, then i would have made it earlier.
In my case, mongodb atlas is correctly working,it is an working application. We can communicate with database, there is no db error.
But, still I'm not getting logged in using oauth.
you should set dbname as /nextauth
nextauth or /nextauth?
nextauth or /nextauth?
Mine which is working like this for mongodb DATABASE_URL=mongodb+srv://d:**@cluster0-yaruk.mongodb.net/nextauth?retryWrites=true&w=majority
Did you tried DATABASE_URL=mongodb+srv://d:**@cluster0-yaruk.mongodb.net/anything?retryWrites=true&w=majority
Because my DB name is bdevg
no i am using another db for my application but you should use nextauth for next-auth db name on mongo. I guess its standart for them. Try to add new url path with this /nextauth?.. env file and use that on your next auth options
I Have two different path one is for my application another is for my next-auth
ok
and you dont have to create db at mongo. Next will generate it automatically
yeah
Now, i created the DATABASE_URL
as you said. There is debug mode is set to true. But, still, there is no improvement. Also, anything is not getting logged on the console.
GitHub repo https://github.com/ats1999/next-auth-example working URL https://next-auth-example-iota.vercel.app/
if debug: true, then at least it should log error on console.
See, my repo for more information.
i cannot see node modules which I advise you to add.
npm install typeorm --save npm install reflect-metadata --save npm i typeorm@0.2.28 4)// _app.ts or _app.js import "reflect-metadata";
npm install typeorm --save npm i typeorm@0.2.28
which version I have to install, latest or 0.2.28?
Unpacked Size of typeorm
is 17 MB
Why this large-sized module is required? It should not have these kinds of dependency, which force users to bring changes in the existing application. It also increases the bundle size of the application.
i cannot see node modules which I advise you to add.
npm install typeorm --save npm install reflect-metadata --save npm i typeorm@0.2.28 4)// _app.ts or _app.js import "reflect-metadata";
I update the repo as you advised, but no improvement. See https://next-auth-example-iota.vercel.app/
npm install typeorm --save npm i typeorm@0.2.28
which version I have to install, latest or 0.2.28?
0.2.28
I checked your repo. 0.2.29 is not working you should change it to .28
npm install typeorm --save npm i typeorm@0.2.28 which version I have to install, latest or 0.2.28?
0.2.28
I checked your repo. 0.2.29 is not working you should change it to .28
Still, it has the same issue 😂
Yes I have tried the many different fixes to no avail.
@bkalafat how do you have your MongoDB Atlas configured?
What cluster tier/region are you using?
I have my Network access set to.
Database user role set to Atlas admin.
Connection string works locally, but doesn't work when deployed on Vercel.
mongodb+srv://pr****:**@marquee.jiqul.mongodb.net/nextauth?retryWrites=true&w=majority
Thanks,
Ollie
string
Why don't you leave DB configuration on the user? It has already taken, so much time.
@balazsorban44 But, it's working here https://next-auth-example.now.sh/ how??
@iaincollins just reverted some changes on the example app, Iain maybe you could give an answer to this?
I am facing the same problem with google provider and github provider, only works in dev mode, tried many solutions, stuck for 2 days on this error..
I'm sure the problem come from the database in [...nextauth].tsx call.
I tried many configuration (all) from this https://github.com/nextauthjs/next-auth/issues/833
And the only solution who work for my for not having the MongoServerSelectionError: getaddrinfo ENOTFOUND error is to set database not like this :
database: process.env.DATABASE_URL,
but like this :
database: {
type: "mongodb",
uri: process.env.DATABASE_URL,
w: "majority",
useNewUrlParser: true,
useUnifiedTopology: true,
retryWrites: true,
},
(who the DTABASE_URL is mongodb+srv://[username]>:[password]@strapidbcluster.jdadp.mongodb.net/strapi-db)
My configuration for github auth (work perfectly with http://localhost:3000) :
My configuration for google auth (work perfectly with http://localhost:3000) :
The correct connexion to my backend server and mongo db (handle with strapi) in local dev
And in production, always after I tried google or github auth (after clicking on my account) i face the same result:
0 logs, only this message in my vercel function log:
I don't know what else to try 😭
My repo just in case : https://github.com/Flosrn/nextjs-headless-cms
I'm sure the problem come from the database in [...nextauth].tsx call.
I tried many configuration (all) from this https://github.com/nextauthjs/next-auth/issues/833
And the only solution who work for my for not having the MongoServerSelectionError: getaddrinfo ENOTFOUND error is to set database not like this :
database: process.env.DATABASE_URL,
but like this :
database: { type: "mongodb", uri: process.env.DATABASE_URL, w: "majority", useNewUrlParser: true, useUnifiedTopology: true, retryWrites: true, },
(who the DTABASE_URL is mongodb+srv://[username]>:[password]@strapidbcluster.jdadp.mongodb.net/strapi-db)
My configuration for github auth (work perfectly with http://localhost:3000) :
My configuration for google auth (work perfectly with http://localhost:3000) :
The correct connexion to my backend server and mongo db (handle with strapi) in local dev
And in production, always after I tried google or github auth (after clicking on my account) i face the same result:
0 logs, only this message in my vercel function log:
I don't know what else to try 😭
My repo just in case : https://github.com/Flosrn/nextjs-headless-cms
wrong db name
@Flosrn
I think I've finally got it working, try changing 'uri' to 'url'
In package.json add:
"resolutions": { "typeorm": "0.2.28" }
I've tested without the 'reflect-metadata' package and it also works.
Hope this helps everyone.
Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep it open. (Read more at #912) Thanks!
Hi there! It looks like this issue hasn't had any activity for a while. To keep things tidy, I am going to close this issue for now. If you think your issue is still relevant, just leave a comment and I will reopen it. (Read more at #912) Thanks!
I've the same problem on Azure. The problem is the NEXTAUTH variabile. I put in the env.production but the server don't read it. So, I solved to put the NEXTAUTH_URL into the Azure App service environment variables. It use the base domain without that variable.
502: BAD_GATEWAY While doing GitHub OAuth, i got this.
Reproduction visit https://bdevg.com/api/auth/signin then choose a sign with Github
Vercel function logs