2

Hi I am having some problem with authentication in laravel. I have to use two middleware 1. is web and 2. auth . I am using web middleware so that I can use session to show flash messages. and want to use auth middleware to do authentication of users/admin. but I am facing some problems.

below is my function to check authorization and to redirect to their respective routes

  public function postLoginForm(){

    $email=Input::get('email');
    $password=Input::get('password');

    $data=[
        'email'=>$email,
        'password'=>$password
    ];
    $rules=[

        'email'=>'required',
        'password'=>'required'

    ];

    $validator=Validator::make($data,$rules);

    if($validator->fails()){

        Session::flash('fail', 'Oops Something went wrong!!');
        return redirect()->back()->withErrors($validator);

    }
    else{



        if(Auth::attempt($data)){




            $checkStatus=User::select('*')->where('email',$email)->first();

            Session::put('email',$checkStatus->email);
            Session::put('user_type',$checkStatus->user_type);




            if($checkStatus['user_type']=='4'){
                if($checkStatus['status']=='0'){
                    Session::flash('wait', 'Registration is not approved!!');

                    return "student";
                    return redirect()->back();
                }
                else{

                    return "student else";

                    return Redirect::route('get.student.dashBoard');

                }
            }
            else if($checkStatus['user_type']=='1'){


                return Redirect::route('get.admin.dashBoard');

            }
            else if($checkStatus['user_type']=='2'){


                return 'admin sir view';

                return Redirect::route('get.admin.dashBoard');
            }
            else if($checkStatus['user_type']=='3'){



                return 'admin other view';


                return Redirect::route('get.admin.dashBoard');
            }
            else{
                Session::flash('fail', 'Oops Something went wrong!!');
                return redirect()->back();
            }

        }
        else{
            Session::flash('fail', 'Login details not matched!!');
            return redirect()->back();
        }

    }
    return 'nothing works';

}

