I am using React frontend with a graphql rails api backend for a SaaS project with apartment. I need to get the authorization token from request.env so i can decode the user_id to get the tenant name which is a field in the User model. This is for a generic elevator based on the user logged in. When i inspect the Rails logger log I see that "HTTP_AUTHORIZATION"=>"" it's empty. what did i go wrong.
This is from the index.js where the authorization is added:
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem(AUTH_TOKEN)
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
}
})
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
})
ReactDOM.render(
<BrowserRouter>
<ApolloProvider client={client}>
<App />
</ApolloProvider>
</BrowserRouter>,
document.getElementById('root')
);
And this is where I try to extract it in Rails:
Rails.application.config.middleware.use Apartment::Elevators::Generic, lambda { |request|
tenant_name = nil
if request.env["HTTP_AUTHORIZATION"]
token = request.env["HTTP_AUTHORIZATION"]
payload = JWT.decode token, Rails.application.secrets.secret_key_base, 'HS256'
user_id = payload.first["user_id"]
tenant_name = User.find(user_id).subdomain
end
tenant_name
}
Aucun commentaire:
Enregistrer un commentaire