Next-auth: Cannot POST /api/auth/signout on signout() call

Created on 1 Jul 2020  路  3Comments  路  Source: nextauthjs/next-auth

I'm playing around with next-auth using Google authentication. I'm following the official example but now facing some issues with signout() method.

  1. I'm successfully singing up with Google provider.
// pages/login.ts

const LoginPage = () => {
  const [session, loading] = useSession();
  return session ? (
    <button onClick={signout}>Sign out</button>
  ) : (
    <button onClick={() => signin('google')}>Sign in with Google</button>
  );
};

  1. Then trying to sign out after redirecting to http://localhost:3000/api/auth/signout and submitting Sign Out button I'm getting the following error: Cannot POST /api/auth/signout:

Screenshot from 2020-07-01 15-05-34

My [...nextauth].ts file:

// pages/api/auth/[...nextauth].ts

import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';

const options = {
  site: 'http://localhost:3000',
  providers: [
    Providers.Google({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
  ],
};

export default (req, res) => NextAuth(req, res, options);

Documentation feedback

  • [x] Found the documentation helpful
  • [x] Found documentation but was incomplete
  • [ ] Could not find relevant documentation
  • [x] Found the example project helpful
  • [ ] Did not find the example project helpful
question

Most helpful comment

@loonskai , It appears to be an error when the express server does not have a handler for processing your post request. Are you using expressjs ?
If yes, then check if your catch all route is like
server.all('*', (req, res) => {
return handle(req, res)
})

not like
server.get('', (req, res) => {
return handle(req, res)
})
You can also add handlers for http methods you want instead of adding it for all
like
server.get('
', (req, res) => {
return handle(req, res)
})
server.post('*', (req, res) => {
return handle(req, res)
})

All 3 comments

I think this message may be coming from another service or a proxy - I sometimes run into something similar when I have left something else running I've forgotten about. This isn't a message that comes from NextAuth.js.

The only error that is similar in NextAuth.js is Error: HTTP POST is not supported.

@loonskai , It appears to be an error when the express server does not have a handler for processing your post request. Are you using expressjs ?
If yes, then check if your catch all route is like
server.all('*', (req, res) => {
return handle(req, res)
})

not like
server.get('', (req, res) => {
return handle(req, res)
})
You can also add handlers for http methods you want instead of adding it for all
like
server.get('
', (req, res) => {
return handle(req, res)
})
server.post('*', (req, res) => {
return handle(req, res)
})

@ranjithvikram Cheers! I would have spent some time digging into the cause if you wouldn't post the reply.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bscaspar picture bscaspar  路  3Comments

alex-cory picture alex-cory  路  3Comments

iaincollins picture iaincollins  路  3Comments

dmi3y picture dmi3y  路  3Comments

SharadKumar picture SharadKumar  路  3Comments