The /user/login endpoint works fine except if you already have an existing session for that user and then you get a 403 with an empty message returned. Tested using Postman but also occurs using an Ember app.

Same result if you send the same post request a 2nd time.

  • Is this the intended behaviour?
  • Should we have a more informative message?

Seen this with 8.x since 8.0 and still the same in 8.5 dev. Testing with Chrome and Postman.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

blainelang created an issue. See original summary.

Wim Leers’s picture

Title: Login returns 403 if logged in » Requests to log in (cookie auth) via /user/login?_format=json result in 403 without helpful message
Category: Support request » Task
Issue tags: +API-First Initiative, +DX (Developer Experience)

First: thank you so much for taking the time to report this! ❤️ We need this kind of feedback to make API-First Drupal better!


Is this the intended behaviour?

Good question! We do have /user/login_status for checking the login status when using cookie authentication. And these are the relevant routes:

user.login.http:
  path: '/user/login'
  defaults:
    _controller: \Drupal\user\Controller\UserAuthenticationController::login
  methods: [POST]
  requirements:
    _user_is_logged_in: 'FALSE'
    _format: 'json'

+

user.logout.http:
  path: '/user/logout'
  defaults:
    _controller: \Drupal\user\Controller\UserAuthenticationController::logout
  methods: [POST]
  requirements:
    _user_is_logged_in: 'TRUE'
    _format: 'json'
    _csrf_token: 'TRUE'

Note how the values for _user_is_logged_in are each other opposite! This is why we also have /user/login_status as I said before:

user.login_status.http:
  path: '/user/login_status'
  defaults:
    _controller: \Drupal\user\Controller\UserAuthenticationController::loginStatus
  methods: [GET]
  requirements:
    _access: 'TRUE'
    _format: 'json'

So: yes.

Should we have a more informative message?

Yes! Let's do it :) Converting this issue from a support request to a task.

Wim Leers’s picture

Status: Active » Needs review
FileSize
1.11 KB

First: expanded test coverage to check the 403 and assert a helpful error message.

This patch should fail.

Wim Leers’s picture

FileSize
1.16 KB
2.24 KB

And now with the changes to add a helpful message. Patch should pass tests now.

The last submitted patch, 3: 2901574-3.patch, failed testing. View results

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Nice improvement. I like that this could be one day maybe even used for normal 403 sites ...

Wim Leers’s picture

Yep :)

This is where our time investment in infrastructure/foundation work in 8.2 and 8.3 is paying off: both the test coverage and the logic change are trivial 😀✋️

Wim Leers’s picture

BTW I also pinged @blainelang after posting #2+#3+#4 :) https://twitter.com/wimleers/status/903383065300533250

blainelang’s picture

Thanks Wim, this is much better. I've tested the patch and successfully getting the new message. Nice improvement and thanks for all your hard effort and support for D8 REST!

Wim Leers’s picture

You're welcome :)

  • catch committed 52477f6 on 8.5.x
    Issue #2901574 by Wim Leers: Requests to log in (cookie auth) via /user/...

  • catch committed b2828ae on 8.4.x
    Issue #2901574 by Wim Leers: Requests to log in (cookie auth) via /user/...

catch credited catch.

catch’s picture

Version: 8.5.x-dev » 8.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.