Re_Dei
TTCTheo's Typesafe Cult
•Created by Aidam on 10/22/2023 in #questions
How can I access the entire user model and not just id, name, email and image?
U also need to add this in authoptions:
session: {
strategy: "jwt",
},
8 replies
TTCTheo's Typesafe Cult
•Created by Aidam on 10/22/2023 in #questions
How can I access the entire user model and not just id, name, email and image?
Yes i think u should
8 replies
TTCTheo's Typesafe Cult
•Created by Aidam on 10/22/2023 in #questions
How can I access the entire user model and not just id, name, email and image?
U need to add a few things:
1. In base directory make a next-auth.d.ts with the following code(change it depending on what u need to add):
import { User } from "next-auth"
import { JWT } from "next-auth/jwt"
type UserId = string
declare module "next-auth/jwt" {
interface JWT {
id: UserId,
dob: string
}
}
declare module "next-auth" {
interface Session {
user: User & {
id: UserId;
dob: string;
}
}
}
2. In your authoptions add this(change depending upon your needs):
callbacks: {
async session({ token, session }) {
if (token) {
session.user.id = token.id;
session.user.name = token.name;
session.user.email = token.email;
session.user.dob = token.dob;
session.user.image = token.picture;
}
return session;
},
async jwt({ token, user }) {
const result = await findUser(token.email!);
if (!result) {
if (user) {
token.id = user?.id;
}
return token;
}
return {
id: result.id,
name: result.name,
email: result.email,
picture: result.image,
dob: result.dob,
};
},
},
In the above code, just to make it clear, I added an id and dob in the session.
8 replies
TTCTheo's Typesafe Cult
•Created by Sesse on 10/22/2023 in #questions
Update child client component on change in props passed from parent server component
There are two ways you can handle this:
1. Make a middleware with NextAuth (withAuth in next-auth/middleware) which kicks the user to the login page when the user is logged out.
2. Import useSession from next auth and use it in useEffect to update the child component.
I am not an expert in this as I have only used next auth in 3 of my project. But I use these option if I encounter the problem that you described above.
4 replies