T3 Stack NextAuth How to get Provider access token

I have one question I have T3 Stack, PrismaAdapter, GithubProvider How can I get access token from Github Provider?
callbacks: {
async session({ session, user, token}) {
},
async jwt({ token, user, account, profile, isNewUser }) {
if (user) {
token.id = user.id;
}
return token;
}
}
callbacks: {
async session({ session, user, token}) {
},
async jwt({ token, user, account, profile, isNewUser }) {
if (user) {
token.id = user.id;
}
return token;
}
}
I am trying to get github access token and preserve it I am new to Next.js, NextAuth and T3 stack if I use adapter does it bypass jwt ?
14 Replies
HAL 9000
HAL 9000OP3y ago
do I need to use sessionToken to look up the session in the database? I want github accessToken because I am going to use it to make github api calls
Sean Cassiere
Sean Cassiere3y ago
The access token should already be persisted into your DB being linked to the account. You could write a protected query, and get the user and the accounts from there.
HAL 9000
HAL 9000OP3y ago
@SeanCassiere So I make a tRPC query request which provides me with ctx and I can use ctx to cross reference user ?
Sean Cassiere
Sean Cassiere3y ago
Yes, just remember that your probably DON'T want to pass the access_token back to the frontend for security. You can retrieve the access token from Account table in the DB using the ctx -> session -> user -> id as reference, and then perform your action in the backend itself. Also, make sure that the access_token has the correct scopes, since the default one if just for identity, and doesn't have persmissions for much else.
HAL 9000
HAL 9000OP3y ago
btw where should I put all thje prisma queries in the t3 stack ? and Object destructing doesn't work well with typescript
Sean Cassiere
Sean Cassiere3y ago
That would be a bit more project specific. You could add them directly into your tRPC queries/mutations, or abstract them into separate functions, or something other class based stuff. I'd recommend put them as close as possible to where you are calling them from, till it is no longer viable.
Sean Cassiere
Sean Cassiere3y ago
GitHub
next-discord-clone/create-new-server-by-user.ts at master · SeanCas...
(WIP) - A Discord clone with the T3 Stack. Contribute to SeanCassiere/next-discord-clone development by creating an account on GitHub.
GitHub
next-discord-clone/server.ts at master · SeanCassiere/next-discord-...
(WIP) - A Discord clone with the T3 Stack. Contribute to SeanCassiere/next-discord-clone development by creating an account on GitHub.
GitHub
next-discord-clone/Server.ts at master · SeanCassiere/next-discord-...
(WIP) - A Discord clone with the T3 Stack. Contribute to SeanCassiere/next-discord-clone development by creating an account on GitHub.
HAL 9000
HAL 9000OP3y ago
thanks a lot btw do you use typing with prisma results
Sean Cassiere
Sean Cassiere3y ago
Do you mean how I get the types from a Prisma query?
HAL 9000
HAL 9000OP3y ago
for example
const access_token = await prisma?.account.findFirst({
where: {
userId: ctx.session?.user.id,
},
select: {
access_token: true,
provider: true,
scope: true,
token_type: true,
},
});
const access_token = await prisma?.account.findFirst({
where: {
userId: ctx.session?.user.id,
},
select: {
access_token: true,
provider: true,
scope: true,
token_type: true,
},
});
I have this but whenever I try to destructure
const { access_token, provider, scope, token_type } = await prisma?.account.findFirst({
where: {
userId: ctx.session?.user.id,
},
select: {
access_token: true,
provider: true,
scope: true,
token_type: true,
},
});
const { access_token, provider, scope, token_type } = await prisma?.account.findFirst({
where: {
userId: ctx.session?.user.id,
},
select: {
access_token: true,
provider: true,
scope: true,
token_type: true,
},
});
i get type error or should I just omit type when I use prisma queries
Sean Cassiere
Sean Cassiere3y ago
Could you try this
const myQuery = await prisma....(...your query options);

if (!myQuery) return;

const { access_token } = myQuery
const myQuery = await prisma....(...your query options);

if (!myQuery) return;

const { access_token } = myQuery
I'll be honest, I never use the select option and so types are never an issue there.
HAL 9000
HAL 9000OP3y ago
yeah that worked thanks
Sean Cassiere
Sean Cassiere3y ago
Cool stuff Happy building
HAL 9000
HAL 9000OP3y ago
thanks a lot I learned a lot from you today
Want results from more Discord servers?
Add your server