简体   繁体   中英

Sanity CMS: Get current user from datastore

I'm trying to fetch the current user in Sanity CMS.

I found this code snippet which demonstrates use of userStore

But I don't understand how I can configure my project to accept this module lookup

import userStore from 'part:@sanity/base/user';

It throws an import error Cannot find module 'part:@sanity/base/user' . Which makes sense as its a non standard node module.

  1. How can I resolve this so the module is found?
  2. Or alternatively anyone know another way to fetch the current user?

Inspecting that @sanity source I can see base/lib/datastores which has a userStore it's just not obvious how to consume this.

So this example hook works and returns the User. Sanity is taking care of the module resolution. It's an IDE error not a runtime error in Sanity.

(╯°□°)╯︵ ┻━┻

Their advice is to ignore these un resolved module warnings for part imports.

  "eslintConfig": {
    "rules": {
      "import/no-unresolved": [2, { "ignore": ["^(all|part):"] }]
    }
  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM