Frontend API
Methods to authenticate a customer's account and get information about their current session.
Use to authenticate a customer with their email address and password. If the email/password combo is correct, their account will be added to the session, making customer-specific methods available. This will set account_logged_in=true and guest=false. In place of a password string, you can pass an object containing a password_token to log the customer in with a one-time token, for example swell.account.login('julia@example.com', { password_token: '...' }). Write the key as password_token even when useCamelCase is enabled — the token is detected before any case conversion is applied.
await swell.account.login('runsincircles@shiverisles.net', 'thepassword')
Use to disconnect the account from the current session. This will set account_logged_in=false and guest=true.
The session itself continues. Logging out does not clear the swell-session cookie or start a new session, and the cart stays attached to it, so items the customer added before logging out are still in their cart afterwards. This is intentional.
Swell.js has no method for clearing a session. A storefront that needs a genuinely fresh session, and therefore an empty cart, can remove the swell-session cookie itself, after which the next request starts a new session. Read the current value with swell.session.getCookie() first if you need to restore it later.
await swell.account.logout()
Use to get information about the customer currently logged in.
Returns an account object, or null if the customer is not logged in.
await swell.account.get()
Use to get current session values, including properties such as `account_id` when the user has been logged-in, and `cart_id` when the user has established a shopping cart.
Returns a decoded session object.
await swell.session.get()Use to get the encoded session cookie created by Swell. This can be used to store the cookie value in an external system with the intention of restoring it later.
Returns an encoded session string.
swell.session.getCookie()Use to set the encoded session cookie initially created by Swell. This can be used to restore the cookie value from an external system.
swell.session.setCookie(encodedString)Methods to authenticate a customer's account and get information about their current session.
Verify session token
Ensure certain pages are only visible to logged-in users. If the user has a valid session token, you will receive an accountId when running this query. If the session token is invalid, the user will be redirected to the login page.
query checkTokenValidity {
session {
accountId
}
}Log in
Authenticate a customer with their email address and password. If the email/password combo is correct, their account will be added to the session, making customer-specific methods available.
The parameters for this mutation are email (required) and password (required), and passwordToken (optional).
mutation login {
loginAccount(
email: "runsincircles@shiverisles.net"
password: "thepassword"
) {
__typename
}
}
Log out
Disconnect the account from the current session. Returns a value indicating success.
mutation logout {
logoutAccount {
success
}
}