0

I'm putting together a personal project that I need to login.

the project is in react js and rails API.

to login i am using devise.

the devise by default when you login, it returns the access-token and client in the headers.

but i can't access these tokens in react.

const url = `${process.env.REACT_APP_API_URL}/auth/sign_in`;

const requestOptions = {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(form)
};

const res = await fetch(url, requestOptions).then(res => res.json());

if(res.errors){
  for(let i = 0; i<res.errors.length; i++){
    setFormErrors(form_errors => [...form_errors, res.errors[i]]);
  }
}else{
  console.log(res);
  // window.location.replace('http://localhost:3002/');
}

I need them to use in future requests to the server.

i'm completely lost in how to go ahead.

I don't know if there's a way to access these tokens in the header.

or suddenly I can change the devise to send this data in the body response.

which is the best option?

help me, thanks

1 Answers1

0

I'd found a solution. I don't know if that is the best way to solve it but solved my problem for now.

I add that code in config/application.rb

config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins 'localhost:3002'
        resource "*",
          :headers => :any,
          :expose  => [
            "client",
            "access-token"],
          :methods => [:get, :post, :options, :delete, :put, :head]
      end
    end

man, so... it solved my problem, but like I said, I don't know if it was the best way.

If anyone can explain a correct way please describe.

hug to everyone