登录时中间件中的auth Guard检查false

时间:2019-04-18 08:31:09

标签: laravel laravel-5.2

问题是中间件出现问题,当从控制器重定向到中间件时,身份验证将变为false,而不是通过设置会话来重定向。但在控制器中,身份验证工作正常。

从控制器重定向到中间件时,实际上不会设置会话

如何在中间件中设置会话以设置身份验证。

SessionController:-

<?php

namespace PS\Http\Controllers\Auth;

use Illuminate\Http\Request;
use PS\Http\Requests;
use Auth;
use PS\Http\Controllers\Controller;
use PS\Eloquent\User;

class SessionsController extends Controller
{
    /**
     * Create a new sessions controller instance.
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Perform the login.
     *
     * @param  Request  $request
     * @return \Redirect
     */
    public function postLogin(Request $request)
    { 
        $this->validate($request, ['email' => 'required|email', 'password' => 'required']);
        if ($this->signIn($request)) { 
            $this->activateUser($request); /// activate user if it was deactivated 
            if($request->ajax())
            {
                return response()->json(["user logged in"],200);
            }
        //     $user = Auth::user()->id;
        // dd($user); die;
            return redirect('/');
        }
        if($request->ajax())
        { 
            return response()->json("Username or Password is wrong or user may not activated",422);
        }

        return redirect('login')->with('message','Username or Password is wrong or user may not activated');
    }
    /**
     * Destroy the user's current session.
     *
     * @return \Redirect
     */
    /**
     * Attempt to sign in the user.
     *
     * @param  Request $request
     * @return boolean
     */
    protected function signIn(Request $request)
    {
        return Auth::attempt($this->getCredentials($request), $request->has('remember'));
    }
    /**
     * Get the login credentials and requirements.
     *
     * @param  Request $request
     * @return array
     */
    protected function getCredentials(Request $request)
    {
        return [
            'email'    => $request->input('email'),
            'password' => $request->input('password'),
            'verified' => true
        ];

    }

    protected function activateUser(Request $request)
    {
        $user = User::where('email',$request->input('email'))->firstOrFail();
        if($user->deactivate == true)
        {
            $user->deactivate = false;
            $user->save();
        }
        return;

    }
}

RedirectIfAuthenticated.php middleware:-

<?php

namespace PS\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/');
        }

        return $next($request);
    }
}

routes.php

<?php

use PS\Eloquent\Job;

use PS\Eloquent\Feedback;

use Illuminate\Http\Request;

Route::get('/', function () {

    $fbs = Feedback::wherePublish(true)->orderBy('id','desc')->take(1)->get();

    return view('index',compact('fbs'));

});

Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
   return "Cache is cleared";
});

Route::get('/refer-to-friend', function () {

    return view('refer-to-friend');

});

Route::get('/testimonials', function () {

    $fbs = Feedback::wherePublish(true)->orderBy('id','desc')->take(1)->get();

    $feedback = Feedback::wherePublish(true)->orderBy('id','desc')->paginate(10);

    return view('testimonials',compact('feedback','fbs'));

});



Route::get('how-it-works', function(){

    $fbs = Feedback::wherePublish(true)->orderBy('id','desc')->take(1)->get();

    return view('how-it-works',compact('fbs'));

});

Route::get('our-expertise',function(){ 

    $fbs = Feedback::wherePublish(true)->orderBy('id','desc')->take(1)->get();

    return view('our-expertise',compact('fbs'));

});

Route::get('example', 'ProjectsController@Projects');

Route::get('example/websites', 'ProjectsController@Websites');

Route::get('example/mobile_apps', 'ProjectsController@MobileApps');

Route::get('why-choose-us', function(){

    return view('why-choose-us');

});

Route::get('faq', function(){

    return view('FAQ');

});

Route::get('terms-and-conditions', function(){

    return view('terms-and-conditions');

});

Route::get('privacy', function(){

    return view('privacy');

});

Route::get('careers', function(){

    $jobs = Job::orderBy('id', 'desc')->paginate(5);

    return view('careers', compact('jobs'));

});
Route::post('careers', 'ProcessController@SendCareerEmailAndFile');

Route::post('contact-us', 'ProcessController@ContactUs');

Route::post('share-project', 'ProcessController@ShareProject');

Route::get('price', function(){

    $fbs = Feedback::wherePublish(true)->orderBy('id','desc')->take(1)->get();

    return view('price',compact('fbs'));

});

Route::get('contact-us', function(){

    return view('contact-us');

});

Route::get('submit-order', function(){

    return view('submit-order');

});

Route::auth();

Route::post('/register', 'Auth\AuthController@register');

