douxianji6104
douxianji6104
2018-05-15 05:18
浏览 59
已采纳

如何使用laravel 5.4中的会话登录

I am a newbie in 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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanlushen8940
    duanlushen8940 2018-05-15 05:26
    已采纳

    To avoid these kind of errors. Use laravel's build-in authentication system.

    1.create new laravel app laravel new my-project

    2.cd into my-project and type php artisan make:auth

    This will create a basic authentication system with all login and registration routes, controllers and views

    点赞 评论
  • douqian6194
    douqian6194 2018-05-15 05:40

    laravel attempt, automatically create the session if the username and password correct, so instead of returning true just redirecting user to his dashboards.
    you also must create the post route in web.php for that login_validation method. hope is work.

    sorry, my speaking english is very weak.

    点赞 评论

相关推荐