loopback icon indicating copy to clipboard operation
loopback copied to clipboard

same user email across different realms

Open n2sandhu opened this issue 10 years ago • 15 comments

I am trying to add same user(same user means same email) across two different realms (in my case which are organisations). When i do that by using the automigrate file it works fine. Allows me to have same user (with same email address) in the user table for different realms. But, when i try to do the same using the explorer it throws an error saying ""The UserModelName instance is not valid. Details: email Email already exists". Yes, the email exist but for a different realm (or organisation) than the one i am trying to register the user for. Not sure what's going wrong. Some help would be much appreciated. Thanks

n2sandhu avatar Jan 24 '16 23:01 n2sandhu

hi @n2sandhu Can you please provide a repo, so I can have a look at that issue and reproduce on my end. Thanks.

loay avatar Jan 26 '16 21:01 loay

Hi @loay .. I have reproduced the issue on this link and provided a bit of info in the README file. Thanks for your help in advance. https://github.com/n2sandhu/loopback-realms

n2sandhu avatar Jan 26 '16 23:01 n2sandhu

Hi @n2sandhu I had a look at your repo and I think the problem lies basically in the configuration of the relationship itself.

I would simply create only 2 models: Organisation and User the relationship could be Ogranisation hasMany Users and/or Users BelongTo Organisation. You can name your relationship organisations for example That way you can as many users to an organization. It worked just fine.

here is a document to help you with the relations:

https://docs.strongloop.com/display/LB/Creating+model+relations

Also, if you want to read more about the topic, try this blog post: https://strongloop.com/strongblog/defining-and-mapping-data-relations-with-loopback-connected-models/

loay avatar Jan 27 '16 17:01 loay

Hi.. thanks for looking into the issue.. But how would your suggested solution allow me to have multiple users records with same email address in the User table as the email constraints will still exist. What i am trying to do is having a common database for multiple organisations. Each organisation is gonna have a different domain. For example, lets say we have two organisations called "ebay" and "amazon" and a user wants to register on ebay with email "[email protected]" & password "123" and on amazon with email "[email protected]" & password "345" . I want to save the information about the users of both organisation in a single user table, but, because the both the user records have the same email, loopback will not allow me to put both the records through although each user record belongs to different organisation.

n2sandhu avatar Jan 27 '16 23:01 n2sandhu

Hi, Have a look at this image here. Is that what you are trying to do? If not, let me know. if it is the main idea, we need just to translate that to loopback. It is just a diagram here obviously so we need to get the main idea. post

loay avatar Jan 28 '16 03:01 loay

Hi, Yes this is what i am trying to do. Organisation can have multiple users and each record in user table can only belong to one organisation

n2sandhu avatar Jan 28 '16 05:01 n2sandhu

Hi @n2sandhu Then you will need to create HasManyThrough relations as it sets up a many-to-many connection with another model. Please follow this document. https://docs.strongloop.com/display/LB/HasManyThrough+relations Think of the the model designed in the document patient/appointment/physican as the user/organisation/organisation_has_user You should probably use a better name than organisation_has_user to properly describe the situation, such as loginAccess or something similar. Let me know if that works for you.

loay avatar Feb 05 '16 02:02 loay

Hi @n2sandhu Did the last suggestion work out for you? .

loay avatar Feb 11 '16 16:02 loay

Hi I am closing this issue for now. If it is still persisting, please feel free to leave a comment and I will re-open the issue. Thanks.

loay avatar Apr 19 '16 21:04 loay

@loay This issue still persists. @n2sandhu did you find a solution for this?

yashmurty avatar Dec 14 '16 18:12 yashmurty

Yes this issue still persist. The actual problem was that how can we allow same email address/username for different realms. It still gives error of user already exist if I already have a user of same email address.

Realm: ORG1 Email: [email protected] Realm: ORG2 Email: [email protected]

The above mentioned scenario should work. if realm is different. But it is not working, please resolve this issue ASAP, Thanks

