简体   繁体   English

如何在Laravel 5.4中使用会话登录

[英]How to logged in with session in laravel 5.4

I am a newbie in laravel. 我是Laravel的新手。 I made a registration and logged in form. 我进行了注册并登录了表格。 The user can register himself but I am unable to create logged in form appropriately. 用户可以注册自己,但是我无法正确创建登录表单。 I want to able the user to logged in with session on correct information of username and password. 我希望用户能够使用正确的用户名和密码信息通过会话登录。

here is my controller: 这是我的控制器:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Auth;

class tuto extends Controller
{
    public function login(){
        return view('pages.login');
    }

    public function login_validation(Request $request){
        $this->validate($request, [
    'username' => 'required|min:3|max:10',
     'password' => 'required|min:6',

    ]);

    if(Auth::attempt(['name'=>$request->username,'password'=>$request->password])){
        return "true";
        }

    return 'false';
    }

}

I am getting false message even on correct username and password. 即使使用正确的用户名和密码,我也会收到错误消息。 here is my logged in page: 这是我的登录页面:

<form method="POST" action="/login" autocomplete="off">

        <input type="hidden" name="_token" value="{{ csrf_token() }}">


        <div class="row">
            <div class="col-md-offset-4 col-md-4">
                <div class="form-group">
                    <label for="username">Username:</label>
                    <input type="text" id="username" name="username" class="form-control" placeholder="Enter Username">
                </div>
            </div>



            <div class="col-md-offset-4 col-md-4">
                <div class="form-group">
                    <label for="password">Password:</label>
                <input type="password" id="password" name="password" class="form-control" placeholder="Enter Password">

                </div>
            </div>
        </div>



        <div class="form-group" align="center">
            <button class="btn btn-success">LogIn</button>

        </div>
        <div align="center"> Don't have an account ?  <a href="/register"> SignUp </a> </div>

</form>

also I want to create session on successful authentication. 我也想创建成功身份验证的会话。 any help would be highly appreciable. 任何帮助都是非常可观的。 Thank you 谢谢

laravel attempt , automatically create the session if the username and password correct, so instead of returning true just redirecting user to his dashboards. laravel attempt ,自动创建会话,这样,如果用户名和密码是否正确,而不是仅仅重定向真正的用户返回到他的仪表板。
you also must create the post route in web.php for that login_validation method. 您还必须在web.php中为该login_validation方法创建发布路线。 hope is work. 希望就是工作。

sorry, my speaking english is very weak. 抱歉,我的英语说得很弱。

To avoid these kind of errors. 为了避免这类错误。 Use laravel's build-in authentication system. 使用laravel的内置身份验证系统。

1.create new laravel app laravel new my-project 1.创建新的laravel应用程序laravel new my-project

2.cd into my-project and type php artisan make:auth 2.cd进入my-project并输入php artisan make:auth

This will create a basic authentication system with all login and registration routes, controllers and views 这将创建具有所有登录和注册路线,控制器和视图的基本身份验证系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM