douxiong5972 2018-07-31 02:28
浏览 92

使用laravel 5.6检查中间件中的用户身份验证

I'm trying to create a middleware in Laravel 5.6 that checks if the user is authenticate or not. My middleware :

checklogin.php
<?php

namespace App\Http\Middleware;

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

class checklogin
{

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {

            return redirect('/welcome');
        }

        return $next($request);
    }


}

I added this in kernel.php in protected $routeMiddleware :

'checklogin' => \App\Http\Middleware\checklogin::class,

I also added it on router :

Route::get('/login','loginController@index');
Route::post('/welcome','loginController@store')->middleware('checklogin');

and also used

use App\Http\Middleware\checklogin;

My loginController is :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use Session;
use Illuminate\Support\Facades\Redirect;
session_start();

class loginController extends Controller
{

    public function index()
    {
        return view('/login');
    }

     public function store(Request $request)
    {


        $name = $request->name;
        $result = DB::table('tbl_login')
                 ->where('name',$name)
                 ->first();
                 if ($result) {
                     Session::put('name',$result->name);
                     Session::put('id',$result->id);
                     return Redirect::to('/welcome');
                 }
                 else{
                    Session::put('messege','invalid email or password');
                    return Redirect::to('/login');
                 }
    }

And my login.blade.php is :

<p class="alert-danger">
    <?php 
        $messege = Session::get('messege');
        if ($messege) {
            echo "$messege";
            Session::put('messege',null);
        }
    ?>
</p>

<form method="POST" action="{{url('/welcome')}}">                        
@csrf


    <label for="name" >Name</label>
         <input id="name" type="text" name="name" value="" required autofocus>                                           
            <button type="submit" class="btn btn-primary">
                login
            </button>

</form>

But it shows this error message :

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

I am new to laravel. I am just started learning. I dont know where is the problem. Please help!

  • 写回答

1条回答 默认 最新

  • doumeng9188 2018-07-31 04:43
    关注

    it seems ok but check this line

    if (Auth::guard($guard)->check()) {
       return redirect('/login');
    }
    

    you cant redirect POST route, you have to redirect only GET route you can use /login or any other routes

    or create another route in GET

    Route::get('/welcome','loginController@home')->middleware('checklogin');
    
    public function home()
    {
        return view('/home');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题