简体   繁体   中英

Laravel 5.4 redirect to login if unauthenticated

I have a custom login page for my laravel app. However, i have modified my Handler.php to first look like this

protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        return redirect()->guest('login');
    }

and even tried like this

protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        return redirect('login');
    }

However, once I get to login, i get this error

在此处输入图片说明

How can i fix this problem?.

This worked for me

in the web.php

Route::get('login', 'Auth\LoginController@login');

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

in the login controller

public function login(){
        return view('login-register');
    }

in the handler.php

protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

      return redirect()->guest('login');
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM