Handle Members with Wix-Managed Login
Share your feedback
Reach out to us with feedback and suggestions to improve the Wix Headless experience, and join the Headless channel of the Devs on Wix Discord community to discuss features and connect with our growing community of developers.
You may want to implement a login flow for your site or app's members. This allows members to access their personal content. The simplest way to create a login flow is to use a Wix-managed login page.
If you to prefer to create a custom login experience, see Handle Members with Custom Login
Prerequisites
- Create a project
- Create an OAuth app for visitors and members
- Add a Wix pages domain
- Add an allowed redirect domain
Login flow
When using a Wix-managed login page, the code you need to write is split between several parts:
- Login request: A login request is initiated from within your site or app. You prepare for redirecting users to Wix and for their eventual return to your site or app. Then you redirect them to the Wix-managed login page.
- Managed login: The member logs in on the Wix-managed login page. After logging in, Wix redirects the member back to your site or app.
- Login callback: The member is redirected back to your site or app. You check that the member is in fact logged in.
You can also log a site member out of your site from any page.
Login request
To implement a login request, take the following steps:
1. Prepare for a redirect session
Before redirecting a member to login on a Wix-managed login page you need to prepare the following:
- A redirect URI for the callback page. This is the URI that the site member is redirected to after logging in. The
redirectUri
must be an allowed authorization redirect URI. - Your OAuth app client ID. You can retrieve this from the Headless Settings menu in the dashboard. If you haven't created an OAuth app yet, see Create an OAuth App for Visitors and Members to set one up.
- A PKCE code verifier and code challenge. These are used to prevent CSRF attacks and authorization code injection attacks.
- An OAuth2 state parameter used to mitigate CSRF attacks.
2. Store security data
Store the code verifier, code challenge, and state parameter locally. For example, you can store the them in
localStorage
or a cookie. You need to use this data when users are redirected back to your site or app from the Wix-managed login page.
3. Get a login URL
Use the Redirect Session
endpoint to get a login URL for the site member.
When calling the Redirect Session
endpoint, send the following parameters, which include the data you prepared earlier:
redirectUri
: There redirect URI to your login callback. TheredirectUri
must be an allowed authorization redirect URI.clientId
: Your OAuth app client ID.codeChallenge
: PKCE code challenge string.codeChallengeMethod
: Use"S256"
.responseMode
: Use"fragment"
. Thestate
andcode
will be returned in the URL used to reach the login callback in a URL fragment.responseType
: Use"code"
.scope
: Use"offline_access"
.state
: A state parameter.
Note: In the request header, use the access token of the current visitor for authorization.
1
The Redirect Session
endpoint responds with:
id
: Redirect session ID.fullUrl
: A URL to the Wix-managed login page.
1
4. Redirect to the Wix login page
Redirect the site member to the login URL returned by the Redirect Session
endpoint.
Managed login
Once you redirect site members to the Wix-managed login page, Wix takes over from there. Members log in on the Wix-managed page and Wix redirects them back to your site or app with information that you can use to verify the login was successful and use to generate member tokens to be used for API calls.
Login callback
Once a site member logs in on the Wix login page, they are redirected to the callback page. To implement a callback page, take the following steps:
1. Get security data
Get the security data that you stored locally before making the login request. Remember, this security data includes a code verifier, code challenge, and state parameter.
2. Get the login results and handle errors
If the login was successful, the URL of the callback page contains a fragment that includes code=
and state=
. Parse the URL to retrieve the code
and state
values.
Check that the state
value is the same as the state value you retrieved from local storage.
Use the code
to generate member tokens.
3. Get the site member's access and refresh tokens
Generate new member tokens using the Token
endpoint.
When calling the Token
endpoint, send the following parameters:
clientId
: The client ID of the OAuth app your project is using.grantType
: Set as"authorization_code"
to get member tokens with an authorization code.redirectUri
: The same URI passed when calling theRedirect Session
endpoint. TheredirectUri
must be an allowed authorization redirect URI.code
: The code you retrieved from the URL fragment.codeVerifier
: The code verifier you retrieved from local storage.
1
The Token
endpoint responds with:
access_token
: An access token used to authorize API calls.expires_in
: The number of seconds before the access token expires. Access tokens expire after 4 hours (14,400 seconds).refresh_token
: A refresh token used to get a new access token.
1
Once you have tokens, you can use them to make authenticated calls to APIs on behalf of the current member.
Log a member out
To log a site member out, take the following steps:
1. Get the member's logout URL
Use the Redirect Session
endpoint to get a logout URL for the site member.
When calling the Redirect Session
endpoint, send the following parameters:
clientId
: Your OAuth app client ID.postFlowUrl
: Where the member will be redirected after logging out.
1
The Redirect Session
endpoint responds with:
id
: Redirect session ID.fullUrl
: A URL to the Wix-managed login page.
2. Redirect to the logout URL
Redirect the browser to the returned fullUrl
to log the site member out. The browser is automatically redirected back to the postFlowUrl
that you passed to the Redirect Session
endpoint.