How can I get user Session Type?
Answered
Txie posted this in #help-forum
TxieOP
Hello, I'm attempting to identify the authentication method being used by the user session. For instance, if they've logged in via Google, I aim to distinguish that. While exploring the NextAuth documentation and experimenting with callbacks, I encountered difficulty in retrieving the provider type as one of the account options.
My original thought of getting was from
But since the signIn button reloads you're page you I kind of can't do it that way 🫨 .
Help a brotha out...
it's been too long to describe how long I been on this issue.
const { data: session, status } = useSession();
If I where to console log session it will just return me the user > email, image, name.
My original thought of getting was from
const handleGoogleSignIn = () => {
signIn('google');
const providerData = session.user;
setProvider({
google: providerData
})
};
But since the signIn button reloads you're page you I kind of can't do it that way 🫨 .
Help a brotha out...
it's been too long to describe how long I been on this issue.
Answered by Txie
So ya boy found out if any of yall by chance having the same issue that I was having cause you weren't reading the docs here the answer.
import { getToken } from "next-auth/jwt"
const secret = process.env.NEXTAUTH_SECRET
export default async function handler(req, res) {
// if using `NEXTAUTH_SECRET` env variable, we detect it, and you won't actually need to `secret`
// const token = await getToken({ req })
const token = await getToken({ req, secret })
console.log("JSON Web Token", token)
res.end()
}
callbacks: {
async jwt({ token, account, profile }) {
// Persist the OAuth access_token and or the user id to the token right after signin
if (account) {
token.provider = account.provider
}
return token
}
}
1 Reply
TxieOP
So ya boy found out if any of yall by chance having the same issue that I was having cause you weren't reading the docs here the answer.
import { getToken } from "next-auth/jwt"
const secret = process.env.NEXTAUTH_SECRET
export default async function handler(req, res) {
// if using `NEXTAUTH_SECRET` env variable, we detect it, and you won't actually need to `secret`
// const token = await getToken({ req })
const token = await getToken({ req, secret })
console.log("JSON Web Token", token)
res.end()
}
callbacks: {
async jwt({ token, account, profile }) {
// Persist the OAuth access_token and or the user id to the token right after signin
if (account) {
token.provider = account.provider
}
return token
}
}
Answer