PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label strapi. Show all posts
Showing posts with label strapi. Show all posts

Wednesday, October 19, 2022

[FIXED] How to assign ‘author’ role to User in strapi.io?

 October 19, 2022     admin, frontend, mongodb, node.js, strapi     No comments   

Issue

In the user table when I am trying to assign a role to a user, it is showing public and authenticated options only. How to select ‘author’/‘editor’ or any other to the user through the admin panel and also through API?


Solution

First of all, you need to understand that the Editor, Author & Super Admin are roles for admin panel users only. So basically, it will only control/limit what the user can do after logging in into the admin panel (http://localhost:1337/admin). Additionally, these roles can only be assigned to the admin panel users which can be created by visiting the following module:

http://localhost:1337/admin/settings/users.

Now coming to your question, as to why we can't assign Editor/Author to users in the users collection, it's because the roles assigned to these users are in a separate module:

http://localhost:1337/admin/settings/users-permissions/roles

The roles created in this module are basically assigned to API consumers. So you could create roles of your own choice in this module and then:

  1. Limit the set of APIs this role can access
  2. Define whether the route will be public or private to this role
  3. Set rate limiting

Once you create the roles of your choice in this module, then you can go to users collection module.

http://localhost:1337/admin/plugins/content-manager/collectionType/plugins::users-permissions.user?page=1&pageSize=10&_sort=username:ASC

You could then create users (API consumers/users) who will have their own set of credentials (email & password) which can then be used for acquiring the bearer token.

So, to sum up, just create the roles you want to assign to users in this module and then use the user with that role for acquiring bearer token, following which you can call the APIs with that token. Simple!

P.S: Bearer Token is used on the headers for accessing private routes enabled for that particular user role.



Answered By - Salvino D'sa
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, August 5, 2022

[FIXED] How to set up a socket connection on a strapi server

 August 05, 2022     socket.io, strapi     No comments   

Issue

I am trying to integrate socket.io with strapi. But unfortunately I have been unable to do so without any proper tutorial or documentation covering this aspect.

I followed along with the only resource I found online which is: https://medium.com/strapi/strapi-socket-io-a9c856e915a6 But I think the article is outdated. I can't seem to run the code mentioned in it without running into tonnes of errors.

Below is my attempt to implement it and I have been trying to connect it through a chrome websocket plugin smart websocket client But I am not getting any response when I try to run the server.

I'm totally in the dark. Any help will be appreciated

module.exports = ()=> {
  // import socket io
  var io = require('socket.io')(strapi.server)
  console.log(strapi.server) //undefined
  // listen for user connection
  io.on('connect', socket => {
  
      socket.send('Hello!');
      console.log("idit")

      // or with emit() and custom event names
      socket.emit('greetings', 'Hey!', { 'ms': 'jane' }, Buffer.from([4, 3, 3, 1]));

      // handle the event sent with socket.send()
      socket.on('message', (data) => {
        console.log(data);
      });

      // handle the event sent with socket.emit()
      socket.on('salutations', (elem1, elem2, elem3) => {
        console.log(elem1, elem2, elem3);
      });
    });
};

Solution

So I found the solution. Yay. I'll put it here just in case anybody needs it.

boostrap.js

module.exports = async () => {
  process.nextTick(() =>{
    var io = require('socket.io')(strapi.server);
    io.on('connection', async function(socket) {

      console.log(`a user connected`)
      // send message on user connection
      socket.emit('hello', JSON.stringify({message: await strapi.services.profile.update({"posted_by"})}));


      // listen for user diconnect
      socket.on('disconnect', () =>{
        console.log('a user disconnected')
      });
    });
    strapi.io = io; // register socket io inside strapi main object to use it globally anywhere
  })

};

Found this at: https://github.com/strapi/strapi/issues/5869#issuecomment-619508153_

Apparently, socket.server is not available when the server starts. So you have to make use of process.nextTick that waits for the socket.server to initialize.

I'll also add a few questions that I faced when setting this up.

How do i connect from an external client like nuxt,vue or react?

You just have to connect through "http://localhost:1337" that is my usual address for strapi.

I am using nuxt as my client side and this is how set up my socketio on the client side

  1. I first installed nuxt-socket-io through npm
  2. Edited the nuxt.config file as per it's documention
modules:[
  ...
  'nuxt-socket-io',
  ...
],
io: {
    // module options
    sockets: [
      {
        name: 'main',
        url: 'http://localhost:1337',
      },
    ],
  },
  1. And then i finally added a listener in one of my pages.
created() {
    this.socket = this.$nuxtSocket({})
    this.socket.on('hello', (msg, cb) => {
      console.log('SOCKET HI')
      console.log(msg)
    })
},

And it works.



Answered By - Moiz Sohail
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, July 14, 2022

[FIXED] what is wrong with deploy heroku strapi app

 July 14, 2022     heroku, strapi, web-deployment     No comments   

Issue

I am trying to deploy strapi app with mongoose connector but getting this error.

Error: Module not found: Error: Can't resolve './DropdownIndicator' in '/tmp/build_754325e2_/.cache/admin/src/components/Roles/ConditionsSelect' at /tmp/build_754325e2_/node_modules/strapi-admin/index.js:70:23 at finalCallback (/tmp/build_754325e2_/node_modules/webpack/lib/Compiler.js:257:39) at onCompiled (/tmp/build_754325e2_/node_modules/webpack/lib/Compiler.js:265:20) at /tmp/build_754325e2_/node_modules/webpack/lib/Compiler.js:670:21 at eval (eval at create (/tmp/build_754325e2_/node_modules/tapable/lib/HookCodeFactory.js:33:10), :28:1)

can anybody help?


Solution

Try deleting your cache folder then npm run build then push to heroku.



Answered By - Muhammad Ahmod
Answer Checked By - David Marino (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing