-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logic to pass newUserInfo when creating a new user #4747
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Hi, @balazsorban44 and @ndom91, I know you two are pretty busy but I was wondering if I can get an update about this proposal? Thanks for all your work! |
Understood. Thanks for the quick response and thanks again for all the hard work you've done! It's truly appreciated 🙌🏽 |
are there any plans to merge this? is there a workaround currently to enable this? |
For user creation you probably have to make a separate request as there is no space in the core types to add your custom data into as of now.
(property) DefaultSession.user?: {
name?: string | null | undefined;
email?: string | null | undefined;
image?: string | null | undefined;
} | undefined Accessing a custom property of
To get around that right now you can define an extension for ExampleSay you'd want to add a // CustomUser.ts
import { User } from "next-auth"
export interface CustomUser extends User {
role: string
} To pass the value down to the client, you define // [...nextauth].js
import NextAuth from "next-auth";
export const authOptions = {
adapter: yourAdapter,
providers: yourProviders,
callbacks: {
// invoked on a session hook call
async session({ session, token, user }) {
// With JS we can add a 'role' prop on session.user (currently impossible with TS)
session.user.role = user.role;
return session;
}
},
}
export default NextAuth(authOptions); Lastly, to access // Component.tsx
import { useSession } from "next-auth/react"
import type { CustomUser } from './CustomUser'
export default function Component() {
const { data: session} = useSession();
// Cast user data to include role injected in session() callback
var userCast = session?.user as CustomUser;
// Errors out
console.log(session?.user.role)
// Ok
return (
<div>{userCast.role}</div>
)
} This PR would allow you to define your extension object using For reference here's a dashboard page that utilizes the above trick. Looking forward to see this merged. |
19c6807
to
3be7bb7
Compare
Hi there @stoickeyboard I think we're ready to review this PR again - could you please help resolve the conflicts and move the changes under |
I build on this PR here #7234 |
Let's close this and move the discussion to #7234 🙏 |
☕️ Reasoning
What changes are being made? What feature/bug is being fixed here?
Added logic that allows the passing of additional info when creating a new user. I wanted the ability to give prospective users a verification code that they could use to allow them to create accounts because I don't want anyone to be able to create an account at the moment. I can then check this verification code in the signup callback before the user is created. That being said, I believe there are more use cases for this such as, allowing the user to save things like their display name when creating an account. I think it's pretty common to ask for a little bit more info when signing up other than just wanting a user's email or Gmail/apple/etc account.
Note: This is my very first pull request so if there is anything I'm missing, please lmk.
🧢 Checklist
🎫 Affected issues
Please scout and link issues that might be solved by this PR.
Fixes: #1069
📌 Resources