wix-users.User

wix-users.User

A site user.

Contents

id Gets the user's ID.
loggedIn Indicates whether the user is logged in or not.
getEmail( ) Gets the email of the current user.
getRoles( ) Gets the user's member roles.
UserRole An object returned by the getRoles() function representing a user's roles.
id

id

Gets the user's ID.

Syntax

get id(): String

Description

The unique ID of the user.

Type

String

Examples

Get the user's ID

bGV0IHVzZXJJZCA9IHVzZXIuaWQ7ICAvLyAicjVnZHVpY21lLTZmZW0tNDg1ai1kanJlLTQ4NDNoNGtqYzM0OSIK
let userId = user.id;  // "r5gduicme-6fem-485j-djre-4843h4kjc349"

Get the current user's information

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCB1c2VyID0gd2l4VXNlcnMuY3VycmVudFVzZXI7CgpsZXQgdXNlcklkID0gdXNlci5pZDsgICAgICAgICAgIC8vICJyNWNtZS02ZmVtLTQ4NWotZGpyZS00ODQ0YzQ5IgpsZXQgaXNMb2dnZWRJbiA9IHVzZXIubG9nZ2VkSW47IC8vIHRydWUKCnVzZXIuZ2V0RW1haWwoKQogIC50aGVuKCAoZW1haWwpID0+IHsKICAgIGxldCB1c2VyRW1haWwgPSBlbWFpbDsgICAgICAvLyAidXNlckBzb21ldGhpbmcuY29tIgogIH0gKTsKCnVzZXIuZ2V0Um9sZXMoKQogIC50aGVuKCAocm9sZXMpID0+IHsKICAgIGxldCBmaXJzdFJvbGUgPSByb2xlc1swXTsKICAgIGxldCByb2xlTmFtZSA9IGZpcnN0Um9sZS5uYW1lOyAgICAgICAgICAgICAgICAvLyAiUm9sZSBOYW1lIgogICAgbGV0IHJvbGVEZXNjcmlwdGlvbiA9IGZpcnN0Um9sZS5kZXNjcmlwdGlvbjsgIC8vICJSb2xlIERlc2NyaXB0aW9uIgogIH0gKTsK
import wixUsers from 'wix-users';

// ...

let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true

user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
  } );

user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );
loggedIn

loggedIn

Indicates whether the user is logged in or not.

Syntax

get loggedIn(): Boolean

Description

Gets one of the following:

  • true — The user is logged in.
  • false — The user is not logged in.

Type

Boolean

Examples

Get whether the user is logged in or not

bGV0IGlzVXNlckxvZ2dlZEluID0gdXNlci5sb2dnZWRJbjsgIC8vIHRydWUK
let isUserLoggedIn = user.loggedIn;  // true

Get the current user's information

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCB1c2VyID0gd2l4VXNlcnMuY3VycmVudFVzZXI7CgpsZXQgdXNlcklkID0gdXNlci5pZDsgICAgICAgICAgIC8vICJyNWNtZS02ZmVtLTQ4NWotZGpyZS00ODQ0YzQ5IgpsZXQgaXNMb2dnZWRJbiA9IHVzZXIubG9nZ2VkSW47IC8vIHRydWUKCnVzZXIuZ2V0RW1haWwoKQogIC50aGVuKCAoZW1haWwpID0+IHsKICAgIGxldCB1c2VyRW1haWwgPSBlbWFpbDsgICAgICAvLyAidXNlckBzb21ldGhpbmcuY29tIgogIH0gKTsKCnVzZXIuZ2V0Um9sZXMoKQogIC50aGVuKCAocm9sZXMpID0+IHsKICAgIGxldCBmaXJzdFJvbGUgPSByb2xlc1swXTsKICAgIGxldCByb2xlTmFtZSA9IGZpcnN0Um9sZS5uYW1lOyAgICAgICAgICAgICAgICAvLyAiUm9sZSBOYW1lIgogICAgbGV0IHJvbGVEZXNjcmlwdGlvbiA9IGZpcnN0Um9sZS5kZXNjcmlwdGlvbjsgIC8vICJSb2xlIERlc2NyaXB0aW9uIgogIH0gKTsK
import wixUsers from 'wix-users';

// ...

let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true

user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
  } );

user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );
getEmail( )

getEmail( )

Gets the email of the current user.

function getEmail(): Promise<String>

Description

The getEmail() function returns a Promise that resolves to the email address of the user that is logged in or rejects if the current user is not logged in.

Return Value

Returns a Promise

On fulfillment String The email address of the user that is logged in.
On rejection String Message containing error that the current user is not logged in.

Examples

Get the user's email address

dXNlci5nZXRFbWFpbCgpCiAgLnRoZW4oIChlbWFpbCkgPT4gewogICAgbGV0IHVzZXJFbWFpbCA9IGVtYWlsOyAvLyAidXNlckBzb21ldGhpbmcuY29tIgogIH0gKTsK
user.getEmail()
  .then( (email) => {
    let userEmail = email; // "user@something.com"
  } );

