|
| 1 | +# Authentication |
| 2 | + |
| 3 | +- [Introduction](#introduction) |
| 4 | +- [Configuration](#configuration) |
| 5 | +- [Basic Usage](#basic-usage) |
| 6 | + |
| 7 | +<a name="introduction"></a> |
| 8 | +## Introduction |
| 9 | + |
| 10 | +Lumen is primarily designed for building fast micro-services and APIs; however, if you wish, you may use Laravel's authentication system to authenticate users of your Lumen application. |
| 11 | + |
| 12 | +<a name="configuration"></a> |
| 13 | +## Configuration |
| 14 | + |
| 15 | +> **Note:** Using the authentication system will require enabling sessions. You can do so by uncommenting the middleware listed in the default call to `$app->middleware` in your `bootstrap/app.php` file. |
| 16 | +
|
| 17 | +The authentication system has several configuration options you can set in your `.env` file: |
| 18 | + |
| 19 | +- `AUTH_DRIVER` |
| 20 | +- `AUTH_MODEL` |
| 21 | +- `AUTH_TABLE` |
| 22 | + |
| 23 | +The `AUTH_DRIVER` value specifies the authentication driver used by the framework. If `eloquent` is specified as the driver, the Eloquent ORM driver will be utilized, while `database` will specify that the plain "database" driver should be used. |
| 24 | + |
| 25 | +The `AUTH_MODEL` option specifies the name of the Eloquent model to be used for authentication. This model must implement the `Illuminate\Contracts\Auth\Authenticatable` contract. For an example model, check out the `App\User` model included in the full-stack Laravel framework. |
| 26 | + |
| 27 | +The `AUTH_TABLE` option specifies which databse table contains the "users" of your application. Of course, this option only applies when using the `database` authentication driver. |
| 28 | + |
| 29 | +<a name="basic-usage"></a> |
| 30 | +## Basic Usage |
| 31 | + |
| 32 | +Unlike Laravel, Lumen does not include any scaffolding for authentication, so you will need to use the authentication libraries manually. |
| 33 | + |
| 34 | +> **Note:** If you intend to use the `Auth` facade, be sure to uncomment the `$app->withFacades()` call in your `bootstrap/app.php` file. |
| 35 | +
|
| 36 | +First, let's check out the `attempt` method: |
| 37 | + |
| 38 | + use Illuminate\Http\Request; |
| 39 | + |
| 40 | + $app->post('auth/login', function(Request $request) { |
| 41 | + |
| 42 | + if (Auth::attempt($request->only('email', 'password'))) { |
| 43 | + return redirect('dashboard'); |
| 44 | + } |
| 45 | + |
| 46 | + }); |
| 47 | + |
| 48 | +The `attempt` method accepts an array of key / value pairs as its first argument. The `password` value will be [hashed](/docs/hashing). The other values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the `email` column. If the user is found, the hashed password stored in the database will be compared with the hashed `password` value passed to the method via the array. If the two hashed passwords match, a new authenticated session will be started for the user. |
| 49 | + |
| 50 | +The `attempt` method will return `true` if authentication was successful. Otherwise, `false` will be returned. |
| 51 | + |
| 52 | +> **Note:** In this example, `email` is not a required option, it is merely used as an example. You should use whatever column name corresponds to a "username" in your database. |
| 53 | +
|
| 54 | +#### Authenticating A User With Conditions |
| 55 | + |
| 56 | +You also may add extra conditions to the authentication query: |
| 57 | + |
| 58 | + if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) { |
| 59 | + // The user is active, not suspended, and exists. |
| 60 | + } |
| 61 | + |
| 62 | +#### Determining If A User Is Authenticated |
| 63 | + |
| 64 | +To determine if the user is already logged into your application, you may use the `check` method: |
| 65 | + |
| 66 | + if (Auth::check()) { |
| 67 | + // The user is logged in... |
| 68 | + } |
| 69 | + |
| 70 | +#### Authenticating A User And "Remembering" Them |
| 71 | + |
| 72 | +If you would like to provide "remember me" functionality in your application, you may pass a boolean value as the second argument to the `attempt` method, which will keep the user authenticated indefinitely, or until they manually logout. Of course, your `users` table must include the string `remember_token` column, which will be used to store the "remember me" token. |
| 73 | + |
| 74 | + if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) { |
| 75 | + // The user is being remembered... |
| 76 | + } |
| 77 | + |
| 78 | +If you are "remembering" users, you may use the `viaRemember` method to determine if the user was authenticated using the "remember me" cookie: |
| 79 | + |
| 80 | + if (Auth::viaRemember()) { |
| 81 | + // |
| 82 | + } |
| 83 | + |
| 84 | +#### Authenticating Users By ID |
| 85 | + |
| 86 | +To log a user into the application by their ID, use the `loginUsingId` method: |
| 87 | + |
| 88 | + Auth::loginUsingId(1); |
| 89 | + |
| 90 | +#### Validating User Credentials Without Login |
| 91 | + |
| 92 | +The `validate` method allows you to validate a user's credentials without actually logging them into the application: |
| 93 | + |
| 94 | + if (Auth::validate($credentials)) { |
| 95 | + // |
| 96 | + } |
| 97 | + |
| 98 | +#### Logging A User In For A Single Request |
| 99 | + |
| 100 | +You may also use the `once` method to log a user into the application for a single request. No sessions or cookies will be utilized: |
| 101 | + |
| 102 | + if (Auth::once($credentials)) { |
| 103 | + // |
| 104 | + } |
| 105 | + |
| 106 | +#### Manually Logging In A User |
| 107 | + |
| 108 | +If you need to log an existing user instance into your application, you may call the `login` method with the user instance: |
| 109 | + |
| 110 | + Auth::login($user); |
| 111 | + |
| 112 | +This is equivalent to logging in a user via credentials using the `attempt` method. |
| 113 | + |
| 114 | +#### Logging A User Out Of The Application |
| 115 | + |
| 116 | + Auth::logout(); |
| 117 | + |
0 commit comments