V
V
Vladislav Olegovich2019-04-18 19:34:15
Laravel
Vladislav Olegovich, 2019-04-18 19:34:15

Laravel: why redirect to login even after authorization?

When trying to log in or register, it redirects to the login, as seen in the picture below. 5cb8a364acbe5214429099.png
Here are the files involved in the authentication process:
1) Routes: web.php

Auth::routes();
Route::get('doc/', function () {
    return view('welcome');
});
Route::get('/confirm', function()
{
    return view('confirm');
})->name('confirm');
Route::post('/confirm', '[email protected]')->name('addworker');
Route::get('users/{id}', '[email protected]')->where('id', '[0-9]+')->name('account');
Route::get('/home', '[email protected]')->name('home');

2. Everything is standard in LoginController and RegisterController except redirectTO
protected function redirectTo()
    {
        return route('account',['id'=> auth()->user()->id]);   
    }

3. My custom middleware
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use App\Employee;
class CheckConfirm
{
    public function handle($request, Closure $next)
    {
        if(Auth::check())           //если пользователь авторизован
        {
            $id = Auth::id();        //сохраняем его id
            $empl = Employee::where('user_id','=', $id)->first();
            dump($empl);
            if($empl == null)// если запрос вернул null
            {
                return redirect()->route('confirm');                    //заставляем его конформится
            }
            else
            {                
                dump($empl);
                return $next($request);                     //то пропускаем пользователя дальше
            }
        }
        else
        {
            return redirect()->route('login');
        }
    }
}

4. Controller Methods EmployeeController.php
public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('confirmated');
    }
    public function show($id)
    {
        return view('account');
    }

I thought that creating the project again would save me, but apparently it didn’t save me, I was already desperate to fight this authentication, which is not clear why it breaks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NubasLol, 2019-04-18
@NubasLol

It's obvious that your middleware does this.

P
Prego666, 2019-04-26
@Prego666

Tried to output Auth::user() in the employer constructor?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question