Devise 422 Unprocessable Entity Error when calling the controller from a React front end

Asked
Active3 hr before
Viewed126 times

8 Answers

fronterrorunprocessableentityreactdevise
90%

I've seen this question posted, but without any solutions that work for me. I have a React front end that is mounted on top of a Rails app. When I try to call my registrations controller I am getting the following error:,Connect and share knowledge within a single location that is structured and easy to search.,Asking for help, clarification, or responding to other answers.

Pretag
 Pretag team - issue, fix, solve, resolve
88%

Sorry that I have to post this issue as I am stuck for a few days. I am using Rails 5 API-only app hosted with Heroku. I try to use React as the front end to do user authentication with J-Toker.,I do not think the issue lies with J-Toker. Really thank you so much for your help on this., Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

class ApplicationController < ActionController::API
include DeviseTokenAuth::Concerns::SetUserByToken
respond_to: json

end
load more v
72%

This covers the backend and the views, we’re missing something in between, say, an architecture to follow. Flux is an architecture for building web UIs, and works really well in combination with React (but it can really be applied anywhere).,Let’s start writing some code. We’ll go through a simple Rails app that will feature user signup and login, and the ability to post a story. Just like a tiny Medium. Let’s appropriately call it Small.,It’s worth noting that the UI now will not be served by rails, it might even sit in a different server. There are solution that let us keep both UI and API on the same domain, but for now we will need to enable Cross-origin resource sharing (CORS). We can do this by adding the rack-cors gem to our Gemfile and then add this in our config/application.rb:

Pretag
 Pretag team - issue, fix, solve, resolve
65%

This is my favorite episode thus far. The expansion on top of Rails is so intuitive, never thought how useful separating accounts from users would be.,We're using the acts_as_tenant gem in JumpstartRails.com for row-level multitenancy. It works pretty well and will automatically set your tenant ID when querying and saving records. You end up building your app like a regular app that way which is nice.,In that case, the User models would belong inside the tenants.

In /config/initializers/session_store.rb

Rails.application.config.session_store: cookie_store, key: '_myapp_session', domain: {
   production: '.myapp.com',
   development: '.lvh.me',
   test: '.lvh.me'
}.fetch(Rails.env.to_sym,: all)
load more v
75%

I am trying to check if an user email exist already when someone is filling the field. I am using React in frontend. When there is a validation error, Rails sends 422 as expected, but when no validation error I see 500. You can see the same in the log. How can I fix this?,The solution depends on what you expect the response to be in the valid case. If it were me, I'd not use responders here and be explicit about what to return in the valid and invalid cases.,My suspicion is that respond_with is trying to redirect to the users index action because the user record is valid but not persisted. By convention, users_url should be defined when a users index route is defined. Since it's undefined, I'm guessing your routes.rb file does not have a users index action.

class Users::ValidationsController < ApplicationController
respond_to: json

def email
@user = User.new(email: params[: email])
@user.validate_an_attribute(: email)

respond_with(@user)
end

#....
end
class User < ApplicationRecord
# Include
default devise modules.Others available are:
   #: confirmable,: lockable,: timeoutable and: omniauthable
devise: database_authenticatable,: registerable,: recoverable,: rememberable,: trackable,: validatable
#....

def validate_an_attribute(attribute)
self.class.validators_on(attribute).each do |validator |
      validator.validate_each(self, attribute, self[attribute])
   end
end
end

Here are logs:

Started POST "/users/email" for ::1 at 2018-03-21 21:06:37 +0530
Processing by Users::ValidationsController#email as JSON
  Parameters: {"email"=>"jan02@example.com", "validation"=>{"email"=>"jan02@example.com"}}
  User Exists (0.5ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "jan02@example.com"], ["LIMIT", 1]]
Completed 422 Unprocessable Entity in 3ms (Views: 0.2ms | ActiveRecord: 0.5ms)


Started POST "/users/email" for ::1 at 2018-03-21 21:06:43 +0530
Processing by Users::ValidationsController#email as JSON
  Parameters: {"email"=>"jan1@example.com", "validation"=>{"email"=>"jan1@example.com"}}
  User Exists (0.5ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "jan1@example.com"], ["LIMIT", 1]]
Completed 500 Internal Server Error in 89ms (ActiveRecord: 0.5ms)
NoMethodError (undefined method `users_url' for #<Users::ValidationsController:0x007fbd2bc41040>
Did you mean?  users_email_url):
load more v
40%

Completed 422 Unprocessable Entity [Rails 5 API only, React with J-Toker] #904,Make compatible with devise 4.7 #1331, Error after upgrade to Rails 6.0.0 #1329

Pretag
 Pretag team - issue, fix, solve, resolve
22%

Other Rails methods let me disable setting the token by calling them inside ApplicationController.,cookies[:csrf_token] = authenticity_token would easily do it. This could be set in ApplicationController and be triggered at the very first request.,While building the login form for WildWish, I ran into errors with authentication. I would send a POST request to /users/sign_in, and the server would return 422 Unprocessable Entity. Here is the error itself:

While building the login form for WildWish, I ran into errors with authentication. I would send a POST request to /users/sign_in, and the server would return 422 Unprocessable Entity. Here is the error itself:

Started POST "/users/sign_in"
for::1 at 2020 - 07 - 11 12: 46: 33 - 0600
Processing by Devise::SessionsController #create as JSON
Parameters: {
   "email" => "test",
   "password" => "[FILTERED]",
   "session" => {
      "email" => "test", "password" => "[FILTERED]"
   }
}
HTTP Origin header(http: //localhost:3000) didn't match request.base_url (http://localhost:3001)
      Completed 422 Unprocessable Entity in 1 ms(ActiveRecord: 0.0 ms)

      ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:
load more v
60%

A free lesson from my upcoming course about file uploads in Laravel. Laravel Ajax requests with Laravel and Axios - 422 and 419 errors dev.to. Recently I,(For routes in web.php file) What about 422 Unprocessable Entity ? If you let Laravel refresh your page after POST request, you get errors,Laravel - POST 422 (Unprocessable Entity) the image upload system for a users profile, but I have an error on uploading a file. It says: GET

Pretag
 Pretag team - issue, fix, solve, resolve

Other "front-error" queries related to "Devise 422 Unprocessable Entity Error when calling the controller from a React front end"