Authenticating a Laravel User for a Single Request

Authentication

Committed to the LaraBrain by: wjgilmore (@wjgilmore) at October 29, 2015 6:35 pm

APIs need to authenticate an account for just a single request/response cycle. This tip shows how it is done.

If you're using Laravel's native authentication capabilities, then you probably don't think much about the gory details pertaining to user authentication and session management. However if you're building an API you'll need to (among other things) think about how an account can be authenticated for the duration of one request/response cycle (no cookies or sessions allowed).

Doing so is fortunately easy in Laravel thanks to a little discussed Auth class method called once. Here's an example:

Auth::once(['email' => $email, 'password' => $password])