Laravel User authentication System-Manual authentication user

Source: Internet
Author: User
Tags auth

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 ');}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.