I'm playing around with next-auth using Google authentication. I'm following the official example but now facing some issues with signout() method.
// 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>
);
};
http://localhost:3000/api/auth/signout and submitting Sign Out button I'm getting the following error: Cannot POST /api/auth/signout:
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
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.
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)
})