~/javascript/strapi-backend-create-content-type-user-permissions.md

Tutorial - How to Create a Content-type, and Configure User Permissions for REST APIs

2 min readMay 01, 2021by Gorav Singal#javascript#strapi
// TL;DR

Create an Article content type in Strapi with title and rich text fields, then configure user permissions so only authenticated users can create articles while anyone can view published ones.

Tutorial - How to Create a Content-type, and Configure User Permissions for REST APIs

Introduction

In this post, we will see how we can create a content type. And, configure user permissions.

This post assumes you have installed strapi. See How to install Strapi backend

Objective

  • We will create an Article type with following fields,
    • Title
    • Body with rich text
  • Only authenticated user will be able to create an article
  • Anybody can view the published article
  • Enable REST APIs for creating/reading/updating/deleting an article.

Create an Article Content type

Open http://localhost:1337/admin/plugins/content-type-builder/content-types/

Click on Create New Collection Type, and give it a name: article

Create two fields:

  1. title, type: Text
  2. body, type: Rich Text

Save

Strapi Server

Create New Article

Lets try to create a new article.

  • Refresh your strapi admin panel
  • Click on: “Collection Types” -> Articles
  • On top right corner, click on “Add new Article”
  • Write title and body.
  • Click Save. Do not forgot to click on Publish button.

Setting up Permissions for REST API

As I mentioned above, our two objectives here:

  1. Anyone should be able to see articles
  2. Only authenticated users will be able to create one

Configuring Anonymous(Public) user permissions:

  • Click on Settings
  • Click on Roles (Under Users & Permissions plugin)
  • Click on Public, wait for 2-3 seconds. It takes time to load settings
  • Click checkbox for count, findone find
  • Click save

Public Permissions

Configuring Authenticated user permissions:

  • Similarly click on “Authenticated” Role
  • Click checkbox for count, create, delete, find, findone, update
  • Click save

Authenticated Permissions

Try on Postman

GET All Articles

Do a GET request for all articles:

GET /articles

Get all Articles

GET single article by id

GET /articles/{id}

Get single Article

Create Article

Now, if you try to create by anonymous user, you will get http error: 401

Because we have configured to create article only by authenticated users.

We will see this in next post, how we can create an article by REST API and an authenticated user.

Note: So far, we have only configured that only authenticated users can create/edit/delete the article. But, we do not want any authenticated user to update or delete any other article.

Lets see next post for configuring this.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Strapi Tutorial - How to Configure Slug to have Nice URLs for SEO

Strapi Tutorial - How to Configure Slug to have Nice URLs for SEO

Introduction In our previous posts, we have seen How to Create Article in Strapi…

How to Create Article by REST API and Configure only Author can Edit/Update/Delete articles

How to Create Article by REST API and Configure only Author can Edit/Update/Delete articles

Introduction In this post, we will see: create a test user Authenticate it via…

Tutorial - How to Setup Strapi Backend with Mongodb

Tutorial - How to Setup Strapi Backend with Mongodb

Introduction In this step-by-step tutorial, we will setup strapi headless CMS…

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Edit/Update Saved Article

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Edit/Update Saved Article

Introduction This post is in contuation of our previous post: How to use Draft…

Create Articles with Image Upload Using Draft.js, Next.js, and Strapi

Create Articles with Image Upload Using Draft.js, Next.js, and Strapi

Introduction In this post, we will use in Next.js with strapi. And, we will…

How to Integrate Next.js with Strapi Backend and Create a common utility class for REST APIs

How to Integrate Next.js with Strapi Backend and Create a common utility class for REST APIs

Introduction In this post, we will integrate Next.js with Strapi fully. And we…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…