Custom Session Google Provider on Next Auth
Unanswered
Briard posted this in #help-forum
BriardOP
I have a code in the below in NextAuth 5.
And I have 2 method for authentication.
In my confugiration, I will send the user data and save on the database. Also, I have the custom session to add user infomasion on database like database id, etc..
Problem:
the custom session only work on credential method
And on google method, i cant do custom session on my configuration. And always have a default google response like:
{
user: {
name: 'google name',
email: 'email@gmail.com',
image: ''link photo",
id: 'id'
},
expires: '2024-10-21T15:44:17.321Z'
}
question:
How to make custom session for Google Provider
Code:
callbacks: {
async signIn({ user, account }) {
if (account?.provider === "google") {
let dbUser = await prisma.users.findUnique({
where: { email: user.email as string },
});
if (!dbUser) {
await prisma.users.create({
data: {
email: user.email as string,
nama: user.name as string,
provider: "google",
provider_id: account.providerAccountId as string,
image: user.image as string,
status: true,
},
});
}
}
return true;
},
async jwt({ token, user, account }) {
if (account) {
token.id = user.id as string;
token.email = user.email as string;
token.nama = user.nama;
token.alamat = user.alamat;
token.status = user.status;
}
return token;
},
async session({ session, token }) {
if (token) {
session.user.id = token.id;
session.user.email = token.email;
session.user.nama = token.nama;
session.user.alamat = token.alamat;
session.user.status = token.status;
session.user.createdAt = token.createdAt;
session.user.updatedAt = token.updatedAt;
session.user.tes = token.tes
}
return session;
},
},
});
And I have 2 method for authentication.
In my confugiration, I will send the user data and save on the database. Also, I have the custom session to add user infomasion on database like database id, etc..
Problem:
the custom session only work on credential method
And on google method, i cant do custom session on my configuration. And always have a default google response like:
{
user: {
name: 'google name',
email: 'email@gmail.com',
image: ''link photo",
id: 'id'
},
expires: '2024-10-21T15:44:17.321Z'
}
question:
How to make custom session for Google Provider
Code:
callbacks: {
async signIn({ user, account }) {
if (account?.provider === "google") {
let dbUser = await prisma.users.findUnique({
where: { email: user.email as string },
});
if (!dbUser) {
await prisma.users.create({
data: {
email: user.email as string,
nama: user.name as string,
provider: "google",
provider_id: account.providerAccountId as string,
image: user.image as string,
status: true,
},
});
}
}
return true;
},
async jwt({ token, user, account }) {
if (account) {
token.id = user.id as string;
token.email = user.email as string;
token.nama = user.nama;
token.alamat = user.alamat;
token.status = user.status;
}
return token;
},
async session({ session, token }) {
if (token) {
session.user.id = token.id;
session.user.email = token.email;
session.user.nama = token.nama;
session.user.alamat = token.alamat;
session.user.status = token.status;
session.user.createdAt = token.createdAt;
session.user.updatedAt = token.updatedAt;
session.user.tes = token.tes
}
return session;
},
},
});
2 Replies
Chinese Alligator
In jwt callback you need to check user not account.
This is my example with credentials and Google auth running https://github.com/ezeparziale/quark/blob/main/src/auth.config.ts