Back to Tutorials
Intermediate25 minutes
Build a Membership Site with OElite
Create a complete membership system with user registration, authentication, and role-based access control using Origin Auth.
Prerequisites
- An Origin Auth account (sign up at /signup)
- Your API credentials (tenant ID and API key)
- A web project with your preferred framework
- Node.js 18+ or .NET 8+ for the SDK examples
1
Create Your Tenant
Every membership site needs a dedicated tenant in Origin Auth.
A tenant represents your membership site. It isolates users, configurations, and settings.
curl -X POST https://auth.origin-auth.io/v1.0/auth/register-with-tenant \
-H "Content-Type: application/json" \
-d '{
"email": "admin@mysite.com",
"password": "your-secure-password",
"tenantName": "My Membership Site",
"tenantSlug": "my-membership-site"
}'2
Define Member Roles
Create roles to control what your members can access.
Origin Auth supports custom roles. For a membership site, you might need: Free Member, Premium Member, and Admin.
curl -X POST https://auth.origin-auth.io/v1.0/roles \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Premium Member",
"description": "Paying members with full access",
"permissions": ["read:content", "read:premium", "comment:write"]
}'3
Set Up User Registration
Enable self-registration for new members.
Configure your tenant to allow self-registration and assign the default member role.
// Registration endpoint in your frontend
async function registerMember(email, password, plan) {
const response = await client.auth.register({
email,
password,
metadata: {
plan: plan, // "free" or "premium"
registeredAt: new Date().toISOString(),
},
});
// Assign role based on plan
const roleName = plan === "premium" ? "Premium Member" : "Free Member";
await client.users.assignRole(response.user.id, roleName);
return response;
}4
Protect Premium Content
Use role-based access control to gate premium content.
Verify the user's role before serving premium content. Here's how to check permissions in your backend:
// Middleware to check premium access
async function requirePremium(req, res, next) {
const token = req.headers.authorization?.split(" ")[1];
// Introspect the token
const introspection = await client.auth.introspect(token);
if (!introspection.active) {
return res.status(401).json({ error: "Invalid token" });
}
// Check if user has premium role
const user = await client.users.get(introspection.userId);
const hasPremium = user.roles.some(
(r) => r.name === "Premium Member" || r.name === "Admin"
);
if (!hasPremium) {
return res.status(403).json({ error: "Premium subscription required" });
}
req.user = user;
next();
}5
Implement Member Management
Allow admins to view, manage, and upgrade members.
Use the Origin Auth API to list members, update their roles, and manage their accounts.
// List all members
async function getMembers(page = 1, limit = 20) {
const response = await client.users.list({
page,
limit,
sort: "createdAt:desc",
});
return response;
}
// Upgrade a member to premium
async function upgradeMember(userId) {
await client.users.assignRole(userId, "Premium Member");
// Optionally send a welcome email
await client.notifications.send(userId, {
template: "membership-upgraded",
data: { plan: "Premium" },
});
}
// Cancel a membership
async function cancelMembership(userId) {
await client.users.assignRole(userId, "Free Member");
await client.notifications.send(userId, {
template: "membership-cancelled",
data: {},
});
}What You've Built
- A multi-tenant membership site with Origin Auth
- Role-based access control with Free and Premium tiers
- Self-service member registration and role assignment
- Protected premium content with middleware
- Admin member management capabilities
Next Steps
Ready to learn more? Check out these related tutorials: