Send additional parameters on SignIn() with GoogleProvider #4094
-
I have a special use case for my login. Quick run down:
Because of the fact that i have 2 forms and 2 roles, I want to send the user's role (depending on which form the user sent) together with the SignIn() request.
and all these properties appear in the authorize callback inside CredentialsProvider. ✔ But now I want to do the same thing with GoogleProvider. In the docs I found only signIn("google"), but i tried it like credentials just to see. Which looks something like this:
Although it works like this, no matter what I send, somewhere along the way these additional properties (userType) get lost.... And I can't access them inside [..nextauth].ts. I also tried to override the profile but still can't access these additional parameters from request like with CredentialsProvider. Anyone has some idea how I could do this? 🤯 |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 30 replies
-
UPDATE I found the answer, in case anyone encounters this as well. As per the docs Next-Auth Additional Parameters I added my parameter inside Google's authorization parameter, like so:
The problem was that the additional parameters don't arrive in the signIn callback, where I run my logic, and I didn't feel like creating my own file for Google's callback. For a reminder I send them like so:
Because of that I tried using the OG request before it enters the Google OAuth cycle, and save that parameter into a global variable, like so:
And it works! 🤯 However since this is triggered on each request sent between Next-auth and Google, we need to check if the userType parameter is not undefined, so we don't overwrite it. After that I can use it in any callback like this:
|
Beta Was this translation helpful? Give feedback.
-
setting a cookie to delete later in the process is really a weird way of doing this. Since next-auth provides a way to send these params to the api backend there should be a much simpler way to retrieve them. |
Beta Was this translation helpful? Give feedback.
-
There really should be a better way to do this than cookies. I use the EmailProvider so it's not uncommon for mobile users to start account creation on one browser and then click the login link on their email which opens an entirely different browser, which breaks the cookies hack suggested by @VictorKolb. |
Beta Was this translation helpful? Give feedback.
-
Anyone have updates to share here? Found myself down this rabbit hole last night and concluded cookies are still the only solution. Is there a technical reason why the query parameters aren't available in the callback? It already seems weird enough that we can't pass anything to the |
Beta Was this translation helpful? Give feedback.
-
One more solution, just get the data from request by add the specific params to the
In
|
Beta Was this translation helpful? Give feedback.
-
I had the same issue. I was actually retrieving the user from the db to pass some more information to the session and could not figure out a way to do that. My solution was actually to make an api call inside the JWT callback. I am setting the token with the information that I need and passing it to the session: ` async jwt({ token, account, user, trigger, session }) {
Hope it helps someone. |
Beta Was this translation helpful? Give feedback.
-
This isn't pretty but it seems to work. Client: Server: [...nextauth].js
|
Beta Was this translation helpful? Give feedback.
-
still not proper solution guys ? |
Beta Was this translation helpful? Give feedback.
-
This method is working fine for me . file : [...nextauth/route.tsx] async function auth(req: NextApiRequest, res: NextApiResponse) {
`}`` use the function to send the params :
|
Beta Was this translation helpful? Give feedback.
-
For Next AppRouter we can do something like this // app/api/auth/[...nextauth]/route.ts
import { getNextAuth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
async function customHandler(req: NextRequest, res: NextResponse) {
if (!req.method) return;
const { handlers }: { handlers: { [key: string]: any } } = getNextAuth(req);
const response = await handlers[req.method](req, res);
return response;
}
export { customHandler as GET, customHandler as POST }; and // auth.ts
import NextAuth from "next-auth";
import { NextRequest } from "next/server";
export function getNextAuth(req: NextRequest): {
handlers: { [key: string]: any };
} {
return NextAuth({ ... })
} |
Beta Was this translation helpful? Give feedback.
UPDATE
I found the answer, in case anyone encounters this as well.
As per the docs Next-Auth Additional Parameters I added my parameter inside Google's authorization parameter, like so:
The problem was that the additional parameters don't arrive in the signIn callback, where I run my logic, and I didn't feel like creating my own file for…