@loay Please reopen this issue.

Realm only worked if I remove unique validation from email before creating user OR enter values manually into db with same email/username in different realms.

hishamjaved avatar Jan 03 '17 08:01 hishamjaved

@loay Reopen this issue or create new one.

Monstrik avatar Jan 03 '17 19:01 Monstrik

I found a workaround for this issue, this may help.

Delete built in validations from email and username properties server/models/user.js module.exports = function(User) { delete User.validations.email; delete User.validations.username; // Adds email format validation // Note custom validator because validatesFormat with regex will return false on a null value var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; user.validate('email', function (err) { if (!re.test(this.email) && this.email !== undefined) err(); }, {message: 'Email format is invalid'}); }

Add unique index on email and username with realm server/models/user.json { "name": "user", "base": "User", ... ... "indexes": { "realm_email_index": { "keys": { "realm": 1, "email": 1 }, "options": { "unique": true } }, "realm_username_index": { "keys": { "realm": 1, "username": 1 }, "options": { "unique": true } } } ... ... }

hishamjaved avatar Jan 04 '17 12:01 hishamjaved

Realms seems like to be part of a solution too. https://loopback.io/doc/en/lb3/Partitioning-users-with-realms.html

I need to implement exactly the same. Multiple users with the same email address belonging to different organisations (brands in my case).

christiaanwesterbeek avatar Jan 05 '17 15:01 christiaanwesterbeek

This is my diff for frontend and backend that introduced duplicate email addresses by leveraging realms.

diff --git a/client/app/modules/home/brand/auth/authCtrl.js b/client/app/modules/home/brand/auth/authCtrl.js
index 95d485c..be23405 100644
--- a/client/app/modules/home/brand/auth/authCtrl.js
+++ b/client/app/modules/home/brand/auth/authCtrl.js
@@ -36,7 +36,9 @@

     vm.login = function () {
-        authService.login(vm.user)
+        authService.login(_.merge({
+          realm  : $scope.brand.idBrand
+        }, vm.user))
         .then(function() {
           ...
         })
@@ -54,7 +56,10 @@
     };

     vm.signup = function () {
-        authService.register(_.merge({idBrand: $scope.brand.idBrand}, vm.user))
+        authService.register(_.merge({
+          idBrand: $scope.brand.idBrand,
+          realm  : $scope.brand.idBrand
+        }, vm.user))
         .then(function() {
           ...
diff --git a/common/models/Merchant.js b/common/models/Merchant.js
index acdd007..25cbcde 100644
--- a/common/models/Merchant.js
+++ b/common/models/Merchant.js
@@ -68,6 +68,27 @@ module.exports = function(Merchant) {
     ], cb);
   };

+  Merchant.beforeRemote('create', beforeCreate);
+  function beforeCreate (ctx, user, next) {
+
+    Merchant.findOne({
+      fields: ['email'],
+      where: {
+        realm : ctx.req.body.realm,
+        email : ctx.req.body.email
+      }
+    }, function(err, merchant) {
+      if (err) {
+        return next(err);
+      }
+      if (merchant) {
+        return next(new Error('An account with the same email address already exists in this realm'));
+      }
+
+      next();
+    });
+  }
+
   Merchant.afterRemote('login',    checkUserBrandScope);
   Merchant.afterRemote('findById', checkUserBrandScope);
   function checkUserBrandScope (ctx, user, next) {
diff --git a/common/models/Merchant.json b/common/models/Merchant.json
index 364e002..6fdbc31 100644
--- a/common/models/Merchant.json
+++ b/common/models/Merchant.json
@@ -6,7 +6,8 @@
     "mssql": {
       "schema": "dbo",
       "table": "merchant"
-    }
+    },
+    "realmRequired": true
   },
   "relations": {
     "brand": {

PS: Reset password needs attention too btw.

christiaanwesterbeek avatar Jan 05 '17 16:01 christiaanwesterbeek