How to Manage User Permissions in Strapi CMS?

Sahil Mahalley

Blog / How to Manage User Permissions in Strapi CMS?

Setting up the right permissions in Strapi forms the backbone of your application's security.

The Users & Permissions plugin helps you control who can access what in your Strapi application.

This built-in plugin uses JSON Web Tokens (JWT) to handle user authentication and implements access control lists (ACL) to manage user group permissions.

When you start managing permissions in Strapi, you'll work with the plugin's two main components: authentication and authorization.

The authentication part verifies who users are, while authorization determines what they can do.

Let's break down how the system works:

  • The plugin checks for an Authorization header in each API request
  • It verifies the JWT token present in the header
  • The system matches the user ID from the token to their assigned group
  • Based on the group's permissions, access is either granted or denied

Understanding Role-Based Permissions

Role-based permissions form the foundation of access control in Strapi.

These roles determine what actions users can perform and what content they can access, making it easier to manage user privileges across your application.

Default Roles Overview

Strapi comes with two pre-configured roles that handle the most basic use cases:

Public Role

  • Works for requests without authorization headers
  • Perfect for content that anyone can access
  • Often used for blog posts or product listings
  • Controls what anonymous users can see

Authenticated Role

  • Automatically assigned to new users
  • Defines what logged-in users can do
  • It can be customized based on your needs
  • Manages common user actions

Creating Custom Roles

You can create new roles to match your specific needs:

  1. Go to Settings → Users & Permissions plugin
  2. Click "Roles" and then "Add New Role"
  3. Name your role and set its permissions
  4. Save and assign to users

Configuring User Permissions

Setting up user permissions requires careful planning and precise implementation.

This section walks you through the essential steps to configure access rights, from basic content permissions to advanced API endpoint controls. 

Permission Types

When managing permissions in Strapi, you'll work with three main types:

  1. Collection Types Permissions
    • Control access to your content types
    • Set read/write permissions
    • Manage bulk operations
       
  2. Single Types Permissions
    • Handle standalone content pieces
    • Control who can edit global settings
    • Manage access to unique pages
       
  3. Plugin Permissions
    • Set access levels for installed plugins
    • Control plugin features
    • Manage plugin-specific settings

Setting Up Route Access

To configure route access:

  1. Open the Roles section
  2. Select the role you want to modify
  3. Check/uncheck permissions for each endpoint
  4. Save your changes

Advanced Permission Management

Take your Strapi permissions to the next level with advanced configuration options.

Learn how to implement custom rules, manage JWT settings, and create sophisticated permission structures matching your needs. 

JWT Configuration

Secure your tokens with these settings:

module.exports = {
  jwt: {
    expiresIn: '7d',
    secret: process.env.JWT_SECRET
  }
}

Custom Permission Rules

Create validation rules like this:

validationRules: {
  password: {
    minLength: 8,
    pattern: /^(?=.*[A-Z])(?=.*[0-9])/
  }
}

Authentication Flow Implementation

A solid authentication system keeps your application secure while providing a smooth user experience.

This section covers everything from basic login flows to complex registration processes, helping you build reliable user authentication. 

The login process follows these steps:

  1. User submits credentials
  2. System validates input
  3. JWT token generates
  4. Token returns to the user

For registration:

  1. Set allowed fields
  2. Configure validation rules
  3. Handle user creation
  4. Send confirmation emails

Provider-Based Authentication

Strapi lets you connect with popular authentication providers, making user sign-ups smoother. Here's how to set them up:

Supported Providers Setup

For GitHub authentication:

  1. Create an OAuth app on GitHub
  2. Add credentials to Strapi
  3. Set callback URLs
  4. Enable the provider

For Google setup:

  1. Get API credentials from the Google Console
  2. Configure the OAuth consent screen
  3. Add authorized domains
  4. Update Strapi settings

Provider Configuration

Each provider needs these basic settings:

module.exports = {
  providers: {
    github: {
      enabled: true,
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET
    }
  }
}

Security and Best Practices

Security isn't optional it's essential for protecting your application and user data.

Here's how to implement robust security measures, from password policies to token management, ensuring your Strapi application stays secure. 

Password Policies

Set strong password rules:

  1. Minimum length requirements
  2. Special character requirements
  3. Number requirements
  4. Case sensitivity rules

Example password policy:

passwordPolicy: {
  minLength: 8,
  requireNumbers: true,
  requireSpecialChars: true,
  requireUppercase: true
}

Token Management

Keep your tokens secure:

  • Store tokens in secure HTTP-only cookies
  • Set appropriate expiration times
  • Implement token refresh mechanisms
  • Create a token blacklisting system

Troubleshooting and Maintenance

Even well-configured permission systems need regular maintenance and occasional troubleshooting.

Learn how to identify, diagnose, and fix common permission issues while keeping your system running smoothly. 

Common Permission Issues

Fix these frequent problems:

  1. Missing route permissions
  2. Incorrect role assignments
  3. Token expiration issues
  4. Provider callback errors

Debug steps:

  1. Check role configurations
  2. Verify token validity
  3. Review provider settings
  4. Check server logs

Practical Implementation Examples

Move from theory to practice with real-world implementation examples.

These code samples and use cases show you how to put permission concepts into action in your Strapi application. 

Basic Role Setup

// Create new role
const role = await strapi.plugins['users-permissions'].services.role.create({
  name: 'Editor',
  description: 'Can edit but not delete content'
});

// Assign permissions
await strapi.plugins['users-permissions'].services.permission.create({
  role: role.id,
  type: 'content-type',
  controller: 'article',
  action: 'update'
});

User Authentication

// Login implementation
const response = await strapi.plugins['users-permissions'].services.jwt.issue({
  id: user.id
});

// Token verification
const verified = await strapi.plugins['users-permissions'].services.jwt.verify(token);

Advanced Features and Customization

Push beyond basic permission management with advanced features and customization options.

Discover how to extend Strapi's default functionality with custom callbacks, email templates, and specialized validation rules. 

Custom Callbacks

Create custom authentication callbacks:

module.exports = {
  async afterLogin(ctx) {
    // Custom logic after login
    const { user } = ctx.state;
    await trackUserLogin(user);
  }
}

Email Templates

Customize your authentication emails:

  1. Reset password template
  2. Email verification template
  3. Welcome emails
  4. Account update notifications

Custom Validation Rules

Add specific validation:

module.exports = {
  validatePassword(password) {
    if (password.length < 10) {
      throw new Error('Password too short');
    }
    return true;
  }
}

API Extensions

Extend default functionality:

  1. Add custom endpoints
  2. Modify existing routes
  3. Create middleware
  4. Add custom services

Remember to:

  • Keep permissions updated as you add features
  • Test new configurations thoroughly
  • Document custom implementations
  • Monitor security logs

Managing permissions in Strapi requires ongoing attention and updates. Regular security audits, permission reviews, and user role assessments help maintain a secure system.

Start with basic configurations, then gradually implement more advanced features as your application grows.

By following these guidelines and examples, you'll create a robust permission system that protects your content while providing the right access to your users.

Keep your documentation updated and regularly review your permission structures to maintain security and efficiency.

Sahil Mahalley
by Sahil Mahalley
Jr. Creative Content Writer

End Slow Growth. Put your Success on Steroids