[英]Auth::login() is not working in Laravel 8
Auth::login()
不起作用。 在做这个项目之前,我做了另一个,然后它就成功了。 这是用户 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;
}
这是我的 controller 用于登录:
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 重定向欢迎路由,但欢迎路由再次将其重定向到登录页面。 路由欢迎具有中间件身份验证:
Route::get('/', function () {
return view('welcome');
})->name('welcome')->middleware('auth');
如果您有解决方案,请告诉我!
正如唯一的托马斯在评论中写道:
尝试为自己创建一个较小的示例,看看哪里开始不再起作用。 我建议停止像在loggedIn 中那样回显,因为这会破坏Laravel 中的一些功能。 例如,因为: >请记住,必须在发送任何实际 output 之前调用 header()
我的错误,我在header()
function 之前写了echo
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.