Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do we need to do anything special to get ngResource working? #114

Closed
davidtlee opened this issue Feb 10, 2015 · 1 comment
Closed

Do we need to do anything special to get ngResource working? #114

davidtlee opened this issue Feb 10, 2015 · 1 comment

Comments

@davidtlee
Copy link

I'm getting an authentication error when I try to call my rails api (through ngResource) when I am signed in on the angular side. In my rails api controller, I have:

before_action :authenticate_user!

which is supposed to require the user to be signed in. I do believe that I am signed in on the angular side. because $auth.validateUser() is returning the user, which I can see on $scope.user. My angular controller code is:

  $auth.validateUser().then (resp) ->                                                     
    $scope.user = resp                                                                    

  rooms = Room.query( ->                                                                  
    $scope.rooms = rooms                                                                  
  )                                                                                       

The authentication error is happening on Room.query (where Room is defined via ngResource). I get the following response:

errors: ["Authorized users only."]

In case it helps, I tried turning off the authentication filter and checking the value of current_user. It is nil.

Also, below are the headers sent (retrieved from Chrome console):

Remote Address:[::1]:3000
Request URL:http://localhost:3000/api/rooms.json
Request Method:GET
Status Code:401 Unauthorized
Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:currentConfigName=default; auth_headers=%7B%22access-token%22%3A%22wg1TBw99F-WyUkKg2b0zwg%22%2C%22token-type%22%3A%22Bearer%22%2C%22client%22%3A%228lrfL8MbS1Kj6Za1a36wKQ%22%2C%22expiry%22%3A%221424738386%22%2C%22uid%22%3A%22108633578503144270402%22%7D; XSRF-TOKEN=BNLWVtiNas8gw7uARftqyhnUIHZA16RoY5TQK51UKqztbOvFG%2BsNF6Yfy4WJBS0qJoIKwu9uYJ6yWg%2F8vrBxkQ%3D%3D; _synapp-chat_session=VjY1bHMrdTdvMWlFZ3VsN0IzUXViaFZUblQ4SmNwa0tyRUdDelU4eXhkczhjMXY1N3VnV1pDVHd1UXFoaGZxakZCelZqc0k4L1RML2FjbzExUnBKeDg2dHdrdVlkTGhyVXBUSFEzV2tPc1NlVDAybnZhU3RDZlN4K3NzNk83Sm9kYnpaNlk5ekRkK0tnUnlFdmJDSlNHVDJjSzBuNHB6d2Q4MlI2L1lVRi9BUHAwRVZXOE85LzNLK1Q4aW1UZC9tMHFMMi92VjNUL3dwbm5rbUo5dVd3ZEs1bmhCUWVERlNXY3kxSDl5WWJaVT0tLUhFRlM4RU9UNE9VMUY5MGcwSnZaYVE9PQ%3D%3D--77244be28f1b77ade2a0ca74bd1176222afe7961
Host:localhost:3000
If-Modified-Since:0
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
X-CSRF-Token:/XNZzGJFdTsrUtUtZEuPnSw6EqD+N3F+nKT3nGw4QDEUzWRfoSMS462OpSiotch9E2w4FFGOtYhNaihLT9wbDA==
X-XSRF-TOKEN:BNLWVtiNas8gw7uARftqyhnUIHZA16RoY5TQK51UKqztbOvFG+sNF6Yfy4WJBS0qJoIKwu9uYJ6yWg/8vrBxkQ==
@davidtlee
Copy link
Author

Just fixed it :) I think, somehow, it wasn't adding the right variables from the cookie into the header, possibly because my apiUrl was defined as localhost:3000.

By changing apiUrl to:

$authProvider.configure({ apiUrl: 'api' });

and changing my rails route to

mount_devise_token_auth_for 'User', at: 'api/auth'

things seem to be working now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant