Comments (14)
I use this to get user ID
export default defineConfig({
providers: [
Credentials({
...
}),
],
callbacks: {
session({ session, token }) {
if (session.user && token?.sub) {
session.user.id = token.sub;
}
return session;
}
}
})
from auth-astro.
For me i added it like this to the auth config, but i only tested it for github, not for discord. t3-stack did the same, in order to add the user.id to every object
import GitHub from '@auth/core/providers/github';
import { DrizzleAdapter } from '@auth/drizzle-adapter';
import { db } from './src/utils/db';
import type { AuthConfig } from '@auth/core';
import { env } from '@/t3-env';
export default {
adapter: DrizzleAdapter(db),
providers: [
GitHub({
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET
})
],
callbacks: {
session({ session, user }) {
if (session.user)
session.user.id = user.id;
}
return session;
}
}
} satisfies AuthConfig;
from auth-astro.
Following up, the solution provided by @willnode works! I explored this option for a site I recently launched, and the solution I provided prior was the cause of a silly bug as some users did not have avatars.
To clarify, for me -- this solution returns the User ID. I don't seem to get any UUID-like string as @Cocodrulo got here
from auth-astro.
I have the same error using github. Did you find a solution?
from auth-astro.
The soluzion is adding a callback inside the auth properties, thats add the userid
],
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
}
return session;
}
}
from auth-astro.
The soluzion is adding a callback inside the auth properties, thats add the userid
], callbacks: { session({ session, user }) { if (session.user) { session.user.id = user.id; } return session; } }
[auth][error] JWTSessionError: Read more at https://errors.authjs.dev#jwtsessionerror
[auth][cause]: TypeError: Cannot read properties of undefined (reading 'id')
at Object.session (eval at instantiateModule (file:///C:/.../auth/node_modules/vite/dist/node/chunks/dep-G-px366b.js:54755:28), <anonymous>:18:31)
at Module.session (file:///C:/.../auth/node_modules/@auth/core/lib/actions/session.js:35:52)
at async AuthInternal (file:///C:/.../auth/node_modules/@auth/core/lib/index.js:35:24)
at async Module.Auth (file:///C:/.../auth/node_modules/@auth/core/index.js:104:29)
at async Module.getSession (eval at instantiateModule (file:///C:/.../auth/node_modules/vite/dist/node/chunks/dep-G-px366b.js:54755:28), <anonymous>:56:20)
at async eval (eval at instantiateModule (file:///C:/.../auth/node_modules/vite/dist/node/chunks/dep-G-px366b.js:54755:28), <anonymous>:16:19)
at async AstroComponentInstance.render (eval at instantiateModule (file:///C:/.../auth/node_modules/vite/dist/node/chunks/dep-G-px366b.js:54755:28), <anonymous>:44:7)
at async Object.render (eval at instantiateModule (file:///C:/.../auth/node_modules/vite/dist/node/chunks/dep-G-px366b.js:54755:28), <anonymous>:356:7)
at async Module.renderChild (eval at instantiateModule (file:///C:/.../auth/node_modules/vite/dist/node/chunks/dep-G-px366b.js:54755:28), <anonymous>:36:5)
[auth][details]: {}
Doesn't work for me, I just get an error message when I try to log in, where should I add this?
Adding to auth.config.ts
did not work.
from auth-astro.
Plss solve this
from auth-astro.
But that does not retrieve the actual discord ID does it? it returns a unique identificatory number but not the actual discord ID right?
from auth-astro.
I think it is.
https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/discord.ts#L164-L168
After reading this repo code, I came to know that 'id' is forwarded as 'token.sub'
from auth-astro.
No for what I can see. ID is now smth that not looks like the discord ID
from auth-astro.
For those struggling - the solution I used was a small function to extract it from the image
URL.
The numbers after /avatars
has the User ID, its standard with Discord.
/**
* @summary Extracts the Discord User ID from the `image` field
* Custom utility function designed to enable us to get a User ID from a session (using auth-astro)
* @param {string} imageUrl
* @returns {string}
*/
export function extractUserIdFromImage(imageUrl: string): string {
const match = imageUrl.match(/avatars\/(\d+)\//);
return match ? match[1] : '';
}
from auth-astro.
And what if it has no avatar, then the image url is: 'https://cdn.discordapp.com/embed/avatars/0.png' @fearandesire
from auth-astro.
And what if it has no avatar, then the image url is: 'https://cdn.discordapp.com/embed/avatars/0.png' @fearandesire
Great point! I haven't come across this issue yet myself. That said, I just offered a solution that works for my needs. I'm not sure what to do in the event they don't have an avatar.
from auth-astro.
I dont know why, sometimes token.sub gives the discord ID but other times it return thar strange UUID-like string. I was finally getting the discord ID and then suddenly token.sub was that kind of string again. I don't really understand.
from auth-astro.
Related Issues (20)
- How to use with Cloudflare Pages env vars? HOT 7
- Add support for api context callback to auth config HOT 1
- Endpoints that should set multiple cookies only set one, causing e.g. callbackUrl to be dropped HOT 1
- Auth configuration filename hidden in README
- AstroAuth (Receiving Undefined Options) HOT 4
- Auth verification error not handled as expected HOT 3
- Make server origin configurable
- Initialize auth config lazily HOT 1
- Session cookies not changing when session is updated HOT 4
- Outdated @auth/core version HOT 1
- restrict users HOT 1
- AstroAuthHandler attempts to modify immutable headers HOT 4
- Sign In with Credentials Provider always return OK HOT 3
- Can't use getSession inside Vercel edge middleware
- Should support `output=hybrid` + `prerender=false`? HOT 2
- get provider source with getSession HOT 1
- Errors trying to use Keycloak as OIDC provider HOT 3
- Error on cancel login HOT 1
- I connect it with keycloak, how can I obtain the token, to consult an api if it is authenticated
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from auth-astro.