lundi 25 décembre 2017

laravel5.4 I can't get login , when i try to login it always redirect to same login page

Web.php

Route::get('/' , ['as' => '/' , 'uses'=> 'loginController@getlogin']);
Route::post('/login', ['as' => 'login', 'uses'=> 'loginController@postlogin']);

Route::group(['middleware' =>['authen']],function (){
Route::get('/logout' ,['as'=>'logout', 'uses'=> 'loginController@getLogout']);
Route::get('/dashboard',['as'=>'dashboard', 'uses'=> 'dashboardController@dashboard']);});

dashboardController

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class dashboardController extends Controller
{
   public function __construct(){
    $this->middleware('web');
   }
   public function dashboard(){
    return view('layouts.master');
   }
}

loginController

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Auth;
class LoginController extends Controller{
     use AuthenticatesUsers;
     protected $username = 'username';
     protected $redirectTo = '/dashboard';
     protected $guard = 'web';

     public function getLogin(){
        if(Auth::guard('web')->check()){
           return redirect()->route('dashboard');
        }
        return view('login');
     }

     public function postLogin(Request $request){
         $auth = Auth::guard('web')->attempt([
            'username' => $request->username,
            'password' => $request->password,
            'active' => 1
          ]);

          if($auth){
             return redirect()->route('dashboard');
           }
           return redirect()->route('/');
     }

     public function getLogout(){
        Auth::guard('web')->logout();
        return redirect()->route('/');
     }
}

Authen.php

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authen
{
public function handle($request, Closure $next ,$guard ='web'){
    if (!Auth::guard($guard)->check()){
      return redirect()->route('/');   
    }
      return $next($request);
    }
}

CheckRole

<?php
namespace App\Http\Middleware;
use Closure;
class CheckRole
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
     public function handle($request, Closure $next){
         $roles = $this->getRequiredRoleForRoute($request->route());
         if($request->user()->hasRole($roles) || !$roles){
            return $next($request);
         }
          return redirect()->route('noPermission');
      }

      private function getRequiredRoleForRoute($route){
          $actions = $route->getAction();
          return isset($actions['roles']) ? $actions['roles'] : null;     
      }
}

when i try to login it redirect to the same page i.e login page , i spent lots of hour to solve this problem but i can't please someone help me . i want to redirect dashboard through login page . but it is not happen . there is no error shown and i cant go on dashboard page too.Please help me to solve this problem. Thanks so much.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire