Introducing User Roles Authorization with SuperTokens

Table of Contents

  • Introduction
  • What are user roles and why are they important for user authentication?
  • User Roles and SuperTokens
  • Introduction
  • Step 1. Creating the roles and permissions
  • Step 2. Assigning roles to users on sign up
  • Step 3. Guarding APIs based on a user’s role or permissions
  • Conclusion

Part 1 — Introducing user roles and understanding why authorization matters

Authn to AuthZ

What are user roles and why are they important for user authentication?

  • Read all blogs: read:all
  • Delete all blogs: delete:all
  • Delete blogs created by self: delete:self
  • Edit all blogs: edit:all
  • Edit blogs created by self: edit:self

User Roles and SuperTokens

  • Create new roles and permissions — With SuperTokens, roles and permissions are simple strings that can be assigned to users. That means you can name your role anything from user to super-admin-i-have-all-the-permissions.
  • Assign roles to users — Once you’ve created the role, you can assign roles to users after sign up. In fact, you can even assign multiple roles to a single user, so you could give someone both regular-user and admin permissions.
  • Product API and website routes — Now to verify if a user has proper permissions, we need to verify that a session exists and validate that the roles/permissions saved in the access token payload have the appropriate values. Fetching the user’s role from their session is simple, and since our roles are strings, verification is as simple as a string comparison.

Part 2 — Implement SuperTokens User Roles

Authorization Functions
  • we have a regular-user role which will allow users to read all blog posts, but only edit or delete posts created by them.
  • we also also have an admin role which allows admins to create, edit, and delete any blog posts.

Step 1) Creating the roles and permissions

import UserRoles from "supertokens-node/recipe/userroles";// creating the admin role and adding permissions to it.
await UserRoles.createNewRoleOrAddPermissions("admin", ["read:all", "delete:all", "edit:all"])
// creating the regular-user role and adding permissions to it.
await UserRoles.createNewRoleOrAddPermissions("regular-user", ["read:all", "delete:self", "edit:self"])

Step 2) Assigning roles to users on sign up

  • Assign roles to users and their sessions
  • Remove roles from users and their sessions
  • Get a list of all roles assigned to a specific user
  • Get a list of all users that are assigned a specific role
import UserRoles from "supertokens-node/recipe/userroles";// The value of roleToAssign needs to be fetched by you.
// For example, you could check if the user's email is of a certain domain,
// and if it is, then they would be an admin, else not.
if (roleToAssign === "admin") {
// the userId belongs to the user who just signed up.
await UserRoles.addRoleToUser(userId, "admin");
} else {
await UserRoles.addRoleToUser(userId, "regular-user");
}

Step 3) Guarding APIs based on a user’s role or permissions

import { verifySession } from "supertokens-node/recipe/session/framework/express";
import UserRoles from "supertokens-node/recipe/userroles";
app.delete("/blog", verifySession(), async (req, res) => {
let blogId = req.body.blogId
let userId = req.session.getUserId();
// if the user is an admin, we will get ["admin"],
// else we will get ["regular-user"]
let roles = req.session.getClaimValue(UserRoles.UserRoleClaim)
// if the role is "admin", we get ["read:all", "delete:all", "edit:all"]
// else we get ["read:all", "delete:self", "edit:self"]
let permissions = await req.session.getClaimValue(UserRoles.PermissionClaim)
if (permissions.includes("delete:all")) {
// allow delete
} else if (permissions.includes("delete:self")) {
if (getOwnerOfBlog(blogId) === userId) {
// allow delete
} else {
// return access denied error
}
}
})
  • We’ve found the user’s role using UserRoles.UserRoleClaim
  • We’ve found the user’s permissions using UserRoles.PermissionClaim
  • We’ve set up permission barriers around the delete function

Conclusion

--

--

https://supertokens.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store