Pass oauth_token
& oauth_token_secret
securely without JWT to the browser?
#3120
-
When I used JWT, I passed it inside session: {
jwt: true,
},
callbacks: {
async jwt({ token, account, user }) {
if (user) {
token.username = user.username
}
if (account) {
token[account.provider] = {
oauth_token: account.oauth_token,
oauth_token_secret: account.oauth_token_secret,
}
}
return token
},
async session({ session, token }) {
session.username = user.username
return session
},
}, And used it in my const token = await getToken({
req,
secret: process.env.NEXTAUTH_SECRET,
})
if (!token)
return res.status(401).json({
status: 'token is null',
})
const userClient = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_KEY,
appSecret: process.env.TWITTER_CONSUMER_SECRET,
accessToken: token.twitter.oauth_token,
accessSecret: token.twitter.oauth_token_secret,
}) Now, I'm trying to use it without JWT by storing the session in the database. However, I don't know how to use Do I need to do a database lookup for My code for session: {
jwt: false,
},
callbacks: {
async signIn({ user, profile, account }) {
if (profile) {
user.username = profile.screen_name
}
// if (account) {
// user[account.provider] = {
// oauth_token: account.oauth_token,
// oauth_token_secret: account.oauth_token_secret,
// }
// }
// return true
},
async session({ session, user }) {
session.username = user.username
return session
},
}, How do I do it? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Turns out, the [...nextauth.ts]session: {
jwt: false,
},
callbacks: {
async signIn({ user, profile }) {
if (profile) {
user.username = profile.screen_name
}
return true
},
async session({ session, user }) {
session.username = user.username
return session
},
}, search.tsimport { NextApiRequest, NextApiResponse } from 'next'
import { TwitterApi } from 'twitter-api-v2'
import { getSession } from 'next-auth/react'
import prisma from '@/server/db/prisma'
const search = async (req: NextApiRequest, res: NextApiResponse) => {
const { query } = req.body
const session = await getSession({
req,
})
if (!session)
return res.status(401).json({
status: 'session is null',
})
const token = await prisma.user.findUnique({
where: {
username: session.username,
},
select: {
accounts: {
select: {
oauth_token: true,
oauth_token_secret: true,
},
},
},
})
if (!token)
return res.status(401).json({
status: 'token is null',
})
if (!token.accounts[0].oauth_token || !token.accounts[0].oauth_token_secret)
return res.status(401).json({
status: 'oauth_token & oauth_token_secret is null',
})
const userClient = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_KEY,
appSecret: process.env.TWITTER_CONSUMER_SECRET,
accessToken: token.accounts[0].oauth_token,
accessSecret: token.accounts[0].oauth_token_secret,
})
try {
const data = await userClient.search(query)
return res.status(200).json({
status: 'Ok',
data: data.tweets,
})
} catch (e: unknown) {
return res.status(400).json({
status: (e as Error).message,
})
}
}
export default search The above part, specifically, |
Beta Was this translation helpful? Give feedback.
Turns out, the
oauth_token
&oauth-token_secret
is stored inAccount
schema so I usedprisma
to solve this by checking forsession.username
which I already store as above.[...nextauth.ts]
search.ts