# Session management

Source: https://developers.swell.is/frontend-api/session-management

**Swell.js:**

Methods to authenticate a customer's account and get information about their current session.

### Log in

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.

**Log in**

**swell.js**

```javascript
await swell.account.login('runsincircles@shiverisles.net', 'thepassword')
```

### Log out

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.

**Log out**

**swell.js**

```javascript
await swell.account.logout()
```

### Get logged in account

Use to get information about the customer currently logged in.

*Returns an account object, or* *`null`* *if the customer is not logged in.*

**Get logged in account**

**swell.js**

```javascript
await swell.account.get()
```

### Get session values

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.*

**Get session values**

**swell.js**

```javascript
await swell.session.get()
```

### Get the session cookie

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.*

**Get the session cookie**

**swell.js**

```javascript
swell.session.getCookie()
```

### Set the session cookie

Use to set the encoded session cookie initially created by Swell. This can be used to restore the cookie value from an external system.

**Set the session cookie**

**swell.js**

```javascript
swell.session.setCookie(encodedString)
```

**GraphQL:**

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.

**Verify account's session token**

**GraphQL**

```graphql
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).

**Log in account**

**GraphQL**

```graphql
mutation login {
  loginAccount(
    email: "runsincircles@shiverisles.net"
    password: "thepassword"
  ) {
    __typename
  }
}
```

### Log out

Disconnect the account from the current session. Returns a value indicating success.

**Log out account**

**GraphQL**

```graphql
mutation logout {
  logoutAccount {
    success
  }
}
```
