Using Beag Session Data

Access and use localStorage session data from Beag.io for personalization and feature gating.

After a successful login through Beag, user session data is stored in localStorage. You can use this data to personalize your app, control feature access, and display subscription information.

Available Values

KeyDescriptionExample Value
x_access_tokenAuthentication tokeneyJhbGciOiJIUzI1NiIs...
x_emailUser’s email address[email protected]
x_plan_idSubscription plan ID123
x_start_dateSubscription start date2023-10-26T10:00:00Z
x_end_dateSubscription end date2024-10-26T10:00:00Z
x_statusSubscription statusPAID

Possible Status Values

  • PAID — Subscription is active and paid
  • FAILED — Payment has failed
  • CANCELLED — Subscription has been cancelled
  • REFUNDED — Payment was refunded
  • PAUSED — Subscription is temporarily paused
  • RESUMED — Subscription was resumed after being paused

Accessing the Data

Basic JavaScript

const accessToken = localStorage.getItem("x_access_token");
const email = localStorage.getItem("x_email");
const planId = localStorage.getItem("x_plan_id");
const startDate = localStorage.getItem("x_start_date");
const endDate = localStorage.getItem("x_end_date");
const status = localStorage.getItem("x_status");

Helper Function

function getBeagSession() {
  return {
    accessToken: localStorage.getItem("x_access_token"),
    email: localStorage.getItem("x_email"),
    planId: localStorage.getItem("x_plan_id"),
    startDate: localStorage.getItem("x_start_date"),
    endDate: localStorage.getItem("x_end_date"),
    status: localStorage.getItem("x_status"),
    isLoggedIn: !!localStorage.getItem("x_access_token"),
    isActive: localStorage.getItem("x_status") === "PAID"
  };
}

// Usage
const session = getBeagSession();
if (session.isLoggedIn && session.isActive) {
  // User has active subscription
}

Common Use Cases

Display User Info

function displayUserInfo() {
  const email = localStorage.getItem("x_email");
  const endDate = new Date(localStorage.getItem("x_end_date"));

  document.getElementById("user-email").textContent = email;
  document.getElementById("subscription-ends").textContent =
    endDate.toLocaleDateString();
}

Feature Gating by Plan

function hasAccess(requiredPlanId) {
  const userPlanId = parseInt(localStorage.getItem("x_plan_id"));
  return userPlanId >= requiredPlanId;
}

// Example: Premium features require plan 2 or higher
if (hasAccess(2)) {
  // Show premium features
} else {
  // Show upgrade prompt
}

Check if Subscription is Expiring Soon

function isExpiringSoon(daysThreshold = 7) {
  const endDate = new Date(localStorage.getItem("x_end_date"));
  const today = new Date();
  const daysRemaining = Math.ceil(
    (endDate - today) / (1000 * 60 * 60 * 24)
  );

  return daysRemaining <= daysThreshold && daysRemaining > 0;
}

if (isExpiringSoon(7)) {
  // Show renewal reminder
}