0

I have a session value from next auth useSession hook ,

my session callback method is this :

async session({ session, token }) {
  console.log(token);
  session.user.accessToken = token.accessToken;
  session.user.refreshToken = token.refreshToken;
  session.user.username = token.username;

  return session;
},

my session object is in a .js file and it has the properites : accesstoken , refreshtoken , username

if i try to use the value from the use session hook :

  const { data: session, status } = useSession();
  console.log(session?.user?.accessToken!);

i get the mentioned error , any help ? my use session is in a tsx file and my next auth is in a js file if that matters

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Mohamed Nabil
  • 525
  • 2
  • 9
  • 24

2 Answers2

0

In the ".ts" file where useSession custom hook available there you might have created an Interface for user Object like below

Interface user {
  name: String,
  email: String
}

I think you have to add "accessToken" property in the user interface If I am not wrong, so your final interace might be look like below code snippet.

Interface user {
 name: String,
 email: String,
 accessToken: String
}
0

The way I went around it was to overwrite the session type.

First, define a custom User type and Session Type like this:

export type UserType = {
   accessToken : string
   refreshToken : string
   username :  string
}


export type SessionType = {
  data: {
    expires  : string
    jwt : string
    user: UserType
  }
  status: "loading" | "authenticated" | "unathenticated"
}

Then in the .ts file import and use it like this.

  const {data, status}  = useSession() as SessionType 
roqkabel
  • 21
  • 4