我通过我验证的restful API进行身份验证,如果成功的话它会给我用户名,我会尝试登录我的Laravel应用程序。当我完成登录时,它似乎工作。但是,它似乎没有保持会话,因为我在网站上导航,我仍然作为访客进行身份验证。
public function login(){
// I do a guzzle request that gives me the value of $response['username']
$Username = $response['username'];
$user = User::where('username','thomc')->first();
Auth::login($user);
return view('home');
}
这是我的用户模型:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'UserDetails';
}
我的表:
答案 0 :(得分:0)
不确定是否进行了测试更改,但您未使用$ Username,而是搜索用户thomc
。需要注意的另一件事是检查数据库中是否存在具有用户名的用户,否则返回的$ user将为null。尝试使用id登录。
$username = $response['username'];
$user = User::where('username', $username)->firstOrFail();
Auth::loginUsingId($user->id);
还要确保所有路由都包含web
中间件,并根据需要在路由中包含相应的auth
和guest
中间件。