I'm trying to get devise to work with my React front end. At the moment I have set up an AuthenticationService in the front end to sign users in. This is the first time I'm setting up devise with React so I'm sure I'm missing something but I can't seem to figure out what.
The picture shows was I am getting when I console log the response from the API.
Here is what my authentication service looks like in the front end.
class AuthenticationService {
async signIn(values) {
return axios
.get(`/users/sign_in`, { user: values })
.then((response) => response.data)
}
}
export default new AuthenticationService()
I have a Users::SessionsController that is empty and my front end looks like the code below. I am submitting a form with Formik, the values are correct when submitting the form and I am currently only console.logging the response to understand what data is being passed back from the AuthenticationService.
<Formik
initialValues={{ email: "", password: "" }}
onSubmit={async (values) => {
console.log("values", values)
try {
const response = await AuthenticationService.signIn(values)
console.log("success?", response.success)
if (response.success) {
window.location.href = response.redirect
? console.log("response redirect")
: console.log("/admin")
}
} catch {
console.log("didn't work")
}
}}
>
this is what my routes look like
Rails.application.routes.draw do
devise_for :users
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root to: "api/dashboard#home"
namespace :api, defaults: { format: :json } do
resources :dashboards
resources :cookbooks
resources :ingredients
resources :recipes
end
end
Devise does not support API mode. https://github.com/heartcombo/devise/issues/4997
You might want to try devise_token_auth gem.
Or write your custom controllers to handle API requests correctly.
Hint: If you use super
, you will end up response with html template or redirection to login page. So do not use them.
https://github.com/heartcombo/devise/wiki/Tool:-Generate-and-customize-controllers