douruoshen1449 2018-11-22 11:48
浏览 130

Laravel会话在页面刷新时销毁

I have an admin panel that I protect with user authentication. Once I try to access it, it redirects me to the login form, which uses the 'guest' middleware (this is the expected behaviour). Once I attempt to log in it works like a charm, redirects me to the admin dashboard and the Auth object is created (I can retrieve the name). However, if I click on any link inside of the panel, or press F5, it redirects me back to the login form. For the sake of testing what happened, I removed the 'auth' middleware protection from the admin panel, and it gives me an error when I try to retrieve the Auth::user()->name property.

Trying to get property 'name' of non-object

So I am assuming that it destroys the Auth object on every page refresh, what I don't understand is where this behaviour comes from. I am using the default Auth middlewares, with the LoginController provided in the documentation (with some tweaks):

<?php

namespace App\Http\Controllers\Authentication;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    public function authenticate(Request $request){
        $email = $request->input('email');
        $password = $request->input('password');
        $remember = $request->input('remember');

        if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
            return redirect()->intended('dashboard');
        }
    }

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

All of the routes in the admin panel are inside a group in my routes file:

Route::middleware(['auth'])->group(function () {
    //All of the admin routes here
});

The login route is protected by the 'guest' middleware, so no authenticated user can access the login form.

Please ask me to post any code that you think could be helpful, I'm fairly new to Laravel and don't really know what to post.

  • 写回答

1条回答 默认 最新

  • dqhnp44220 2018-11-22 15:20
    关注

    I solved the problem. It was just a rookie mistake. I had the Auth::logout() function in the href property of a link, thinking it would trigger when someone clicked it. Instead, it logged me out as soon as the page loaded, that's why there was a session just the first time.

    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私