It is very easy to implement user authentication in Laravel. In fact, almost everything has been configured for you. The configuration file is located in Config/auth.php, which contains an annotation-annotated option configuration that adjusts the behavior of the authentication service.
in its core code, Laravel's authentication component consists of guards and providers, Guard defines how users can authenticate in each request, for example, Laravel to maintain session storage status and Co through session Guard Okie
Provider defines how to get user information from a persistent store, laravel the bottom layer supports two ways to get users through eloquent and database query Builder, and you can define additional Provider if needed.
1. Fast Certification
Laravel with a set of code to handle user authentication. You only need to run the following command to quickly generate the routes and views required for authentication:
PHP Artisan Make:auth
Then you will see this set of login verification in your laravel frame and exit the login code.
2. Manually authenticate the user (write a simple)
first is the landing interface, must be careful not to drop the token
<form action= "Dologin" method= "POST" > <input type= "hidden" name= "_token"
value= "{{Csrf_token ()}}" >
<input type= "text" name= "name"/> <input type= "password" name= "password"/> <input "type="
Submit "value=" >
</form>
Then model, which can take the user.php generated in Auth, but pay attention to its own path and namespace problems. The model can be generated directly using the Artisan command. Note: If your model path changes, such as under admin instead of where you originally placed user.php, you will need to modify the path here in the config/auth.php file:
' Model ' => App\model\admin\user::class,
<?php
namespace App\model\admin;
Use Illuminate\foundation\auth\user as authenticatable;
Class User extends authenticatable
{
//
protected $fillable = [
' name ', ' email ', ' Password
', ];
/**
* The attributes that should is hidden for arrays.
*
* @var array
/protected $hidden = [
' password ', ' Remember_token ',
];
}
Finally, the controller, we want to use facades. Facades provides a "static" interface for the classes available in the application's service container. Laravel has a lot of facades and can be used to access all the services in Laravel. These callable programs are under: Vendor\laravel\framework\src\illuminate\support\facades.
<?php
namespace App\http\controllers;
Use Illuminate\http\request;
Use Illuminate\support\facades\auth;
Class Logincontroller extends Controller
{public
function login ()
{return
view (' login ');
}
Public Function Dologin (Request $request)
{
$name = $request->input (' name ');
$password = $request->input (' password ');
if (auth::attempt ([' Email ' => $name, ' password ' => $password])) {
//authentication passed ...
DD (' OK ');
} else{
DD (' ERROR ');}}