Skip to main content
import { StytchB2BClient } from '@stytch/vanilla-js/b2b';

const stytch = new StytchB2BClient('public-token-test-b8c84de4-7d58-4ffc-9341-432b56596862');

const authenticate = async () => {
  if (stytch.session.getSync()) {
    const response = await stytch.session.authenticate({
      session_duration_minutes: 60,
    });
    console.log('Session authenticated:', response);
  }
};

// Refresh session every 50 minutes
const interval = setInterval(authenticate, 3000000);

// Later, to stop the interval
// clearInterval(interval);
{
	"request_id": "request-id-test-b05c992f-ebdc-489d-a754-c7e70ba13141",
	"status_code": 200,
	"member_id": "member-test-32fc5024-9c09-4da3-bd2e-c9ce4da9375f",
	"session_jwt": "example_jwt",
	"session_token": "mZAYn5aLEqKUlZ_Ad9U_fWr38GaAQ1oFAhT8ds245v7Q",
	"intermediate_session_token": "",
	"member_authenticated": true,
	"mfa_required": null,
	"primary_required": null,
	"member_session": {...},
	"member": {...},
	"organization": {...}
}
sessions.authenticate wraps the Authenticate Session endpoint and validates that the session issued to the user is still valid. The SDK will invoke this method automatically in the background.
This method is used by the Stytch SDK internally to refresh session tokens on a regular cadence, so you probably won’t need to call this method directly. However, if you want to manually refresh the session token, you can call this method.

Parameters

session_duration_minutes
number
required
Set the session lifetime to be this many minutes from now. This will return both an opaque session_token and session_jwt for this session, which will automatically be stored in the browser cookies. The session_jwt will have a fixed lifetime of five minutes regardless of the underlying session duration, and will be automatically refreshed by the SDK in the background over time.This value must be a minimum of 5 and may not exceed the maximum session duration minutes value set in the Frontend SDK page of the Stytch Dashboard.A successful authentication will continue to extend the session this many minutes.

Response

session_jwt
string
The JSON Web Token (JWT) for a given Stytch Session.
session_token
string
A secret token for a given Stytch Session.
member_session
object
The Member Session object.
member
object
The Member associated with the Session.
organization
object
The Organization associated with the Member’s Session.
request_id
string
Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue.
status_code
number
The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors.