Constantly getting 401 errors in loopback while using User Model

前端 未结 2 854
别那么骄傲
别那么骄傲 2021-01-22 12:06

I am new to loopback and I am not able to extend User Base model properly. Though in explorer it shows that it is extended but all API\'s give a 401 error. ex. In normal get cal

相关标签:
2条回答
  • 2021-01-22 12:50

    Some notes meriting consideration though:

    1)You are defining email, password,.. properties, although they are already defined exactly the same way in the parent User model; please see: https://github.com/strongloop/loopback/blob/master/common/models/user.json;

    2)For ACLs you are missing accesstypes, they are not right, but they do not break anything...For more info about ACL please see: https://docs.strongloop.com/display/public/LB/Define+access+controls

    3)Also when you login please make sure to use user that you have created(a POST request) and it is in database already.

    Thanks!

    0 讨论(0)
  • 2021-01-22 12:59

    Seems like you've not logged into the application. Anyway by default most of the functions are not accessible by the settings in user parent class. (This totally is a turn-off)

    1. run the code in the login section

      { "username":"abc", "password":"xyz" }

    2. This action will return the token id.

    3. Enter this id in the top most right corner of the page and click the set token button.
    4. Now you're able to use some of the user function.

    Create a model inheriting user

    :~/nodejs/lab/user-api$ slc loopback:model
    ? Enter the model name: customer
    ? Select the data-source to attach customer to: db (memory)
    ? Select model's base class: User
    ? Expose customer via the REST API? Yes
    ? Custom plural form (used to build REST URL): customers
    Let's add some customer properties now.
    
    Enter an empty property name when done.
    ? Property name: phone
       invoke   loopback:property
    ? Property type: string
    ? Required? No
    
    Let's add another customer property.
    Enter an empty property name when done.
    ? Property name: 
    

    Granting ACL Access:

     slc loopback:acl
    ? Select the model to apply the ACL entry to: customer
    ? Select the ACL scope: All methods and properties
    ? Select the access type: All (match all types)
    ? Select the role: All users
    ? Select the permission to apply: Explicitly grant access
    

    Granting ACL Access once again:

     slc loopback:acl
    ? Select the model to apply the ACL entry to: customer
    ? Select the ACL scope: All methods and properties
    ? Select the access type: All (match all types)
    ? Select the role: All users
    ? Select the permission to apply: Explicitly grant access
    

    When we're granting access two times it takes precedence over DENY in the base class. You'll get a result next time.

    A sample class with the ACLs. You can try it in a loopback project, it'll work :)

    {
      "name": "customer",
      "plural": "customers",
      "base": "User",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "phone": {
          "type": "string"
        }
      },
      "validations": [],
      "relations": {},
      "acls": [
        {
          "accessType": "*",
          "principalType": "ROLE",
          "principalId": "$everyone",
          "permission": "ALLOW"
        },
        {
          "accessType": "READ",
          "principalType": "ROLE",
          "principalId": "$everyone",
          "permission": "ALLOW"
        }
      ],
      "methods": []
    }
    

    Please accept answer if it works. It will. Cheers!

    0 讨论(0)
提交回复
热议问题