Get the current user's information

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCB1c2VyID0gd2l4VXNlcnMuY3VycmVudFVzZXI7CgpsZXQgdXNlcklkID0gdXNlci5pZDsgICAgICAgICAgIC8vICJyNWNtZS02ZmVtLTQ4NWotZGpyZS00ODQ0YzQ5IgpsZXQgaXNMb2dnZWRJbiA9IHVzZXIubG9nZ2VkSW47IC8vIHRydWUKCnVzZXIuZ2V0RW1haWwoKQogIC50aGVuKCAoZW1haWwpID0+IHsKICAgIGxldCB1c2VyRW1haWwgPSBlbWFpbDsgICAgICAvLyAidXNlckBzb21ldGhpbmcuY29tIgogIH0gKTsKCnVzZXIuZ2V0Um9sZXMoKQogIC50aGVuKCAocm9sZXMpID0+IHsKICAgIGxldCBmaXJzdFJvbGUgPSByb2xlc1swXTsKICAgIGxldCByb2xlTmFtZSA9IGZpcnN0Um9sZS5uYW1lOyAgICAgICAgICAgICAgICAvLyAiUm9sZSBOYW1lIgogICAgbGV0IHJvbGVEZXNjcmlwdGlvbiA9IGZpcnN0Um9sZS5kZXNjcmlwdGlvbjsgIC8vICJSb2xlIERlc2NyaXB0aW9uIgogIH0gKTsK
import wixUsers from 'wix-users';

// ...

let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true

user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
  } );

user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );
getRoles( )

getRoles( )

Gets the user's member roles.

function getRoles(): Promise<UserRole[]>

Description

The getRoles() function returns a Promise that resolves to the roles of the user that is logged in or rejects if the current user is not logged in.

Member Roles help you manage which site members can access which pages.

Return Value

Returns a Promise

On fulfillment UserRole[ ] The user's roles.
On rejection String Error message.

Examples

Get a user's roles

dXNlci5nZXRSb2xlcygpCiAgLnRoZW4oIChyb2xlcykgPT4gewogICAgbGV0IGZpcnN0Um9sZSA9IHJvbGVzWzBdOwogICAgbGV0IHJvbGVOYW1lID0gZmlyc3RSb2xlLm5hbWU7ICAgICAgICAgICAgICAgIC8vICJSb2xlIE5hbWUiCiAgICBsZXQgcm9sZURlc2NyaXB0aW9uID0gZmlyc3RSb2xlLmRlc2NyaXB0aW9uOyAgLy8gIlJvbGUgRGVzY3JpcHRpb24iCiAgfSApOwo=
user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );

Get the current user's information

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCB1c2VyID0gd2l4VXNlcnMuY3VycmVudFVzZXI7CgpsZXQgdXNlcklkID0gdXNlci5pZDsgICAgICAgICAgIC8vICJyNWNtZS02ZmVtLTQ4NWotZGpyZS00ODQ0YzQ5IgpsZXQgaXNMb2dnZWRJbiA9IHVzZXIubG9nZ2VkSW47IC8vIHRydWUKCnVzZXIuZ2V0RW1haWwoKQogIC50aGVuKCAoZW1haWwpID0+IHsKICAgIGxldCB1c2VyRW1haWwgPSBlbWFpbDsgICAgICAvLyAidXNlckBzb21ldGhpbmcuY29tIgogIH0gKTsKCnVzZXIuZ2V0Um9sZXMoKQogIC50aGVuKCAocm9sZXMpID0+IHsKICAgIGxldCBmaXJzdFJvbGUgPSByb2xlc1swXTsKICAgIGxldCByb2xlTmFtZSA9IGZpcnN0Um9sZS5uYW1lOyAgICAgICAgICAgICAgICAvLyAiUm9sZSBOYW1lIgogICAgbGV0IHJvbGVEZXNjcmlwdGlvbiA9IGZpcnN0Um9sZS5kZXNjcmlwdGlvbjsgIC8vICJSb2xlIERlc2NyaXB0aW9uIgogIH0gKTsK
import wixUsers from 'wix-users';

// ...

let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true

user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
  } );

user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );
UserRole

UserRole

An object returned by the getRoles() function representing a user's roles.

Type

Object

Properties

name String Role name as defined in the site's dashboard or one of "Admin" or "Member".
description (optional) String Role description, if defined in the site's dashboard.

Examples

Get a user's member roles

dXNlci5nZXRSb2xlcygpCiAgLnRoZW4oIChyb2xlcykgPT4gewogICAgbGV0IGZpcnN0Um9sZSA9IHJvbGVzWzBdOwogICAgbGV0IHJvbGVOYW1lID0gZmlyc3RSb2xlLm5hbWU7ICAgICAgICAgICAgICAgIC8vICJSb2xlIE5hbWUiCiAgICBsZXQgcm9sZURlc2NyaXB0aW9uID0gZmlyc3RSb2xlLmRlc2NyaXB0aW9uOyAgLy8gIlJvbGUgRGVzY3JpcHRpb24iCiAgfSApOwo=
user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );

See Also

getRoles( )