Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.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
Angular 使用Nestjs进行社交登录。RESTAPI的问题_Angular_Express_Oauth 2.0_Passport.js_Nestjs - Fatal编程技术网

Angular 使用Nestjs进行社交登录。RESTAPI的问题

Angular 使用Nestjs进行社交登录。RESTAPI的问题,angular,express,oauth-2.0,passport.js,nestjs,Angular,Express,Oauth 2.0,Passport.js,Nestjs,我正在用Nestjs、Passport和oauth2实现Google登录。此外,我还尝试将我的NestJSRESTAPI应用程序与角度为10的前端连接起来 在我的auth.controller.js中,我有: @UseGuards(AuthGuard('google')) async googleAuth(@Req() req) {} @Get('google/redirect') @UseGuards(AuthGuard('google')) async googleLo

我正在用Nestjs、Passport和oauth2实现Google登录。此外,我还尝试将我的NestJSRESTAPI应用程序与角度为10的前端连接起来

在我的auth.controller.js中,我有:

  @UseGuards(AuthGuard('google'))
  async googleAuth(@Req() req) {}

  @Get('google/redirect')
  @UseGuards(AuthGuard('google'))
  async googleLoginRedirect(@Req() req: Request, @Res() res: Response): Promise<any> {

    // show current user
    console.log(req.user);

          return {
            statusCode: HttpStatus.OK,
            payload: req.user,
          };
  }


此外,我能够访问重定向页面的正确响应在浏览器中

但在curl中做同样的事情,我得到的反应是:

$ curl -v -H "content-type: application/json" http://127.0.0.1:3000/auth/google/redirect
*   Trying 127.0.0.1:3000...
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET /auth/google/redirect HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/7.71.1
> Accept: */*
> content-type: application/json
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Found
< Content-Security-Policy: default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests
< X-DNS-Prefetch-Control: off
< Expect-CT: max-age=0
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=15552000; includeSubDomains
< X-Download-Options: noopen
< X-Content-Type-Options: nosniff
< X-Permitted-Cross-Domain-Policies: none
< Referrer-Policy: no-referrer
< X-XSS-Protection: 0
< X-RateLimit-Limit: 100
< X-RateLimit-Remaining: 98
< Date: Sun, 30 May 2021 01:48:38 GMT
< X-RateLimit-Reset: 1622339539
< Location: https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fgoogle%2Fredirect&scope=email%20profile&client_id=604620607227-j25at9l5bjmqkdpfjoqb9ttu4b2v6tu9.apps.googleusercontent.com
< Content-Length: 0
< Connection: keep-alive
< Keep-Alive: timeout=5
< 
* Connection #0 to host 127.0.0.1 left intact

$curl-v-H“内容类型:应用程序/json”http://127.0.0.1:3000/auth/google/redirect
*正在尝试127.0.0.1:3000。。。
*连接到127.0.0.1(127.0.0.1)端口3000(#0)
>GET/auth/google/redirect HTTP/1.1
>主持人:127.0.0.1:3000
>用户代理:curl/7.71.1
>接受:*/*
>内容类型:application/json
> 
*将捆绑包标记为不支持多用途
找到
它不显示任何用户负载

为什么curl或Postman不响应用户数据?我对http等缺少什么或不理解什么

最后,从Angular发送http请求以获取用户数据和访问令牌的正确方式是什么


非常感谢您的时间。

如果它在浏览器中工作,而不是通过curl或postman,很可能是因为您已经登录到他们的服务,并且/或者它正在提交一个cookie以及帮助他们识别您的请求。因此,它们将负载重定向回您,而不要求您登录。如果没有上下文,他们会要求您先登录。谢谢您的回复。是的。我已登录,但如何确保访问令牌和用户配置文件在重定向页面中可用?通过添加express类型解决
Wed May 12 2021 21:16:10 GMT-0500 (Central Daylight Time) - [Request]  - {}
{
  email: 'jdoe@gmail.com',
  firstName: 'John',
  lastName: 'Doe',
  picture: 'https://lh3.googleusercontent.com/a/AATXAJzw91B2GNWHUkh6yzlwSmbZKQOmS0_NB-tAiViS=s96-c',
  accessToken: 'ya29.a0AfH6SMACc_xs2RSYB6vg5Kwl4-Nta_89RZUgHt9qP20bKpf2TRing2Pvtfo0tBu61og-CIYCSfJPe8CI36busf7I3smWBQ4gozTkloLc7o-uw-TjXSfb3PNdc3tOb3jvHqQTTlxCPq0tIa1yCTbuYEMLxR94'
}

$ curl -v -H "content-type: application/json" http://127.0.0.1:3000/auth/google/redirect
*   Trying 127.0.0.1:3000...
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET /auth/google/redirect HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/7.71.1
> Accept: */*
> content-type: application/json
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Found
< Content-Security-Policy: default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests
< X-DNS-Prefetch-Control: off
< Expect-CT: max-age=0
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=15552000; includeSubDomains
< X-Download-Options: noopen
< X-Content-Type-Options: nosniff
< X-Permitted-Cross-Domain-Policies: none
< Referrer-Policy: no-referrer
< X-XSS-Protection: 0
< X-RateLimit-Limit: 100
< X-RateLimit-Remaining: 98
< Date: Sun, 30 May 2021 01:48:38 GMT
< X-RateLimit-Reset: 1622339539
< Location: https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fgoogle%2Fredirect&scope=email%20profile&client_id=604620607227-j25at9l5bjmqkdpfjoqb9ttu4b2v6tu9.apps.googleusercontent.com
< Content-Length: 0
< Connection: keep-alive
< Keep-Alive: timeout=5
< 
* Connection #0 to host 127.0.0.1 left intact