How to run a function inside async session only when its GoogleProvider and not if its other provide
Unanswered
Brown bear posted this in #help-forum
Brown bearOP
This is my nextauth file :
const handler = NextAuth({
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID?? '',
clientSecret: process.env.GOOGLE_CLIENT_SECRET?? ''
}),
CredentialsProvider({
name : "Credentials",
async authorize(credentials, req){
try {
const response = await axios.post(
process.env.API_ROUTE,
{ email: credentials.email, password: credentials.password, apiKey: process.env.API_KEY },
{
headers: {
"Content-Type": "application/json",
},
}
);
const userData = response.data;
return { success: true };
} catch (error) {
console.error("Login failed:", error);
return { success: false };
}
}
})
],
callbacks: {
async jwt({token, account}) {
if (account) {
token = Object.assign({}, token, { access_token: account.access_token, id_token: account.id_token, refresh_token: account.refresh_token });
}
return token
},
async session({session, token}) {
if(session) {
session = Object.assign({}, session, {access_token: token.access_token, id_token: token.id_token})
try {
const apiKey = process.env.API_KEY
const response = await axios.post(
process.env.GOOGLE_API,
{ authorizationCode: token.id_token, apiKey: apiKey },
{
headers: {
"Content-Type": "application/json",
},
}
);
const userData = response.data;
} catch (error) {
console.error("Failed:", error);
}
}
return session
},
}
})
export {handler as GET, handler as POST};
```
Now the code inside the session callback where I am making a post request is something that I only want to do when logging in with google and not with credentials. So how can I do that?
const handler = NextAuth({
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID?? '',
clientSecret: process.env.GOOGLE_CLIENT_SECRET?? ''
}),
CredentialsProvider({
name : "Credentials",
async authorize(credentials, req){
try {
const response = await axios.post(
process.env.API_ROUTE,
{ email: credentials.email, password: credentials.password, apiKey: process.env.API_KEY },
{
headers: {
"Content-Type": "application/json",
},
}
);
const userData = response.data;
return { success: true };
} catch (error) {
console.error("Login failed:", error);
return { success: false };
}
}
})
],
callbacks: {
async jwt({token, account}) {
if (account) {
token = Object.assign({}, token, { access_token: account.access_token, id_token: account.id_token, refresh_token: account.refresh_token });
}
return token
},
async session({session, token}) {
if(session) {
session = Object.assign({}, session, {access_token: token.access_token, id_token: token.id_token})
try {
const apiKey = process.env.API_KEY
const response = await axios.post(
process.env.GOOGLE_API,
{ authorizationCode: token.id_token, apiKey: apiKey },
{
headers: {
"Content-Type": "application/json",
},
}
);
const userData = response.data;
} catch (error) {
console.error("Failed:", error);
}
}
return session
},
}
})
export {handler as GET, handler as POST};
```
Now the code inside the session callback where I am making a post request is something that I only want to do when logging in with google and not with credentials. So how can I do that?