Route::get('/home', 'HomeController@index');

Route::get('register/confirm/{token}', 'Auth\AuthController@confirmEmail');

Route::get('password-reset', 'Auth\PasswordController@showResetForm');

Route::get('my-account', function(){

    return view('account/my-account');

});

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

    Route::resource('profile','ProfileController');

    Route::get("orders/first-or-last-order", "OrderController@firstOrLastOrder");

    Route::get("orders/next-or-previous-order/{order_id}", "OrderController@nextOrPreviousOrder");

    Route::resource("orders","OrderController",['except' => ['store']]);
    Route::post('orders/charge/{order_id}',"OrderController@chargeOrder");

    Route::get('/order/download-file', 'OrderController@download_file');

    Route::post('orders/accept-offer/{order_id}',['as'=>'order.accept-offer','uses'=>'OrderController@acceptOffer']);

    Route::resource("messages","MessageController");

    Route::get("/message/download-file",'MessageController@download_message_file');

    Route::resource("feedback","FeedbackController");

    Route::post('/order/enable-feedback/{order_id}',['as'=>'orders.enable-feedback','uses'=>'OrderController@enableFeedback']);

    Route::post('orders/activate/{order_id}',  ['as'=>'orders.activate', 'uses' => 'OrderController@activateOrder']);

    Route::post('payment', array(

    'as' => 'payment',

    'uses' => 'PaypalController@postPayment',

    ));

// this is after make the payment, PayPal redirect back to your site

    Route::get('payment/status', array(

        'as' => 'payment.status',

        'uses' => 'PaypalController@getPaymentStatus',

    ));

});

Route::post('orders',  ['as'=>'orders.store', 'uses' => 'OrderController@store']);

Route::post('login-user', 'Auth\SessionsController@postLogin');

Route::get('deactivate/{user_id}', 'Auth\AuthController@deactivateUserAccount');

Route::get('social/login/{provider}', 'Auth\SocialAuthController@redirectToProvider');

Route::get('social/callback/{provider}', 'Auth\SocialAuthController@handleProviderCallback');

Route::group(['prefix' => 'admin','middleware'=>['auth','PS\Http\Middleware\AdminMiddleware']], function() {

    Route::get('login', 'Admin\SessionController@getLogin');

    Route::post('login', 'Admin\SessionController@postLogin');

    Route::get('orders/search','Admin\ProcessOrderController@search');

    Route::post("orders/access/{order_id}", "Admin\ProcessOrderController@setOrderAccess");

    Route::post('orders/delete/{order_id}',"Admin\ProcessOrderController@deleteOrder");
    Route::post('orders/update/{order_id}',['as'=>'admin.orders.update', 'uses'=>'Admin\ProcessOrderController@update']);
    Route::get('orders/show/{order_id}',"Admin\ProcessOrderController@show");
    //////////////////////////////////
    Route::get("orders","Admin\ProcessOrderController@index");
    Route::get("orders/getdata","Admin\ProcessOrderController@getdata");
    Route::get("orders/storedata","Admin\ProcessOrderController@storedata");
    Route::get("messages/read","Admin\ProcessOrderController@readmessages");
    Route::post("messages/store","Admin\MessageController@store");
    ///////////////////////////////////
    Route::resource("messages","Admin\MessageController");

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

        Route::resource("jobs","Admin\JobController");

        Route::resource("assign-expert", "Admin\AssignExpertController",['only' => ['index', 'create','store','show']]);

        Route::post('assign-expert/send',"Admin\AssignExpertController@sendTask");

        Route::post('assign-expert/assign',"Admin\AssignExpertController@store");

        Route::get('feedback',"Admin\ProcessFeedbackController@index");

        Route::get('projects', 'Admin\ProjectsController@Index');

        Route::get('projects/create', 'Admin\ProjectsController@Create');

        Route::post('projects/store', 'Admin\ProjectsController@Store');

        Route::get('projects/delete/{id}', 'Admin\ProjectsController@destroy');

        Route::get('experts', 'Experts@View_Admin');        

        Route::post('experts/get_all', 'Experts@Action_Admin_GetAll');

        # EXPERTS - Xian    

        Route::get('experts/getdata', 'Experts@getdata');

        Route::get('students', 'Students@View_Admin');
        Route::get('students/getdata', 'Students@getdata');
        Route::get("students/storedata","Students@storedata");
        Route::get("studentmessages/read","Students@readmessages");


    });


});


Route::group(['prefix' => 'plagiarism'], function() {

    Route::get('register', function(){

        return view('auth.register');

    });

});
});

0 个答案:

没有答案