Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4616

ISSUE WITH COOKIES - RUBY ON RAILS AND REACT JS

$
0
0

I am currently facing an issue using cookies for login. I have ruby on rails in backend and vite in front end.

I am able to send cookies to front end can see in the response headers in developer toolsdeveloper tools screenshot

I can see the set-cookie in the login response.

But I cannot find it under Application->cookies tab in the developer tools.Somewhere I read that you cannot see the token if httponly: true is set so I tried to make a API call to backend to check if cookies are being I am not receiving it I checked for cookies in console.

If possible please also mention how to check cookies, I may be checking it wrong in the backend.

I am adding cors settings and cookies settings I have

Rails.application.config.session_store :cookie_store,                                   key: 'F1Sodharas',                                   domain: 'localhost', # PRODUCTION CHANGE                                   same_site: :none,                                   secure: Rails.env.production?,                                   httponly: true

Login method and setting cookies

def login  @user = User.find_by(email: params[:email])  if @user&.authenticate(params[:password])    token = jwt_encode(user_id: @user.id)    cookies.signed[:jwt] = {      value: token,      expires: 24.hour.from_now    }    log_session(token)    render json: { message: 'Login Successfull', user: UserSerializer.new(@user).serializable_hash[:data] },             status: :ok  else    render json: { message: 'Email And Password Are Not Matching' }, status: :unauthorized  endend

This the axios request

export const getUser = async () => {    try {        const response = await axios.get("http://localhost:3000/user/show", {withCredentials: true});        if (response.status === 200) {            return response.data;        } else {            return null;        }    } catch (error) {        console.error("Error while fetching user data", error);    }}

This is my cors

Rails.application.config.middleware.insert_before 0, Rack::Cors do  allow do    origins 'http://localhost:5173' # PRODUCTION CHANGE    resource '*',             headers: :any,             methods: %i[get post put patch delete options head],             credentails: true  endend

Are the cookies being set properly in the frontend because I cannot find them under Application->cookies section?

Am I sending the cookies to backend for the getUser call ?

Please also mention how to check cookies in the backend.


Viewing all articles
Browse latest Browse all 4616

Trending Articles