Access to authenticated user in custom controller

System Information
  • Strapi Version:4.0.1
  • Operating System:
  • Database: sqlite
  • Node Version: 14.x
  • NPM Version:
  • Yarn Version:

Hey, I’m creating a custom controller to be used on a custom route. It should return the “wallet” with the good user id, (see USER_ID_HERE).

/src/api/wallet/controllers/wallet.js

'use strict';

/**
 *  wallet controller
 */

const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::wallet.wallet', ({ strapi }) =>  ({


  //custom controller to allow ath
  async findMy(ctx) {

    console.log(ctx)
    const entity = await strapi.db.query('api::wallet.wallet').findOne({
      where: {user: {id: USER_ID_HERE}},
      populate: ['user']
    });

    ctx.body = entity;
  },


}));

How can I get the id of the current authenticated user making the request inside of the custom controller ?
I couldn’t find the solution in the docs.
Thank you ! :slight_smile:

TLDR: How to validate jwt token to get user id ?

Update: user can be accessed via: ctx.state.user, and id is ctx.state.user.id.
Found in the docs here: Roles & Permissions - Strapi Developer Docs
Solved, hope this could help other ppl :slight_smile: