1
votes

I have extended User model of loopback to make it work with my custom user model. This is the user model(Custom Model) file

{
  "name": "user",
  "base": "User",
  "options": {
    "idInjection": false,
    "mysql": {
      "schema": "BANNERG",
      "table": "User"
    }
  },
  "properties": {
    "id": {
      "type": "Number",
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "id": 1,
      "mysql": {
        "columnName": "id",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      }
    },
    "accId": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "acc_id",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      }
    },
    "fname": {
      "type": "String",
      "required": false,
      "length": 300,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "fname",
        "dataType": "varchar",
        "dataLength": 300,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "lname": {
      "type": "String",
      "required": false,
      "length": 300,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "lname",
        "dataType": "varchar",
        "dataLength": 300,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "username": {
      "type": "String",
      "required": false,
      "length": 300,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "username",
        "dataType": "varchar",
        "dataLength": 300,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "email": {
      "type": "String",
      "required": false,
      "length": 250,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "email",
        "dataType": "varchar",
        "dataLength": 250,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      }
    },
    "phone": {
      "type": "String",
      "required": false,
      "length": 100,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "phone",
        "dataType": "varchar",
        "dataLength": 100,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "status": {
      "type": "Number",
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "status",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      }
    },
    "passwordHash": {
      "type": "String",
      "required": false,
      "length": 300,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "password_hash",
        "dataType": "varchar",
        "dataLength": 300,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "salt": {
      "type": "String",
      "required": false,
      "length": 300,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "salt",
        "dataType": "varchar",
        "dataLength": 300,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "tokenSalt": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "token_salt",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      }
    },
    "parentUserid": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "parent_userid",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      }
    },
    "lastLogginTime": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "last_loggin_time",
        "dataType": "timestamp",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "passwordChange": {
      "type": "String",
      "required": false,
      "length": 20,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "password_change",
        "dataType": "varchar",
        "dataLength": 20,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "userAccountAccess": {
      "type": "String",
      "required": false,
      "length": 2000,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "user_account_access",
        "dataType": "varchar",
        "dataLength": 2000,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "createTs": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "create_ts",
        "dataType": "timestamp",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "updateTs": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "update_ts",
        "dataType": "timestamp",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "createUser": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "create_user",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      }
    },
    "updateUser": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "update_user",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      }
    },
    "password": {
      "type": "String",
      "required": false,
      "length": 45,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "password",
        "dataType": "varchar",
        "dataLength": 45,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "realm": {
      "type": "String",
      "required": false,
      "length": 45,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "realm",
        "dataType": "varchar",
        "dataLength": 45,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    },
    "verificationtoken": {
      "type": "String",
      "required": false,
      "length": 45,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "verificationToken",
        "dataType": "varchar",
        "dataLength": 45,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      }
    }
  },
  "acls": [
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "accessType": "*"
    }
  ]
}

In the my sql database the username is set as loopback and password as loopback.

But when i try and login using this json credentials.

I get this error stack

{
  "error": {
    "statusCode": 401,
    "name": "Error",
    "message": "login failed",
    "code": "LOGIN_FAILED",
    "stack": "Error: login failed\n    at /home/rahul/Loopback/Helloworld/node_modules/loopback/common/models/user.js:249:26\n    at /home/rahul/Loopback/Helloworld/node_modules/loopback-datasource-juggler/lib/dao.js:2168:62\n    at allCb (/home/rahul/Loopback/Helloworld/node_modules/loopback-datasource-juggler/lib/dao.js:2100:7)\n    at /home/rahul/Loopback/Helloworld/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:1196:14\n    at cbForWork (/home/rahul/Loopback/Helloworld/node_modules/loopback-datasource-juggler/lib/observer.js:234:34)\n    at /home/rahul/Loopback/Helloworld/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:428:7\n    at handleResponse (/home/rahul/Loopback/Helloworld/node_modules/loopback-connector-mysql/lib/mysql.js:182:17)\n    at Query._callback (/home/rahul/Loopback/Helloworld/node_modules/loopback-connector-mysql/lib/mysql.js:193:7)\n    at Query.Sequence.end (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)\n    at Query.ErrorPacket (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/sequences/Query.js:90:8)\n    at Protocol._parsePacket (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/Protocol.js:279:23)\n    at Parser.write (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/Parser.js:76:12)\n    at Protocol.write (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/protocol/Protocol.js:39:16)\n    at Socket.<anonymous> (/home/rahul/Loopback/Helloworld/node_modules/mysql/lib/Connection.js:103:28)\n    at emitOne (events.js:96:13)\n    at Socket.emit (events.js:191:7)"
  }
}
1
Did you sign-up using the REST endpoint or did you manually put the credentials into the database? - Antonio Trapani
What's the endpoint you used to login? You can also check the debug logs by setting DEBUG env variable to * - Farid Nouri Neshat

1 Answers

0
votes

It's solution is very simple. Just rename your custom model to "CustomUser" or some other name you like and you are done. I have tried it myself. For reference: https://github.com/strongloop/loopback-example-access-control/issues/73