Skip to main content

User Permissions

$w.auth.getUserInfo

$w.auth.getUserInfo(): Promise<CurrentUserInfo>

Feature Description

Get Currently Logged-in User Information

Response Parameters

CurrentUserInfo

PropertyTypeDescription
namestringUsername
nickNamestringNickname
avatarUrlstringUser avatar
typenumberType. 0: internal user; 1: external user; 2: anonymous user
emailstringEmail
phonestringPhone number
relatedRolesarrayThe roles associated with the user. Specific fields are explained below.
openIdstringWeChat OpenID/Enterprise WeChat OpenID
unionIdstringWeChat unionId/Enterprise WeChat unionId
userIdstringWeDa user id
mainOrgobjectPrimary department
orgsarrayConcurrent departments
userTypestringUser login status. anonymousUser: Not logged in (anonymous user); externalUser: Logged in (external user)
userDescstringUser description

object.relatedRoles[i] property:

PropertyTypeDescription
idstringrole id
envIdstringEnvironment ID
namestringrole name
roleIdentitystringrole identity

object.mainOrg property:

PropertyTypeDescription
idstringprimary department id
namestringPrimary Department Name

object.orgs[i] property:

PropertyTypeDescription
idstringconcurrent department id
namestringConcurrent Department Name

Example

const userInfo = await $w.auth.getUserInfo();
console.log('$w.auth.getUserInfo:', userInfo);

console.log('$w.auth.currentUser:', $w.auth.currentUser);
/**
* {
* name: "xxx",
* avatarUrl: "xxx",
* openId: "xxx",
* unionId: "xxx",
* relatedRoles: [{envId: "lowcode-xxx", id: "xxx",name: "xxx", roleIdentity: "xxx"}],
* type: 1,
* phone: "xxx",
* email: "",
* userId: "xxx",
* mainOrg: {id: "xxx", name: "xxx"},
* orgs: [{id: "xxx", name: "xxx"}],
* userType: "anonymousUser"
* userDesc: "xxx"
* }
* /

$w.auth.currentUser

Feature Description

User Information Reference

Properties

The $w.auth.currentUserand$w.auth.getUserInfohave identical output parameters. Refer to the output parameters of$w.auth.getUserInfo`.

Example

console.log('$w.auth.currentUser:', $w.auth.currentUser);
/**
* {
* name: "xxx",
* openId: "xxx",
* relatedRoles: [{envId: "lowcode-xxx", id: "xxx",name: "xxx", roleIdentity: "xxx"}],
* type: 1,
* phone: "xxx",
* email: "",
* userId: "xxx",
* mainOrg: {id: "xxx", name: "xxx"},
* orgs: [{id: "xxx", name: "xxx"}]
* }
* /

$w.auth.signOut

Feature Description

User Logout

Input Parameters

options: object
PropertyTypeDefault ValueRequiredDescription
isRefreshbooleanfalseNoWhether to refresh the page after exiting

Example

$w.auth?.signOut?.({ isRefresh: true });

// Setting isRefresh = true is equivalent to performing the following operations
$w.auth?.signOut?.();
setTimeout(() => {
// On the web side, refreshing the current page will automatically redirect to the default login page.
window.location.reload();

// On the Mini Program side, after logging out, you need to restart the mini program using the following method. Method reference: https://developers.weixin.qq.com/miniprogram/dev/api/navigate/wx.restartMiniProgram.html
wx.restartMiniProgram({
path: "Replace with your own page path",
});
}, 1000);

$w.auth.loginScope

Feature Description

Get User Login Permission Scope

Response Parameters

Promise<anonymous | undefined>

ValueDescription
anonymousAnonymous user
undefinedNon-anonymous user

Example

try {
const scope = await $w.auth?.loginScope?.();
} catch (e) {
// Not logged in
}
/**
* anonymous: The current state is an anonymous user. When login access is enabled, it can be considered that the user is not logged in.
* undefined: The current state is a non-anonymous user. When login access is enabled, it can be considered that the user is logged in.
*/