Php Laravel外部身份验证
我通过一个restful API进行身份验证,如果验证成功,它会给我用户名,我会接受这个用户名并尝试登录到我的Laravel应用程序。当我完成登录时,它似乎工作了。然而,当我在网站上浏览时,它似乎并没有保持会话,我仍然被认证为访客Php Laravel外部身份验证,php,laravel,authentication,laravel-5.4,Php,Laravel,Authentication,Laravel 5.4,我通过一个restful API进行身份验证,如果验证成功,它会给我用户名,我会接受这个用户名并尝试登录到我的Laravel应用程序。当我完成登录时,它似乎工作了。然而,当我在网站上浏览时,它似乎并没有保持会话,我仍然被认证为访客 public function login(){ // I do a guzzle request that gives me the value of $response['username'] $Username = $response['user
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';
}
不确定这是否是测试更改,但您没有使用$Username,而是搜索用户thomc
。另一件需要注意的事情是检查数据库中是否存在用户名为的用户,否则返回的$user将为null。请尝试使用id登录
$username = $response['username'];
$user = User::where('username', $username)->firstOrFail();
Auth::loginUsingId($user->id);
此外,还应确保所有路由都使用web
中间件包装,并根据需要在路由上使用适当的auth
和guest
中间件