3
votes

I have a model UserB2b extended from User. I have enabled authorization using server.authEnabled in the boot script so that all requests pass through the auth middleware.

Problem: Whenever I have an non-numeric username in my UserB2b model, I get a NaN userId from AccessToken.findForRequest in loopback/common/model/access-token.js.

Here are my models.

AccessToken

{
  "name": "AccessToken",
  "base": "AccessToken",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "table": "AccessToken"
  },
  "properties": {
    "ttl": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "ttl",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "created": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "created",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "userId": {
      "type": "String",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "userId",
        "dataType": "varchar",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "id": {
      "type": "String",
      "id": true,
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "id",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "model": {
      "type": "string",
      "id": false,
      "length": 100,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "model",
        "dataType": "varchar",
        "dataLength": 100,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    }
  },
  "validations": [],
  "relations": {
    "userB2B":{
      "type":"belongsTo",
      "model":"UserB2b",
      "foreignKey":"userId"
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId":"$authenticated",
      "permission": "ALLOW"
    }
  ],
  "methods": {}
}

UserB2b

{
  "name": "UserB2b",
  "base": "User",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "table": "user_b2b"
  },
  "scope": {
    "where": {
      "deleted": 0
    }
  },
  "properties": {
    "username": {
      "type": "String",
      "id": true,
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "username",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "password": {
      "type": "String",
      "required": true,
      "length": 1000,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "password",
        "dataType": "varchar",
        "dataLength": 1000,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "name": {
      "type": "String",
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "name",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "contactNumber": {
      "type": "String",
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "contact_number",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "createdAt": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "created_at",
        "dataType": "timestamp",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "updatedAt": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "updated_at",
        "dataType": "timestamp",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "deleted": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 3,
      "scale": 0,
      "mysql": {
        "columnName": "deleted",
        "dataType": "tinyint",
        "dataLength": null,
        "dataPrecision": 3,
        "dataScale": 0,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "email": false,
    "created": false,
    "lastUpdated": false,
    "credentials": false,
    "challenges": false,
    "status": false,
    "verificationToken": false,
    "realm": false,
    "emailVerified": false
  },
  "validations": [],
  "relations": {
    "accessTokens": {
      "type": "hasMany",
      "model": "Accesstoken",
      "foreignKey": "userName"
    }
  },
  "methods": {}
}

Here is the raw packet that I get from the debugger

<-- RowDataPacket
RowDataPacket {
  id: 'BJVqc7CtRXzPVVtTyJqmUabijgLRRHBe3sqcCh0bh5NuGBIZCnY8nwLIGeB6dILv',
  ttl: 12096000,
  created: Tue May 24 2016 05:44:18 GMT+0000 (UTC),
  model: 'UserB2b',
  userId: 'max1' }

and the query that loopback fires after that is

SELECT `username`,`password`,`name`,`contact_number`,`created_at`,`updated_at`,`deleted` FROM `user_b2b` WHERE (`username`=?) AND (`deleted`=?) ORDER BY `username` LIMIT 1, params: ["NaN",0]

Is there something that needs to be corrected here ?

1
It seems that you are also trying to extend built-in AccessToken model. In that case you should name your extended model differently and let your loopback app know, that you are going to use it. Also mind the casing in your model definition (Accesstoken vs AccessToken, userName vs username). - Ivan Schwarz

1 Answers

0
votes

it seems something wrong here in your access token:

"userId": {
  "type": "String",
  "required": false,
  "length": null,
  "precision": 10,
  "scale": 0,
  "mysql": {
    "columnName": "userId",
    "dataType": "int",

The type you are using int and you are passing as varchar item and this will throw exception that string is not a number(NAN). Either change the target type to string/varchar or change the source data type that you are using that matched with target.

Hope this will help you.