Pavel
Pavel

Reputation: 1974

Rails: session variable is nil outside

I have made method in rails controller which returns json with session variable:

def get_facebook_code
    render json: { code: session[:facebook_auth_code] }
end

When I paste link in browser http://localhost:3000/credentials/facebook/code it works fine - session variable returns

And session variable is null when I run request from Postman or from js code from third-party application:

$.ajax({
  type: "GET",
  url: "http://localhost:3000/credentials/facebook/code",
  success: function(msg) {
    console.log(msg);
  },
});

I allow requests for credentials/* path with help of rack-cors gem

resource '/credentials/*', headers: :any, methods: [:get]

Upvotes: 2

Views: 623

Answers (1)

Abhishek Patel
Abhishek Patel

Reputation: 138

Because by default rails stores sessions in cookies.

Somehow you have added that key 'facebook_auth_code' in your browser session. The same key is not available to postman/third party app requests because cookies are different.

Upvotes: 2

Related Questions