Next.js Discord

Discord Forum

I cannot found "Get, Post" in Auth handlers

Answered
Atlantic mackerel posted this in #help-forum
Open in Discord
Atlantic mackerelOP
hey guys, im trying to add auth on my website. so I create my authOption and retrieve GET,POST methods from NextAuth but cannot found

I tried this

import NextAuth from "next-auth"
import GithubProvider from "next-auth/providers/github"
import Google from "next-auth/providers/google"

export const authOptions = {
    providers: [
        GithubProvider({
            clientId: process.env.GITHUB_CLIENT_ID as string,
            clientSecret: process.env.GITHUB_CLIENT_PASSWORD as string,
        }),
        Google({
            clientId: process.env.GOOGLE_CLIENT_ID as string,
            clientSecret: process.env.GOOGLE_CLIENT_PASSWORD as string
        })
    ],
    pages: {
        signIn: "/auth/signIn"
    }
}

export const handlers = NextAuth(authOptions)


export const {GET, POST} = handlers
Answered by Ray
and change the export to this
export { handlers as GET, handlers as POST }
View full answer

68 Replies

@Ray export the `GET` and `POST` from `app/api/auth/[...nextauth]/route.ts`
Atlantic mackerelOP
I just did this but it shouldn't be on this file?
const handler = NextAuth(authOptions)
and authOptions
just export const { GET, POST} = handlers
@Ray just `export const { GET, POST} = handlers`
Atlantic mackerelOP
like this?

export const authOptions = {
    providers: [
        GithubProvider({
            clientId: process.env.GITHUB_CLIENT_ID as string,
            clientSecret: process.env.GITHUB_CLIENT_PASSWORD as string,
        }),
        Google({
            clientId: process.env.GOOGLE_CLIENT_ID as string,
            clientSecret: process.env.GOOGLE_CLIENT_PASSWORD as string
        })
    ],
    pages: {
        signIn: "/auth/signIn"
    }
}

const handler = NextAuth(authOptions)
export const {GET, POST} = handler
@Ray yes
Atlantic mackerelOP
Thank youu and just use signIn function on my button?
app/api/auth/[...nextauth]/route.ts
@Ray app/api/auth/[...nextauth]/route.ts
Atlantic mackerelOP
ahhhh
@Ray app/api/auth/[...nextauth]/route.ts
Atlantic mackerelOP
but when i tried withsignIn function just redirect to error page like this
http://localhost:8080/api/auth/error
@Ray your server running on port 8080?
Atlantic mackerelOP
yeah
does it work if you go to /api/auth/signin?
Atlantic mackerelOP
@Ray does it work if you go to `/api/auth/signin`?
Atlantic mackerelOP
no doens't work the page
what do you see
Atlantic mackerelOP
I just change url on chrome
@Atlantic mackerel Click to see attachment
how does your folder structure look like now?
Atlantic mackerelOP
Like this
you didn't change the path yet
app/api/auth/[...nextauth]/route.ts
@Ray app/api/auth/[...nextauth]/route.ts
Atlantic mackerelOP
ahhhh I have to change route in auth folder
yes
and change the export to this
export { handlers as GET, handlers as POST }
Answer
@Ray and change the export to this `export { handlers as GET, handlers as POST }`
wait, are you using next-auth v4 or v5?
@Ray wait, are you using next-auth v4 or v5?
Atlantic mackerelOP
how can I check it?
package.json
@Ray wait, are you using next-auth v4 or v5?
Atlantic mackerelOP
opps I use ^4.24.5
Should I upgrade it?
@Ray then use this export
Atlantic mackerelOP
Ahh just like this?

const handlers = NextAuth(authOptions)
export { handlers as GET, handlers as POST  }
yes
Atlantic mackerelOP
The error has changed hahaha
@Atlantic mackerel Click to see attachment
you go to this url?
or you press the sign in
@Ray or you press the sign in
Atlantic mackerelOP
just pressed the signin button
and my signIn button just look like this


"use client"
import Image from "next/image";
import GoogleLightButton from "@/public/google/web_dark_sq_SI.svg";

import {signIn} from "next-auth/react";


const SigninButton = async () => {
    return (
        <div className="hover:cursor-pointer">
            <button onClick={(event) => {
                signIn("")
                console.log(process.env.GITHUB_CLIENT_ID)
            }}>
                <Image
                    priority
                    src={GoogleLightButton}
                    alt="Follow us on Twitter"
                />
            </button>

        </div>
    )
}

export default SigninButton
Atlantic mackerelOP
Ahhh
I think for now it just kinda like setup my google app from console
@Atlantic mackerel Ahhh
add http://localhost:3000 to the callback url on google
Atlantic mackerelOP
@Ray add http://localhost:3000 to the callback url on google
Atlantic mackerelOP
just put on this http://localhost:3000/ url?
Atlantic mackerelOP
But the error still same
restart the server and try again
and make sure you have correct client id and client secret
Atlantic mackerelOP
I just kinda werid 'cause I have already put my secret and url
export const authOptions = {
    providers: [
        GithubProvider({
            clientId: process.env.GITHUB_CLIENT_ID as string,
            clientSecret: process.env.GITHUB_CLIENT_PASSWORD as string,
        }),
        Google({
            clientId: process.env.GOOGLE_CLIENT_ID as string,
            clientSecret: process.env.GOOGLE_CLIENT_PASSWORD as string
        })
    ],
    pages: {
        signIn: "app/api/auth/[...nextauth]/route.ts"
    }
}
pages: {
        signIn: "/login"
    }
and set NEXTAUTH_SECRET and NEXTAUTH_URL
Atlantic mackerelOP
I set my keys on .env.local
NEXTAUTH_URL too
http://localhost:8080
@Ray NEXTAUTH_URL too
Atlantic mackerelOP
andd where to put on?
your .env.local
Atlantic mackerelOP
ahhh wow I can select my account
but when I select my account
Just seem like t his
@Atlantic mackerel but when I select my account Just seem like t his
go to oauth consent screen on google
either add your acc to test user or publish the app
@Ray go to oauth consent screen on google
Atlantic mackerelOP
ahhh got it you made my day!!!