Giter VIP home page Giter VIP logo

Comments (14)

willnode avatar willnode commented on July 17, 2024 5

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.

julianfbeck avatar julianfbeck commented on July 17, 2024 1

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.

fearandesire avatar fearandesire commented on July 17, 2024 1

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.

julianfbeck avatar julianfbeck commented on July 17, 2024

I have the same error using github. Did you find a solution?

from auth-astro.

julianfbeck avatar julianfbeck commented on July 17, 2024

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.

FurStaku avatar FurStaku commented on July 17, 2024

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.

Cocodrulo avatar Cocodrulo commented on July 17, 2024

Plss solve this

from auth-astro.

Cocodrulo avatar Cocodrulo commented on July 17, 2024

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.

willnode avatar willnode commented on July 17, 2024

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.

Cocodrulo avatar Cocodrulo commented on July 17, 2024

No for what I can see. ID is now smth that not looks like the discord ID

image

from auth-astro.

fearandesire avatar fearandesire commented on July 17, 2024

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.

Cocodrulo avatar Cocodrulo commented on July 17, 2024

And what if it has no avatar, then the image url is: 'https://cdn.discordapp.com/embed/avatars/0.png' @fearandesire

from auth-astro.

fearandesire avatar fearandesire commented on July 17, 2024

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.

Cocodrulo avatar Cocodrulo commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.