php - Laravel 5.2: After login the app lost session -
i'm trying fix 1 entire day, searching many times , many ways in google.
after enter username , password 401 unauthorized, in /storage/framework/session file created.
login page parts
<meta name="csrf-token" content="{{ csrf_token() }}"> ... <form class="login-form" action="admin/login" method="post"> <h3 class="form-title">access data</h3> <div class="alert alert-danger display-hide"> <button class="close" data-close="alert"></button> <span> username or password invalid. </span> </div> <div class="form-group"> <!--ie8, ie9 not support html5 placeholder, show field title that--> <label for="username" class="control-label visible-ie8 visible-ie9">username</label> <div class="input-icon"> <i class="fa fa-user"></i> <input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="username" name="username"/> </div> </div> <div class="form-group"> <label for="password" class="control-label visible-ie8 visible-ie9">password</label> <div class="input-icon"> <i class="fa fa-lock"></i> <input class="form-control placeholder-no-fix" type="password" autocomplete="off" placeholder="password" name="password"/> </div> </div> <div class="form-actions"> <label class="checkbox"> <input type="checkbox" name="remember" value="1"/> remember me </label> <button type="submit" id="submit" class="btn blue pull-right"> login <i class="m-icon-swapright m-icon-white"></i> </button> </div> <input type="hidden" name="_token" value="{{ csrf_token() }}" /> </form> ... <script> $('#submit').on('click', function (e) { e.preventdefault(); data = $('form').serialize(); $.ajax({ 'method': 'post', 'url': 'admin/login', 'data': data, 'datatype': 'json', 'success': function (data) { if (data.type === 'redirect') { window.location.href = 'admin/dashboard'; } else { console.log(data); } } }); }); </script> in end of jquery.min file
$.ajaxsetup({ headers: { 'x-csrf-token': $('meta[name="csrf-token"]').attr('content') } }); route.php
/* |-------------------------------------------------------------------------- | routes file |-------------------------------------------------------------------------- | | here register of routes in application. | it's breeze. tell laravel uris should respond | , give controller call when uri requested. | */ route::get('/', function () { return view('welcome'); }); /* |-------------------------------------------------------------------------- | application routes |-------------------------------------------------------------------------- | | route group applies "web" middleware group every route | contains. "web" middleware group defined in http | kernel , includes session state, csrf protection, , more. | */ route::group(['middleware' => ['web']], function () { //route::group(['prefix' => 'admin'], function() { route::get('admin', ['uses' => 'userscontroller@getlogin']); route::post('admin/login', ['uses' => 'userscontroller@dologin']); route::post('admin/logout', ['uses' => 'userscontroller@dologout']); //}); }); route::group(['middleware' => ['web', 'auth']], function () { //route::group(['prefix' => 'admin'], function() { route::get('admin/dashboard', function() { return view('admin/dashboard'); }); //}); }); userscontroller.php
<?php namespace app\http\controllers; use request, validator, redirect, hash, auth; use illuminate\support\facades\input; use app\models\usersauth; use app\models\usersmodel; class userscontroller extends controller { function getlogin() { return view('admin/index'); } function dologin() { $validator = validator::make(input::all(), [ 'username' => 'required', 'password' => 'required|alphanum|min:5' ]); if($validator->fails()) { /*...*/ } else { if(auth::attempt(['username' => input::get('username'), 'password' => input::get('password'), 'active' => '1'])) { if(auth::check()) { return json_encode(['type' => 'redirect']); } } else { return json_encode(['type' => 'danger', 'msg' => 'username or password invalid.']); } } } } auth.php
<?php return [ /* |-------------------------------------------------------------------------- | authentication defaults |-------------------------------------------------------------------------- | | option controls default authentication "guard" , password | reset options application. may change these defaults | required, they're perfect start applications. | */ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], /* |-------------------------------------------------------------------------- | authentication guards |-------------------------------------------------------------------------- | | next, may define every authentication guard application. | of course, great default configuration has been defined | here uses session storage , eloquent user provider. | | authentication drivers have user provider. defines how | users retrieved out of database or other storage | mechanisms used application persist user's data. | | supported: "session", "token" | */ 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], /* |-------------------------------------------------------------------------- | user providers |-------------------------------------------------------------------------- | | authentication drivers have user provider. defines how | users retrieved out of database or other storage | mechanisms used application persist user's data. | | if have multiple user tables or models may configure multiple | sources represent each model / table. these sources may | assigned authentication guards have defined. | | supported: "database", "eloquent" | */ 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => app\models\usersauth::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], /* |-------------------------------------------------------------------------- | resetting passwords |-------------------------------------------------------------------------- | | here may set options resetting passwords including view | password reset e-mail. may set name of | table maintains of reset tokens application. | | may specify multiple password reset configurations if have more | 1 user table or model in application , want have | separate password reset settings based on specific user types. | | expire time number of minutes reset token should | considered valid. security feature keeps tokens short-lived | have less time guessed. may change needed. | */ 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ], ]; usersauth.php
<?php namespace app\models; use illuminate\foundation\auth\user authenticatable; class usersauth extends authenticatable { /** * users db table. * * @var string */ protected $table = '002'; /** * url redirect after login. * * @var string */ protected $redirectto = 'admin/dashboard'; /** * attributes mass assignable. * * @var array */ protected $fillable = [ 'username', 'email', 'password', ]; /** * attributes excluded model's json form. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; } and laravel.log empty ('debug' => env('app_debug', true)).
thanks lot helping me.
after 2 days searching solution think in changing database columns default name id, username, password, etc (before using 002_id, 002_username, 002_password, etc...)... after changes working fine.
in laravel dosen't have option use custom fields without mod base auth class? o.o , errors without information? "tokenmismatchexception"...
Comments
Post a Comment