Extending built-in User model

48 views
Skip to first unread message

Jens Winter

unread,
Oct 4, 2015, 6:32:32 AM10/4/15
to LoopbackJS

Hello there,

I can't manage to extend the user model. I want to add a remote method "loginWithFacebook" but it doesn't show up in explorer. I used the Tweet model from https://github.com/belibasakis/loopback-example-ionic and its greet method as a template. But there has to be something different, as it still does not work.

module.exports = function (User) {

    User.loginWithFacebook = function (cb) {
        cb(null, 'Login with Facebook');
    };

    User.remoteMethod(
        'loginWithFacebook',
        {
            http: {verb: 'post'}
        }
    );

};

Any clues here?

Thanks in advance,
Jens

Yaraslau Kurmyza

unread,
Oct 5, 2015, 2:53:38 AM10/5/15
to LoopbackJS
Hi Jens,

Your code is perfectly valid.
The problem might be, that you have to extend base User model with your own one, and extend with remotes the new one, instead.

What I have in model-config.json:
 "User": {
   
"dataSource": "mainDb",
   
"public": false
 
},
 
"user": {
   
"dataSource": "mainDb",
   
"public": true
 
},

and common/models/user.json:
{
 
"name": "user",
 
"base": "User",
 
"idInjection": true,
...
}


And then putting your code in common/models/user.js will work.

P.S. Not sure where I read about this.
The only drawback I've noticed with this approach, is that using relational databases, it creates both tables: User and user
With mongo, you'll stay with one table user

Jens Winter

unread,
Oct 22, 2015, 5:18:46 AM10/22/15
to LoopbackJS

Thank you, Yaraslau!
Reply all
Reply to author
Forward
0 new messages