Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel外部身份验证_Php_Laravel_Authentication_Laravel 5.4 - Fatal编程技术网

Php 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

我通过一个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';
}

不确定这是否是测试更改,但您没有使用$Username,而是搜索用户
thomc
。另一件需要注意的事情是检查数据库中是否存在用户名为的用户,否则返回的$user将为null。请尝试使用id登录

$username = $response['username'];
$user = User::where('username', $username)->firstOrFail();
Auth::loginUsingId($user->id);
此外,还应确保所有路由都使用
web
中间件包装,并根据需要在路由上使用适当的
auth
guest
中间件