NextAuth Callbacks

I'm currently trying to implement my nextauth instance in an ios app, but im having some trouble implementing callbacks that pass the session id and user id/token back to the app. I'm currently just using the credentials authOption, but if anyone could point me in the right direction that would be great. Thanks!
export const authOptions: NextAuthOptions = {
callbacks: {
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id;
token.email = user.email;
}

return token;
},
session: ({ session, token, user }) => ({
...session,
user: {
...session.user,
id: token.id || user.id,
},
}),
},
adapter: PrismaAdapter(db) as Adapter,
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: {
label: "Username",
type: "text",
placeholder: "[email protected]",
},
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
if (!credentials?.email || !credentials?.password) return null;

const user = await db.user.findUnique({
where: {
email: credentials.email.toLowerCase(),
},
});

if (!user?.passwordHash) return null;

const passwordsMatch = await bcrypt.compare(
credentials.password,
user.passwordHash,
);

if (!passwordsMatch) return null;

return user;
},
}),
],
};
export const authOptions: NextAuthOptions = {
callbacks: {
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id;
token.email = user.email;
}

return token;
},
session: ({ session, token, user }) => ({
...session,
user: {
...session.user,
id: token.id || user.id,
},
}),
},
adapter: PrismaAdapter(db) as Adapter,
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: {
label: "Username",
type: "text",
placeholder: "[email protected]",
},
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
if (!credentials?.email || !credentials?.password) return null;

const user = await db.user.findUnique({
where: {
email: credentials.email.toLowerCase(),
},
});

if (!user?.passwordHash) return null;

const passwordsMatch = await bcrypt.compare(
credentials.password,
user.passwordHash,
);

if (!passwordsMatch) return null;

return user;
},
}),
],
};
I haven't changed much from the base t3 stack app besides adding the passwordHash functionalities.
4 Replies
Zefty
Zefty5mo ago
Hey, i've done something very similar to you for my app I noticed you have db adapter so you may have to use database strategy for the session instead of jwt Then in your session callback you can add a primsa call to db to fetch tokens and etc
session: async ({ session, user }) => {
let acc = await db.query.accounts.findFirst({
where: eq(accounts.userId, user.id),
});

return {
...session,
user: {
...session.user,
id: user.id,
},
accessToken: acc?.access_token,
};
}
session: async ({ session, user }) => {
let acc = await db.query.accounts.findFirst({
where: eq(accounts.userId, user.id),
});

return {
...session,
user: {
...session.user,
id: user.id,
},
accessToken: acc?.access_token,
};
}
Something like this (i'm using drizzle) ^
Helix
HelixOP4mo ago
I have added a accessToken return to the session callback method but I am not seeing any response on the iOS app
Helix
HelixOP4mo ago
Gist
ContentView.swift
GitHub Gist: instantly share code, notes, and snippets.
Helix
HelixOP4mo ago
I’ve updated my callback url to the correct url and it’s returning the scheme and host but not the token
Want results from more Discord servers?
Add your server