User authentication
1. Bring your own user authentication
Brief introduction
Laravel makes it very easy to implement authentication mechanisms. In fact, almost all of the settings are already done by default. The certified profiles are placed in the config/auth.php, and in these files are also included with good annotations describing each option's corresponding authentication service.
Laravel default in the App folder is a App\user model that uses the default eloquent authentication driver.
Note: When designing the database structure for this authentication model, the password field has a minimum of 60 character widths. Again, before you begin, make sure that your users (or other synonyms) database table contains a string that is named Remember_token length 100, and accepts null fields. This field will be used to store the "Remember Me" session token. You can also use the $table-remembertoken () in the migration file; Method. Of course, these fields have been set up in the Migrations Laravel 5 itself.
If your application does not use eloquent, you can also use the Laravel Query Builder to do the database authentication driver.
Certification
Laravel has pre-programmed two certified controllers. Authcontroller handles new user registrations and "logins", while Passwordcontroller can help users who have already registered to reset their passwords.
Each controller uses trait to introduce the required methods. In most applications, you do not need to modify these controllers. The views used by these controllers are placed in the Resources/views/auth directory. You can change the view according to your needs.
Table structure
Laravel's own certification includes: User registration, login, password reset, but also self-organizing the data structure required for these functions, located in:
Under Database migrations, after you have configured the databases, perform all migrations:
PHP Artisan Migrate
The database is automatically generated: Users (user table), password_resets (Reset Password table), Migrations (migration table)
User Registration
To modify the form fields that are used when the app registers a new user, you can modify the App\services\registrar class. This class is responsible for validating and building new users of the application.
The Registrar validator method contains validation rules for new users, and registrar's Create method is responsible for creating a new user record in the database. You are free to modify these methods. The Registrar method is called by Authcontroller in the authenticatesandregistersusers trait.
Can look at the source code:
Class Registrar implements registrarcontract { /** * Get a validator for an incoming registration request. * * @param Array $data * @return \illuminate\contracts\validation\validator * /Public Function Validator (array $data) { Return Validator::make ($data, [ ' name ' = ' required|max:255 ', ' email ' = ' required|email|max:255| ' Unique:users ', ' password ' = ' Required|confirmed|min:6 ', ]); } /** * Create A new user instance after a valid registration. * * @param array $data * @return User * /Public Function Create (array $data) { return user::create ([ ' name ' = = $data [' name '], ' email ' + $data [' email '], ' password ' = bcrypt ($data [' Password ']), ]); }
2. Manual user authentication
If you do not want to use the default Authcontroller, you need to manage user authentication directly using the Laravel authentication class. Don't worry, it's easy too! First, let's look at the attempt method:
$email, ' password ' = $password]) {return redirect ()->intended (' Dashboard ');}} }
The attempt method can accept an array of key-value pairs as the first parameter. The value of the password is hashed first. Other values in the array are used to query the user in the data table. So, in the example above, the user is identified based on the value of the email column. If the user is found, it is compared to the hashed password stored in the database and the password value after the hash in the array. Assuming the same password after the two hashes, the session will be restarted for the user to start the authentication pass.
If the authentication succeeds, attempt will return true. Otherwise, false is returned.
* * Note: In the above example, the email field is not necessarily used, just as an example. You should use any key value that corresponds to the "username" in the data table.
The intended method redirects to the URL that the user is trying to access, and its value is saved before authentication is filtered. You can also pass a preset URI to this method, preventing the redirected URLs from being used.
Validating users with specific criteria
During the certification process, you may want to add additional authentication criteria:
if (auth::attempt ([' email ' = = $email, ' password ' = ' + ' $password, ' active ' + 1])) { //the user is active, not s Uspended, and exists.}
As you can see, this validation adds a validation of an active field.
Authenticate the user and "remember" him
If you want to provide the "Remember Me" feature in your app, you can pass in the Boolean as the second parameter of the attempt method, so that you can retain the user's authenticated identity (or until he manually log out). Of course, your users data table must include the Remember_token column of a string type? Stores the identity of "Remember Me".
if (auth::attempt ([' email ' = = $email, ' password ' + $password], $remember) } {//The user is being remembered ...}
If you use the "Remember Me" feature, you can use the Viaremember method to determine if a user has a "Remember Me" cookie? To determine the user authentication:
if (Auth::viaremember ()) { //}
3. Determine if the user has verified
To determine if a user is logged in, you can use the check method:
if (Auth::check ()) { //the user is logged in ...}
4. Only verify not logged in
The Validate method allows you to verify user credential information without actually logging on to the app
if (Auth::validate ($credentials)) { //}
5. Log in to a user within a single request
You can also use the once method to allow users to log in within a single request. There will be no session or cookie generated:
if (Auth::once ($credentials)) { //}
6. User Instance Login
If you need to log an existing user instance to the application, you can call the login method and pass in the user instance:
Auth::login ($user);
Safe exit
Of course, suppose you use the Laravel-built authentication controller, presets provide a way for users to log out.
Auth::logout ();
Get authenticated User Instances
1. Obtained from Auth facade
2. Illuminate\http\request instances obtained
User ()) {//$request->user () returns an instance of the authenticated user ...}} }
3. Use illuminate\contracts\auth\authenticatable Contract type hints
!--? php namespace App\http\controllers;use illuminate\routing\controller;use illuminate\ Contracts\auth\authenticatable;class Profilecontroller extends Controller {/** * Update the user s profile. * * @retur N Response */Public Function updateprofile (authenticatable $user) {//$user are an instance of the authenticate D user ...}} Reference Address: Http://laravel-china.org/docs/5.0/authentication