-
For next-auth discord integration, I found these options to integrate next-auth into an existing database. The first option seems to be the easiest and it's what Lihas and I thought of in first attempt: Configure the OAuth services you want to use, but don't configure a database (which will enable JSON Web Tokens) and use a custom It's possible but I'm not sure how to get the
return prisma.user.update({
where: {
id: userId
},
data: {
discordRefreshToken: refreshToken,
discordAccessToken: accessToken,
discordAccessTokenExpires: accessTokenExpiresAt
}
}) It seems it's getting it from the
return new Promise(resolve => {
const middlewares = [loggingMiddleware, sessionMiddleware(), userMiddleware]
runMiddlewares(middlewares, req, res, async () => {
if (!req.user?.id) {
return resolve({
props: {
errorCode: ErrorCode.USER_NOT_LOGGED_IN
}
})
}
try {
const { code } = query || {}
const user = await setTokenFromAuthCode(req.user.id, code as string)
const userInfo = await getDiscordUserInfo(user)
...
resolve({ props: { userInfo, username: req.user.username } })
}
...
})
}) Potential solution:
Here's where the callbacks: {
async signIn({ account }) {
const { provider, refresh_token, access_token, expires_at } = account
const accessTokenExpiresAt = new Date(
Date.now() + (expires_at * 1000 || 0)
)
if (provider === 'discord') {
await prisma.user.update({
where: {
id: userId // Missing
},
data: {
discordRefreshToken: refresh_token,
discordAccessToken: access_token,
discordAccessTokenExpires: accessTokenExpiresAt
}
})
}
return true
}
}, The The 3rd option seems to be the best as the above is just a workaround that we'll have to deal with in the future when further integrations of other providers are required or needed. Create a new set of tables and import your existing data into them. This is a lot easier and more robust than trying to update an existing table, as if things like timestamp fields are declared slightly differently you will likely run into problems at some point. You can use the entityPrefix option if you want NextAuth.js to create the tables in the same database without the table names clashing with existing tables, the option is documented here. You only need to populate the users and accounts tables if you want to import users. You don't need to worry about the sessions table. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
Update(1): Tried to run {
id: 1,
name: 'Admin Administrator',
username: 'admin',
password: '$2b$10$OKsH6r5//AtYlIW/EzUWQeUYdwcKF8JUEeD8RXfLURj8BPw1ilir2',
email: 'admin@mail.com',
gsId: null,
isOnline: null,
createdAt: 2022-02-26T15:21:15.540Z,
updatedAt: 2022-02-26T23:18:46.359Z,
isAdmin: true,
forgotToken: null,
cliToken: 'Qa-fLZjMTGEQrnGUfV8eM',
emailVerificationToken: null,
tokenExpiration: null,
discordRefreshToken: null,
discordAccessToken: null,
discordAccessTokenExpires: 2022-02-26T23:18:46.356Z
} |
Beta Was this translation helpful? Give feedback.
-
I don't fully understand your 3rd option, but when I'm reading it, it seems like alot of work and it could be broken down into steps. There are a couple of things going on, so let's clarify the user experience. In the current experience, user is logging in with their username / password. After they are logged in, they connect to their discord account. This is not the same as giving user an option to login with Discord from the signin page. So I'm thinking the first step could be your option 1, where we deprecate the existing implementation in favor of having |
Beta Was this translation helpful? Give feedback.
-
Update(2): Was able to update the user with discord token data. Semi-final code: export default (req, res) =>
NextAuth(req, res, {
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET
})
],
callbacks: {
async signIn({ account }) {
try {
const { provider, refresh_token, access_token, expires_at } = account
const middlewares = [
loggingMiddleware,
sessionMiddleware(),
userMiddleware
]
const userId = await new Promise((resolve, reject) =>
runMiddlewares(middlewares, req, res, async () => {
if (!req.user?.id) {
return reject({
errorCode: ErrorCode.USER_NOT_LOGGED_IN
})
}
return resolve(req.user.id)
})
)
const accessTokenExpiresAt = new Date(
Date.now() + (expires_at * 1000 || 0)
)
if (provider === 'discord') {
await prisma.user.update({
where: {
id: userId
},
data: {
discordRefreshToken: refresh_token,
discordAccessToken: access_token,
discordAccessTokenExpires: accessTokenExpiresAt
}
})
}
// Means the signIn was successful
return true
} catch {
// Means the signIn wasn't successful
return false
}
}
},
debug: true
}) |
Beta Was this translation helpful? Give feedback.
-
Answer: #1528 (reply in thread) |
Beta Was this translation helpful? Give feedback.
Answer: #1528 (reply in thread)