简体   繁体   中英

SystemStackError (stack level too deep) - rendering JSON

building with Rails API. Getting the error SystemStackError (stack level too deep) when submitting an API request to login.

The error is around rendering JSON on this line render json: {user: @user, token: token}.as_json, serializer: nil, :status =>:ok

Tried solutions:

SystemStackError (stack level too deep)

Ruby 2.4 and Rails 4 stack level too deep (SystemStackError)

https://github.com/rmosolgo/graphql-ruby/issues/2214

The users controller:

class UsersController < ApplicationController
  #auth_login only action to be authorized before an action
  before_action :authenticate_request, only: [:auto_login, :edit]

  def login
    @user = User.find_by(email: params[:email])

    if @user && @user.authenticate(params[:password])
      token = encode_token({email: @user.email})
      render json: {user: @user, token: token}.as_json, serializer: nil, :status => :ok
    else
      render json: {error: "Invalid email or password"}, :status => :unauthorized
    end
  end

  private

  def default_serializer_options
    {
    serializer: nil
    }
  end
end

The error is annoying as it's inconsistent. Sometimes it builds with no issue and sometimes it doesn't. Not sure where to go with this as I don't see anywhere where code is recursive.

Update: Application Controller with encode_token

class ApplicationController < ActionController::API
    before_action :authenticate_request

    def encode_token(payload)
        JWT.encode(payload, 's3cr3t')
    end

    def auth_header
        #requesting the header type of authorization (with token) that we will declare through our api requests
        # { Authorization: 'Bearer <token>' }
        request.headers['Authorization']
    end

    def decoded_token
        if auth_header
            #going to take the token and decode it
            token = auth_header.split(' ')[1]
            # header: { 'Authorization': 'Bearer <token>' }
            begin
                puts token
                JWT.decode(token, 's3cr3t') #The header is sending the correct token but returning a fail.
            rescue JWT::DecodeError
                # puts "fail"
                nil
            end
        end
    end

    def logged_in_user
        #consults decode_token to check the header for valid information
        if decoded_token
            puts "Do"
            email = decoded_token[0]['email']
            @user = User.find_by(email: email)
        end
    end

    def logged_in?
        #returns true or false
        !!logged_in_user
    end

    def authenticate_request
        #consults logged_in? see see if user is authorized
        render json: { message: 'Please log in' }, status: :unauthorized unless logged_in?
    end
end

For the solution, I found that it was an issue with rendering the @user. Although I had reset the db through rake db:reset and more, I needed to reset postgres. After, all worked as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM