0

I have button which submit login form. This button generate POST request for my session controller

Started POST "/login" for 127.0.0.1 at 2024-04-03 10:49:13 +0300
Processing by SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"B1kNyKftmEga+Dz7UZ25m4KUDZCn5p7z1qdsC/r84gPhbD9hPNmc4q4WH4IJdos+QS0LDAm0YMX8YpxpzWtPCw==", "session"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}, "lang"=>"en"} 

in route.rb

 post    '/login',      to: 'sessions#create'

But also I have ajax method for update user token

setTimeout( () => {
if (typeof user_grants !== 'undefined') {
  if(!channels['user_session']) {
    userSessionChannel = new Channel("UserSessionChannel");
    userSessionChannel.socketSubscribe();
    userSessionChannel.socketCallBack( {event: 'user_login'} , "user_session", userSessionWebsocketRouter);
    userSessionChannel.socketTrackConnection(toggleDisconnectedModal, 'offline', websocket_keep_alive);
    channels['user_session'] = userSessionChannel;
  }
  const jwtUpdater = new PeriodicRunner(() => {
    $.ajax({
      url: '/renew_jwt',
      type: 'GET',
      success: function (res) {
        if (res.ok) {
          app_config.jwt_encoded_user = res.data;
          ajaxSetup();
        }
      },
      error: function (error) {
        console.log(error);
      },
    });
  }, 300).start()
}

}, 0);

in routs.rb:

get   '/renew_jwt', to: 'application#renew_jwt'

in controller:

  def renew_jwt
    return render json: {ok: true, data: jwt_encode_user}
  end

def jwt_encode_user
  if Rails.env.stage?
    key = Rails.application.credentials.secret_key_base_dev
  elsif Rails.env.production?
    key = Rails.application.credentials.secret_key_base
  else
    key = Rails.application.credentials.secret_key_base_dev
  end
  payload = { user_id: current_user.id.to_s, exp: ((Time.now.to_f * 1000) + (60000 * 10)).to_i }
  response.headers['Cache-Control'] = 'no-cache'
  return JWT.encode payload, key, 'HS256'
end

And sometimes very rarely after login i see response for this ajax request instead of result of my submit request.

enter image description here

My question is: Why I get this result?

2
  • It's very unclear what is going on here and what you even expect to happen. To start with you should really use MimeResponds for that action to ensure that the renew_jwt only responds to and with Application/JSON.
    – max
    Commented Apr 7 at 9:37
  • In other words, I start the process of rendering a new page, but instead I get ajax response as JSON. View should have been displayed, but instead of it - json
    – Anderson
    Commented Apr 10 at 14:24

0