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
When using a Wix-managed login page, the code you need to write is split between several parts:
You can also log a site member out of your site from any page.
To implement a login request, take the following steps:
Before redirecting a member to login on a Wix-managed login page you need to prepare the following:
redirectUri
must be an allowed authorization redirect URI.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.
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. The redirectUri
must be an allowed authorization redirect URI.clientId
: Your OAuth app client ID.codeChallenge
: PKCE code challenge string.codeChallengeMethod
: Use "S256"
.responseMode
: Use "fragment"
. The state
and code
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.
The Redirect Session
endpoint responds with:
id
: Redirect session ID.fullUrl
: A URL to the Wix-managed login page.
Redirect the site member to the login URL returned by the Redirect Session
endpoint.
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.
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:
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.
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.
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 the Redirect Session
endpoint. The redirectUri
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.
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.
Once you have tokens, you can use them to make authenticated calls to APIs on behalf of the current member.
To log a site member out, take the following steps:
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.
The Redirect Session
endpoint responds with:
id
: Redirect session ID.fullUrl
: A URL to the Wix-managed login page.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.