title: how to use middleware in rails to get current_user_id by JWT?
body: <p>I am new in Rails, I usually on Express using middleware to get user.id by decoded JWT,
  it something like this</p>
  
  <p><strong>/middleware/jwt.js</strong> </p>
  
  <pre><code>const authentic = async (req, res, next) =&gt; {
    const token = req.header("token");
    if (!token) {
      res.status(400).json({ error: "no token " });
    } else {
      try {
        const decoded = verifyJwt(token);
        const isAuth = await Person.findOne({ _id: decoded._id });
        if (!isAuth) {
          res.status(400).json({ error: `you are not authentic` });
        } else {
          // keys : _id, role, username
          req.user = decoded;
          next();
        }
      } catch (e) {
        next(e);
      }
    }
  };
  
  export default authentic;
  </code></pre>
  
  <p>then I use it on routes, <code>app.use(authentic)</code>
  so I can get <code>req.user.id</code> on each query I associated or populate</p>
  
  <p>in rails, how to use middleware and required that authentic middle to get current_user_id ?? so I could associate it with other models, 
  I am using the rails for API only, I would love to get some implementation for this without any gems like <code>devise</code> or others, just using JWT </p>
  
  <p>here is on rails for JWT:</p>
  
  <p><strong>/lib/json_web_token.rb</strong> </p>
  
  <pre><code>class JsonWebToken
   class &lt;&lt; self
     def encode(payload, exp = 24.hours.from_now)
       payload[:exp] = exp.to_i
       JWT.encode(payload, Rails.application.secrets.secret_key_base)
     end
  
     def decode(token)
       body = JWT.decode(token, Rails.application.secrets.secret_key_base)[0]
       HashWithIndifferentAccess.new body
     rescue
       nil
     end
   end
  end
  </code></pre>