nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.64k stars 3.46k forks source link

Add values to custom model #1320

Closed vorcigernix closed 3 years ago

vorcigernix commented 3 years ago

Your question How do I update custom value in custom model

What are you trying to do I am following guide for extending default User model and added my own user attribute (basically a copypaste of the guide). It is not clear how do I update this attribute (say phoneNumber as in example).

Feedback

jonlow commented 3 years ago

Yep also not able to add a custom model following the documentation

Noudea commented 3 years ago

I have the same issue sadly :(

jonlow commented 3 years ago

@balazsorban44 can you confirm that creating a custom model from the docs example works for you? Happy to provide more info if you need.

yann-yinn commented 3 years ago

Hi, same here, i tried last night without success following the guide; and also I don't know either how to call the ORM to update my user object once the adapter is written

Noudea commented 3 years ago

One solution i found with mongodb is to use the createUser event, so i add userRole. And then i create a new model for the user profile, this new model is linked with the user by the id.

  events: 
    {
        createUser: async (message) => {
        const client = new MongoClient(process.env.DATABASE_URL, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        });

        if (!client.isConnected()) await client.connect();
        const dbName = process.env.DATABASE_URL.split('/')
            .pop()
            .split('?')
            .shift();

        let usersCollection = client.db(dbName).collection('users');

        usersCollection.updateOne(
            { _id: message.id },
            {
            $set: {
                role : 'user'
            },
            }
        );
        },
    },
HisWife2287 commented 3 years ago

![In Progress](https://badge.waffle.io/nhaarman/triad.svg?label=in-progress&title=In Progress) ![Ready for Release](https://badge.waffle.io/nhaarman/triad.svg?label=next-release&title=Ready for Release)

Triad is a tiny Android library which enables use of the Model-View-Presenter pattern in an easy way. It uses custom Views to replace the dreaded Fragments, and introduces Presenter classes to separate business logic from view logic. Since the Presenters are plain Java objects, tests for these classes can run blazingly fast on a local JVM.

Triad is not a framework, but a tool that can help structure your ui logic in a clean and concise way.

yann-yinn commented 3 years ago

@Noudea nice hack, but as we are in a serverless world, this mean we are creating one new db connection on each api call here, don't we?

Maybe we can call the db connection from next-auth, and maybe it cares of not creating a new connection each time ? (don't know it this is even possible, i'm not used to "serverless" world)

Noudea commented 3 years ago

@yann-yinn hey salut. Je vois que tu es un des fondateurs de popcorn, mon professeur a travaillé dessus, comme quoi le monde est petit :) !!

You are raising very good points here. And i don't really know the solutions. One solution i'm thinking to solve those points is to create and use a middleware to connect to the db, this middleware will be checking if a connection is already existing and if yes use it, else create one. The other solution is to use the connection from next-auth. But i have no idea how to do it. I will try to update my code, but serverless world is also blurry for me.