OAuthStrategy

Notes:

  • This strategy is only intended for use with Wix Headless.
  • OAuth for Wix Headless only requires a client ID. It doesn't require a client secret.

This authentication strategy is used with a Wix Client to authenticate API calls using OAuth tokens. When using this strategy, the requester's identity is that of the corresponding visitor or member, and permissions are determined by their assigned roles.

Choose your authentication flow

Different methods are designed for different authentication scenarios. Use the table below to find the right methods for your use case:

ScenarioMethods to useLearn more
Anonymous visitorsHandled automatically. Use generateVisitorTokens() for manual control.Handle Visitors
Members with Wix-managed logingenerateOAuthData(), getAuthUrl(), parseFromUrl(), getMemberTokens()Handle Members with Wix-Managed Login
Members with custom login pagelogin(), register(), processVerification(), getMemberTokensForDirectLogin(), sendPasswordResetEmail()Handle Members with a Custom Login Page
Members with external identity providergetMemberTokensForExternalLogin()Handle Members with Externally-Managed Login

After authenticating, use token management methods to store and refresh tokens, and session management methods to speed up redirects to Wix pages.

OAuthStrategy()

Creates an authentication strategy object that uses OAuth for visitor or member authentication.

Syntax

Copy

Parameters

NameTypeDescription
clientIdstringThe Wix Headless client ID.
tokensTokensOptional. The access and refresh tokens to use for authentication.
siteIdstringOptional. The ID of the Wix site to make API calls to.

Returns

OAuthStrategy

Example

Copy

Methods for visitor authentication

Use these methods to manage anonymous visitor sessions.

generateVisitorTokens()

Generates visitor access and refresh tokens.

Use this method when creating a session for visitors using the OAuth authentication strategy. The client automatically calls this method when you make your first API call, but you can call it explicitly to generate tokens before making any calls.

To generate new tokens without checking whether the current access token is valid, use renewToken() instead.

Syntax

Copy

Parameters

NameTypeDescription
tokensTokensOptional. Existing tokens to validate or renew. If provided, returns the same tokens if valid, or new tokens if expired.

Returns

Tokens

Methods for Wix-managed member login

Use these methods when redirecting members to a Wix-hosted login page for authentication.

generateOAuthData()

Generates the data required for an OAuth flow.

Call this method on the page that initiates the login. Store the returned data and use it later on the login callback page when calling getMemberTokens(). Learn more about storing tokens.

Syntax

Copy

Parameters

NameTypeDescription
redirectUristringThe URI to redirect to after authentication is complete.
originalUristringOptional. The URL of the page where the authentication request originated from. Use this to redirect back from the callback page.

Returns

NameTypeDescription
statestringA randomized string to be used in the OAuth flow.
redirectUristringThe URI to redirect to after authentication is complete.
originalUrlstringThe URL of the page where the authentication request originated from.
codeVerifierstringA randomized string to use for PKCE authentication.
codeChallengestringA SHA256 hash of codeVerifier to use for PKCE authentication.

getAuthUrl()

Gets a URL to redirect visitors to for Wix authentication.

Call this method using the OAuthData returned from generateOAuthData(). After the visitor authenticates, they're redirected to the redirectUri specified in the oauthData. The authorization code and state parameter are appended to the URL and can be parsed using parseFromUrl().

Syntax

Copy

Parameters

NameTypeDescription
oAuthDataOauthDataOAuth data generated by generateOAuthData().

Returns

NameTypeDescription
authUrlstringURL to redirect the browser to for Wix authentication.

parseFromUrl()

Parses the code and state fragments from the browser's current URL.

Call this method on the login callback page after the visitor returns from the Wix-managed login page. Use the returned data to verify the authentication was successful and as arguments when calling getMemberTokens().

Syntax

Copy

Returns

NameTypeDescription
codestringThe authorization code appended to the callback page URI by the Wix server.
statestringThe state parameter value appended to the callback page URI by the Wix server.
errorstringOptional. Error, if one occurs.
errorDescriptionstringOptional. Error description, if one occurs.

getMemberTokens()

Retrieves and authenticates a site member's access and refresh tokens.

Call this method on the login callback page after parsing the URL using parseFromUrl(). Pass the stored OAuthData from generateOAuthData() and the parsed URL information.

Syntax

Copy

Parameters

NameTypeDescription
codestringThe authorization code from parseFromUrl().
statestringThe state parameter value from parseFromUrl().
oAuthDataOauthDataOAuth data from generateOAuthData().

Returns

Tokens

Methods for custom member login

Use these methods when building your own login UI instead of redirecting to Wix.

login()

Logs in an existing member using an email address and password.

The method returns a StateMachine object indicating the login result. If loginState is SUCCESS, retrieve the session token from data.sessionToken and pass it to getMemberTokensForDirectLogin().

If loginState is EMAIL_VERIFICATION_REQUIRED, an email with a verification code is sent automatically. Use processVerification() to complete the login.

Syntax

Copy

Parameters

NameTypeDescription
emailstringMember's email address.
passwordstringMember's password.
captchaTokensCaptchaTokensOptional. reCAPTCHA tokens.

Returns

StateMachine

register()

Registers a new member using an email address and password.

The method returns a StateMachine object indicating the registration result. If loginState is SUCCESS, retrieve the session token from data.sessionToken and pass it to getMemberTokensForDirectLogin().

If loginState is EMAIL_VERIFICATION_REQUIRED, an email with a verification code is sent automatically. Use processVerification() to complete registration.

