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
| Key | Description | Example Value |
|---|---|---|
x_access_token | Authentication token | eyJhbGciOiJIUzI1NiIs... |
x_email | User’s email address | [email protected] |
x_plan_id | Subscription plan ID | 123 |
x_start_date | Subscription start date | 2023-10-26T10:00:00Z |
x_end_date | Subscription end date | 2024-10-26T10:00:00Z |
x_status | Subscription status | PAID |
Possible Status Values
PAID— Subscription is active and paidFAILED— Payment has failedCANCELLED— Subscription has been cancelledREFUNDED— Payment was refundedPAUSED— Subscription is temporarily pausedRESUMED— 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
}