kinde-oss / kinde-auth-nextjs

Kinde NextJS SDK - authentication for server rendered apps
https://docs.kinde.com/developer-tools/sdks/backend/nextjs-sdk/
MIT License
157 stars 24 forks source link

Bug: getUser function not returning a username property #178

Closed noelalfaro closed 4 months ago

noelalfaro commented 5 months ago

Prerequisites

Describe the issue

I'm attempting to retrieve the username property for users that have usernames set. However, the getUser method from the getKindeServerSession function only returns an object with the following properties:

{

  "family_name": "family name",

  "given_name": "given name",

  "picture": "picture_url",

  "email": "email",

  "id": "idString",

  "properties": {

    "city": undefined,

    "industry": undefined,

    "job_title": undefined,

    "middle_name": undefined,

    "postcode": undefined,

    "salutation": undefined,

    "state_region": undefined,

    "street_address": undefined,

    "street_address_2": undefined

  }

}

The username property is missing from the response despite the fact that users do have usernames set.

Steps to Reproduce

  1. Import the getKindeServerSession function from @kinde-oss/kinde-auth-nextjs/server.

  2. Call the getUser method.

  3. Observe the response object.

import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

export default async function Home() {

  const { getUser } = getKindeServerSession();

  const user = await getUser();

  console.log(user);

  return (

   

     

{JSON.stringify(user, null, 2)}

   

  );

}


**Expected Behavior**

The response object from the getUser method should include the username property if it is set for the user.

**Actual Behavior**

The getUser method returns an object without the username property, even for users who have usernames set.

**Verification**

I have contacted support and verified that users do indeed have the username property set. This was confirmed by using the Kinde Management API to retrieve user objects, which included the username property.

**Environment**

Package Version: @kinde-oss/kinde-auth-nextjs "^2.2.13"

Node.js Version: v21.1.0

### Library URL

https://github.com/kinde-oss/kinde-auth-nextjs

### Library version

"2.2.13"

### Operating system(s)

macOS

### Operating system version(s)

macOS Sonoma 14.4.1 

### Further environment details

_No response_

### Reproducible test case URL

_No response_

### Additional information

_No response_
peterphanouvong commented 5 months ago

Hey @noelalfaro thanks for raising this - I'll put in a fix to add the username to the User object.

In the meantime you can get around this by doing

  const { getIdToken } = getKindeServerSession();
  const idToken = await getIdToken();
  console.log(idToken.preferred_username)
noelalfaro commented 5 months ago

Trying out that workaround right now and the KindeIdToken type does not have a preferred_usernameproperty but when I console log the entire idToken object returned from await getIdToken()the property does exist.

peterphanouvong commented 4 months ago

Hey @noelalfaro we've added the username property to the KindeUser object now

type KindeUser = {
  id: string;
  email: string | null;
  given_name: string | null;
  family_name: string | null;
  picture: string | null;
  username?: string | null;
  phone_number?: string | null;
  properties?: {
    usr_city?: string;
    usr_industry?: string;
    usr_job_title?: string;
    usr_middle_name?: string;
    usr_postcode?: string;
    usr_salutation?: string;
    usr_state_region?: string;
    usr_street_address?: string;
    usr_street_address_2?: string;
  };
};