I am using next-auth in nextjs for my authentication. After authentication with google provider, I am getting only a refresh and I need to manually enter the address I was navigated from to access it after authentication. What am I doing wrong?
// srcappapiauth[...nextauth]route.ts
...
export const authConfig: NextAuthOptions = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
authorization: {
params: {
redirect_uri: 'http://localhost:3000/api/auth/callback/google'
}
}
}),
GitHubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string,
profile: (profile) => {
return {
id: profile.id.toString(),
name: profile.login ?? profile.name,
email: profile.email,
image: profile.avatar_url,
}
},
}),
],
callbacks: {
session({ session, token }) {
if (session.user) {
session.user.id = token.sub || "";
}
return session;
}
},
pages: {
signIn: "/home/auth",
error: "/home/error",
},
}
const handler = NextAuth(authConfig)
export { handler as GET, handler as POST }
// srcmiddleware.ts
import { withAuth } from "next-auth/middleware"
export default withAuth({
// Matches the pages config in `[...nextauth]`
pages: {
signIn: "/home/auth",
error: "/home/error",
},
})
export const config = { matcher: ["/home/form/:path*", "/home/dashboard/:path*"] }
The url looks something like this http://localhost:3000/home/auth?callbackUrl=%2Fhome%2Fform
. I am assuming after authentication the callback should automatically redirect to /home/form
as shown in the web browser url.
Update
The signin function is import from next-auth
'use client'
import { signIn } from "next-auth/react";
...
const handleClick = (providerName: string) => {
signIn(providerName);
}
...
<button onClick={() => handleClick("google")}>
...
<FcGoogle className="absolute left-0 w-5" />
...
</button>
1
I believe you have the GoogleProvider wrong. That authorization property is wrongly setted.
What you should do is delete the authorization property and use the callbackUrl on the signIn component
signIn('google', { callbackUrl: 'http://localhost:3000/bar' })
As you can see from the Docs