Search code examples
ruby-on-railsdevise

How do I set a header when overriding the SessionsController#create action of Devise controllers?


I wrote a small method to generate a JWT and save it on the user model. I'm now trying to override the SessionsController#create method to send back the token on successful login. This is how I'm attempting to do it:

    # SessionsController#create
    def create
        super do |user|
            if user.persisted?
                user.generate_auth_token!
                response.set_header('Authorization', 'Bearer ' + user.auth_token)
            end
        end
    end
    # user model
    def generate_auth_token!
        payload = { user_id: self.id }
        token = JWT.encode(payload, Rails.application.credentials.secret_key_base, 'HS256')
        self.auth_token = token
        self.auth_token_expiration = DateTime.now + 30.days
        self.save!
    end

The token is generated just fine, and it appears to be saved on the user model when I inspect the user record via the rails console.

I'm using Postman (it's like cURL, but with a nice GUI) to inspect the headers from logging in, and there is no 'Authorization' header.


Solution

  • try to do like this response.headers['your_header_name'] = 'value'