简体   繁体   中英

Auth::login() is not working in Laravel 8

Auth::login() is not working. Before doing this project I did another one then it was worked. Here is User Model

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements Authenticatable
{
    use \Illuminate\Auth\Authenticatable;

    use HasFactory;
}

Here is my controller for login:

function loggedIn(Request $request){
       echo $name = $request['name'];
        $password = $request['password'];
        $user = User::where([
            ["name", $name],
            ["password", $password],
        ])->get()->first();

        if ($user != ''){
            Auth::login($user);
            return redirect()->route('welcome');

        }else{
            return redirect()->back()->with(['error'=>'Ismingiz tasdiqlanmadi!']);
        }

    }

Controller redirecting welcome route but welcome route again redirecting it to the login page. Route welcome has middleware auth:

Route::get('/', function () {
    return view('welcome');
})->name('welcome')->middleware('auth');

If you have a solution, please inform me!

As onlie Thomas wrote on comment:

try to create a smaller example for yourself and see where things start to no longer work. I would suggest stop echo-ing like in loggedIn because this disrupts some functions in Laravel. for example bacause: >Remember that header() must be called before any actual output is sent

My mistake, I have wrote echo , before header() function;

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