below is my routes for admin

    Route::group(['middleware' => ['web']], function () {

Route::get('/login',
    ['as' => 'get.login.page',
        'uses' => 'LoginController@getLoginPage']);

Route::post('/login-done',
    ['as' => 'post.login.page',
        'uses' => 'LoginController@postLoginForm']);


Route::get('/register',
    ['as' => 'get.register.page',
        'uses' => 'LoginController@getRegisterPage']);

Route::post('/register',
    ['as' => 'post.register.form',
        'uses' => 'LoginController@postRegisterForm']);

Route::get('/forgot-password',
    ['as' => 'get.forgotPassword.form',
        'uses' => 'LoginController@getForgotPasswordForm']);



      Route::group(['middleware' => ['auth']], function () {


    Route::get('/admin-dashboard',
        ['as' => 'get.admin.dashBoard',
            'uses' => 'admin\PageController@getAdminDashboard']);


    Route::get('/all-achievements',
        ['as' => 'get.achievements',
            'uses' => 'admin\AchievementsController@getAchievementsList']);

    Route::get('/new-achievement',
        ['as' => 'get.add.achievement',
            'uses' => 'admin\AchievementsController@getAddAchievement']);

    Route::post('/add-achievement',
        ['as' => 'post.achievementsForm',
            'uses' => 'admin\AchievementsController@postAchievements']);


    Route::get('remove-achievement/{achie_slug}',
        ['as' => 'post.delete.achievements',
            'uses' => 'admin\AchievementsController@postDeleteAchievement']);

    Route::get('edit-achievement/{achie_slug}',
        ['as' => 'get.edit.achievements',
            'uses' => 'admin\AchievementsController@getEditAchievement']);

    Route::post('update-achievement/{ach_id}',
        ['as' => 'post.edited.achievement',
            'uses' => 'admin\AchievementsController@postEditedAchievement']);


    Route::get('/all-news',
        ['as' => 'get.news.list',
            'uses' => 'admin\NewsController@getNewsList']);


    Route::get('/add-news',
        ['as' => 'get.add.news',
            'uses' => 'admin\NewsController@getAddNews']);


    Route::post('/add-news',
        ['as' => 'post.add.news',
            'uses' => 'admin\NewsController@postAddNews']);

    Route::get('/delete-news/{news_slug}',
        ['as' => 'get.delete.news',
            'uses' => 'admin\NewsController@postDeleteNews']);

    Route::get('/edit-news/{news_slug}',
        ['as' => 'get.edit.news',
            'uses' => 'admin\NewsController@getEditNews']);


    Route::post('/edit-news/{news_slug}',
        ['as' => 'post.edited.news',
            'uses' => 'admin\NewsController@postEditedNews']);




    Route::get('/all-admins',
        ['as' => 'get.admin.list',
            'uses' => 'admin\AdminController@getAllAdminList']);

    Route::get('/add-admin',
        ['as' => 'add.new.admin',
            'uses' => 'admin\AdminController@getAddNewAdmin']);

    Route::post('/add-new-admin',
        ['as' => 'post.add.new.admin',
            'uses' => 'admin\AdminController@postAddNewAdmin']);


    Route::get('/all-schedule',
        ['as' => 'get.timeTable.list',
            'uses' => 'admin\TimeTableController@getTimeTableList']);

    Route::get('/add-schedule/{id}',
        ['as' => 'add.timeTable',
            'uses' => 'admin\TimeTableController@getAddNewBatch']);

    Route::post('/add-new-batch',
        ['as' => 'add.newBatch',
            'uses' => 'admin\TimeTableController@postAddNewBatch']);

    Route::post('/save-year-batch',
        ['as' => 'save.year.batch',
            'uses' => 'admin\TimeTableController@postSaveYearBatch']);

    Route::get('/schedule-table/{year}',
        ['as' => 'view.schedule.table',
            'uses' => 'admin\TimeTableController@getScheduleTable']);

    Route::get('/delete-schedule/{slug}',
        ['as' => 'delete.schedule.one',
            'uses' => 'admin\TimeTableController@postDeleteOneSchedule']);

    Route::get('/edit-schedule/{slug}',
        ['as' => 'edit.schedule.one',
            'uses' => 'admin\TimeTableController@getEditScheduleForm']);

    Route::post('/save-edited-schedule/{id}',
        ['as' => 'save.edited.schedule',
            'uses' => 'admin\TimeTableController@postEditScheduleForm']);




    Route::get('/all-results',
        ['as' => 'get.all.results',
            'uses' => 'admin\ResultsController@getAllResults']);

    Route::get('/add-result',
        ['as' => 'get.add.results',
            'uses' => 'admin\ResultsController@getAddResult']);

    Route::post('/add-new-result',
        ['as' => 'post.add.result',
            'uses' => 'admin\ResultsController@postAddResult']);

    Route::get('/delete-result/{id}',
        ['as' => 'get.delete.student.result',
            'uses' => 'admin\ResultsController@getDeleteResult']);

    Route::get('/edit-result/{id}',
        ['as' => 'get.edit.student.result',
            'uses' => 'admin\ResultsController@getEditResult']);


    Route::post('/save-edited-result/{id}',
        ['as' => 'post.edited.result',
            'uses' => 'admin\ResultsController@postEditedResult']);


    Route::get('/contact-messages',
        ['as' => 'get.contact.message',
            'uses' => 'admin\ContactMessageController@getAllContactMessages']);


    Route::get('/contact-messages/{id}',
        ['as' => 'get.delete.contact.message',
            'uses' => 'admin\ContactMessageController@getDeleteContactMessages']);


});

  });

every time i try to login it redirects me to the same login page. please guide me whats wrong with this.

thedudecodes
  • 1,479
  • 1
  • 16
  • 37
  • Remove one of them =>return 'admin other view'; =>return Redirect::route('get.admin.dashBoard'); When it return "admin Other view", than the control is gone, and it will not execute second 'return' line – Muhammad Sadiq Nov 02 '16 at 08:00

1 Answers1

0

You should remove web middleware from middleware group to make it work. It applies to all routes inside web.php (5.3) and routes.php (5.2.27 and higher) automatically and if you'll add it manually, it will break session related functionality.

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • My Laravel Framework version 5.2.45 . when I remove the web middleware it doesn't allow me to use Session:flash to show flash messages. – thedudecodes Nov 02 '16 at 06:45
  • @pawankumar, if your routes are in `routes.php`, you should remove `web` middleware. This middleware applies automatically to all routes since `5.2.27`. Please click the link in the answer above. – Alexey Mezenin Nov 02 '16 at 06:47
  • suppose if some of my routes doesn't need auth . ?? – thedudecodes Nov 02 '16 at 06:48
  • sir i Have tried but removing web middle ware didn't worked . it showing me some errors of error variable {{ $errors->first('password') }} when i use like this. – thedudecodes Nov 02 '16 at 06:58
  • @pawankumar, well you should remove the middleware anyway and find out why are you getting this error message. If you post some related information, we'll be able to help you. – Alexey Mezenin Nov 02 '16 at 07:00