Can I authenticate specific blog articles, not the whole content type?

System Information
  • Strapi Version: 14.3.7
  • Operating System: windows
  • Database: sqlite
  • Node Version: 18.13
  • NPM Version: 8.19.3
  • Yarn Version:

Is there a way to flag a specific blog article isAuthenticated? For example if I have a content-type blog and I want to fetch all of my blogs. I want all public users to see all blogs with high-level information like title, description, author etc. But I only want users who are logged in to be able to read specific blogs with the isAuthenticated flag is true. (findOne) Apparently this is a standard feature in WordPress so I am trying to implement in my project. What do you think? Thank you for your time.

Hi!

You can create a custom controller on your content type to validate whenever a request isAuthenticated and a post requires authentication.

Something like this:

  async validateAuthenticatedPost(ctx){
    await this.validateQuery(ctx);

    const { user } = ctx.state
    const { id } = ctx.params

    const article = await strapi.entityService.findOne('api::article.article', id)

    if(article.requiresAuth !== user.id){
      throw strapi.errors.forbidden()
    }
  }

This is just an example, you will have to adapt it to your needs, but basically you will only return the authenticated post if there is an authenticated user

1 Like