3

I've been following the AngularJS Tutorial for Rails on Thinkster and right now I'm nearly at the end of User Authentication with Devise. The app seems to run properly on my local server, but when I look at the console I always see the message:

Failed to load resource: the server responded with a status of 401
(Unauthorized) http://localhost:3000/users/sign_in.json

A few sections of the tutorial have been outdated and I've found answers through here and blog posts, for example there was a routing error that required bundle update which had been addressed at the question AngularJS and Rails routing error. (it won't let me post all relevant links because I am a newbie so I can't post more than 2, but there were many small things like that so far in the tutorial).

So far I can't figure out why I'm getting the 401, why it's relevant when the app is still functional, and how to fix it.

I'm not completely sure which parts of the code would be relevant. I have the whole repo on github/KellyTeresa/angular-news but will be glad to pull out and add relevant code blocks if you can point me in the right direction.

Community
  • 1
  • 1
  • Are you submitting a `form_authenticity_token` with your login request? – Daniel Bonnell Oct 14 '15 at 19:20
  • I'm having the same issue, afaik this is how the user redirection to login page works, still, I'll be happy to know if there is a way to check if the user logged in without Error response – yossico Sep 11 '16 at 08:33

3 Answers3

0

You're probably missing your CSRF token, which is accessible as the form_authenticity_token variable in Rails. You can include it in your form like so:

<input name="authenticity_token" type="hidden" ng-model="myModal.token" ng-init="myModel.token='<%= form_authenticity_token %>'">

Then ensure your model has the token attributes like so:

$scope.myModel = {
    email: $scope.email,
    password: $scope.password,
    token: $scope.token
}
Daniel Bonnell
  • 4,817
  • 9
  • 48
  • 88
0

I'm seeing this along with the 401 status codes in chrome devtools E.g.:

{error: "You need to sign in or sign up before continuing."}

and

{error: "Invalid email or password."}

I think these are 401 status codes returned when Devise is checking to see if we're logged in / trying to log us in.

I'm not sure whether they have a huge negative impact when left as is (they don't seem to) what the best way to handle them is (interceptors looks possible) but these looks like starting points:

https://github.com/cloudspace/angular_devise

https://technpol.wordpress.com/2013/09/23/angularjs-and-devise-authentication-with-a-rails-server/

Unifex
  • 197
  • 1
  • 2
0

in your config/initializers/devise.rb ensure you have changed

#config.navigational_formats = ['*/*', :html]

to

config.navigational_formats = ['*/*', :html, :json]
Akshay Revankar
  • 41
  • 1
  • 1
  • 2