Syntax

Copy

Parameters

NameTypeDescription
emailstringRegistering member's email address.
passwordstringRegistering member's password.
profileIdentityProfileOptional. Details about the registering member.
captchaTokensCaptchaTokensOptional. reCAPTCHA tokens.

Returns

StateMachine

processVerification()

Processes a verification code from a member.

Call this method when the loginState property returned by login() or register() is EMAIL_VERIFICATION_REQUIRED. An email containing a verification code is sent automatically to the member. Use that code when calling this method.

Syntax

Copy

Parameters

NameTypeDescription
verificationCodestringThe verification code from the email sent to the member.

Returns

StateMachine

getMemberTokensForDirectLogin()

Retrieves and authenticates a site member's access and refresh tokens from a session token.

Call this method after a successful login(), register(), or processVerification() call. Pass the session token from the returned StateMachine.data.sessionToken, then use setTokens() to set the tokens as the client's active tokens.

Syntax

Copy

Parameters

NameTypeDescription
sessionTokenstringThe session token from a successful login(), register(), or processVerification() call.

Returns

Tokens

sendPasswordResetEmail()

Sends a password reset email to a member.

The email contains a link to a Wix-managed page where the member can set a new password.

Syntax

Copy

Parameters

NameTypeDescription
emailstringMember's email address.
redirectUristringThe URI to redirect to after the password is reset.

Methods for external identity providers

Use this method when authenticating members through an external identity provider.

getMemberTokensForExternalLogin()

Retrieves and authenticates a site member's access and refresh tokens given a member ID.

This is an administrative method that bypasses the need for a session token. Call it from a secure server environment only.

A member ID can be retrieved using queryMembers() with any externally identifiable field such as email or phone number.

Syntax

Copy

Parameters

NameTypeDescription
memberIdstringMember ID.
apiKeystringAPI key with permissions for Wix Contacts & Members.

Returns

Tokens

Token management methods

Use these methods to manage tokens for any authentication flow.

getTokens()

Gets the active tokens from the client.

Syntax

Copy

Returns

Tokens

setTokens()

Sets tokens as the active tokens for the client.

Syntax

Copy

Parameters

NameTypeDescription
tokensTokensThe tokens to set.

renewToken()

Generates a new access token for an existing refresh token without checking whether the current access token is valid.

To generate a new access token only if the current access token is invalid, use generateVisitorTokens() instead.

Syntax

Copy

Parameters

NameTypeDescription
refreshTokenRefreshTokenThe refresh token to use.

Returns

Tokens

getAuthHeaders()

Gets the client's authorization headers.

Syntax

Copy

Returns

Authorization headers.

loggedIn()

Indicates whether the current visitor is a logged-in member.

Syntax

Copy

Returns

true if the current visitor is logged in, false otherwise.

logout()

Logs out the current logged-in site member.

Syntax

Copy

Parameters

NameTypeDescription
originalUrlstringThe URL to redirect to after logging out.

Returns

NameTypeDescription
logoutUrlstringURL to redirect the browser to in order to initiate the logout.

Session management methods

Speed up redirects to Wix pages like checkout by pre-creating session cookies.

By default, createRedirectSession() creates a session cookie on-demand, which adds latency. To avoid this, check if a cookie exists using sessions.isSessionSynced(). If not, you can call sessions.syncWithWixPages() to create a cookie before calling createRedirectSession().

sessions.isSessionSynced()

Checks whether a session cookie exists for the redirect and is in force.

Syntax

Copy

Returns

true if a current session cookie exists, false if no cookie exists or the existing cookie is expired.

sessions.syncWithWixPages()

Creates a session cookie if none exists.

Syntax

Copy

Properties

captchaVisibleSiteKey

Gets a Wix reCAPTCHA site key for use with a visible reCAPTCHA.

Type: string

captchaInvisibleSiteKey

Gets a Wix reCAPTCHA site key for use with an invisible reCAPTCHA.

Type: string

Objects

Tokens

PropertyTypeDescription
accessTokenobjectThe access token.
accessToken.valuestringToken string value.
accessToken.expiresAtnumberWhen the token expires (Unix timestamp).
refreshTokenobjectThe refresh token.
refreshToken.valuestringToken string value.
refreshToken.rolestringToken role. One of visitor, member, or none.

StateMachine

PropertyTypeDescription
dataobjectSession token for logged-in member as {sessionToken: string}.
loginStatestringOne of: FAILURE, EMAIL_VERIFICATION_REQUIRED, OWNER_APPROVAL_REQUIRED, SUCCESS.
errorCodestringOne of: invalidEmail, invalidPassword, resetPassword, missingCaptchaToken, emailAlreadyExists, invalidCaptchaToken.
errorstringError message.

CaptchaTokens

PropertyTypeDescription
invisibleRecaptchaTokenstringToken for invisible reCAPTCHA.
recaptchaTokenstringToken for visible reCAPTCHA.

IdentityProfile

PropertyTypeDescription
firstNamestringFirst name.
lastNamestringLast name.
nicknamestringNickname.
picturestringPicture.
emailsstring[]Email addresses.
phonesstring[]Phone numbers.
labelsstring[]Labels.
languagestringLanguage.
privacyStatusstringOne of: UNDEFINED, PUBLIC, PRIVATE.
customFieldsobjectCustom fields as an object where the keys are the field name and the values are the field values.
Did this help?