Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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
从第三方API对Laravel进行基本身份验证_Laravel_Rest_Basic Authentication - Fatal编程技术网

从第三方API对Laravel进行基本身份验证

从第三方API对Laravel进行基本身份验证,laravel,rest,basic-authentication,Laravel,Rest,Basic Authentication,我现在正在开发一个web应用程序。这个应用程序使用的是Laravel框架,应该通过另一个已经存在的REST-API获取每个数据,以及关于经过身份验证的用户的信息。此REST-API使用基本身份验证作为确认身份验证的方法 到目前为止,我所做的是以下代码: function userLogin(){ //get username and password from login form $username = $_POST["username"]; $password =

我现在正在开发一个web应用程序。这个应用程序使用的是Laravel框架,应该通过另一个已经存在的REST-API获取每个数据,以及关于经过身份验证的用户的信息。此REST-API使用基本身份验证作为确认身份验证的方法

到目前为止,我所做的是以下代码:

function userLogin(){

    //get username and password from login form
    $username = $_POST["username"];
    $password = $_POST["password"];

    //create a new Guzzle client
    $client = new Client();

    //send username and password to REST API to get the Authentication
    $response = $client->get('localhost:8080/api/user/login', [
        'auth' => [$username, $password]
    ]);

    if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
        return back()->with('danger','Login failed.');
    }else{
        return view('home');
    }
}
函数userLogin(){
//从登录表单获取用户名和密码
$username=$_POST[“username”];
$password=$_POST[“password”];
//创建一个新的Guzzle客户端
$client=新客户端();
//将用户名和密码发送到REST API以获取身份验证
$response=$client->get('localhost:8080/api/user/login'[
'auth'=>[$username,$password]
]);
如果($response->getStatusCode()<200 | |$response->getStatusCode()>=300){
return back()->带有('danger','Login failed');
}否则{
返回视图(“主视图”);
}
}

我设法登录,但当我点击菜单时,它将我带回登录页面,因为中间件不知道用户登录了

我还从我的同事那里得到一个角度代码,该代码在他的Ionic Mobile应用程序中成功管理了登录尝试:

login(user:User): Observable<any> {
const headers = new HttpHeaders(user ?
  {authorization:'Basic ' + btoa(user.username + ":" + user.password)}
  :
  {});

  /**
   * a simple temporary measure to counter the error we get
   * when we log in to the app. 
   * because the API sends nothing as response, nothing is assigned to the currentUser variable.
   * this one line only assign value of currentUser manually to localStorage.
   * it consists only of username and password.
   */
  localStorage.setItem('currentUser', JSON.stringify(user));

  /**
   * as of now these do nothing, because the API sends out nothing as response.
   */
  return this.http.get<any>(API_URL + "login", {headers: headers})
  .pipe(map(response => {
    if(response){
      localStorage.setItem('currentUser', JSON.stringify(response));
    }
    return response;
  }));
}
登录(用户:用户):可观察{
const headers=新的HttpHeaders(用户?
{授权:'Basic'+btoa(user.username+“:“+user.password)}
:
{});
/**
*一个简单的临时措施来抵消我们得到的错误
*当我们登录到应用程序时。
*由于API不发送任何响应,因此不会向currentUser变量分配任何内容。
*这一行仅将currentUser的值手动分配给localStorage。
*它只包含用户名和密码。
*/
localStorage.setItem('currentUser',JSON.stringify(user));
/**
*到目前为止,它们什么也不做,因为API没有发送任何响应。
*/
返回this.http.get(API_URL+“login”,{headers:headers})
.pipe(映射(响应=>{
如果(答复){
localStorage.setItem('currentUser',JSON.stringify(response));
}
返回响应;
}));
}

不幸的是,我不知道如何将其转换为PHP代码或Laravel函数。我希望有人能给我这个问题的解决方案。

在后端进行身份验证时,您必须应用中间件对用户进行身份验证,因此您必须在authenticate.php或任何其他自定义中间件中编写代码,如下所示:

try{

        $client = new \GuzzleHttp\Client();
        $url = "https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=".env("API_KEY");

        $request = $client->post($url, array(
            'form_params' => array(
                'idToken' => $token
            )
        ));
        //$response = $request->getBody();
        //$response = $request->send();
        $body = $request->getBody();

        if ($request->getStatusCode() !== 200) {
            $res['status'] = 401;
            $res['message'] = 'Error, Invalid domain.';
            return response($res);
        }
    }
    catch(\GuzzleHttp\Exception\ClientException $e) {
       log::info('catch========== Exception');
        $res['status'] = 401;
        $res['message'] = 'Error, Unauthorized.';
        return response($res);
    }

“我设法登录了,但当我点击菜单时”你正在开发RESTAPI并点击菜单?使用基本身份验证时,您应该始终发送标题。API为您提供了什么作为“登录”的回报?它必须还给你一些东西,即代币。否则,您将如何在其他API请求中证明您是谁?我忘了提到我没有开发REST API,它已经存在,我试图在我的web应用程序中使用它的身份验证。我的同事说,我应该将标题保存在LocalStorage中,但我还没有找到在Laravel中的LocalStorage中保存值的方法