微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

角度和护照谷歌身份验证

如何解决角度和护照谷歌身份验证

我第一次使用passport.js,我尝试使用谷歌策略passport-google-oauth2创建登录,到目前为止我的节点代码如下所示:

passport.config

import passport,{ Profile } from 'passport';
import { Strategy } from 'passport-google-oauth2';

const googleStrategy = new Strategy(
  {
    clientID: process.env.GOOGLE_CLIENT_ID,clientSecret: process.env.GOOGLE_CLIENT_SECRET,callbackURL: '/api/v1/auth/google/callback',passReqToCallback: true,},(
    request: any,accesstoken: any,refreshToken: any,profile: Profile,done: any
  ) => {
    console.log(accesstoken);
    done(null,profile);
  }
);

passport.serializeUser((user,done) => {
  done(null,user);
});

passport.deserializeUser((user,user);
});

passport.use(googleStrategy);

auth.route

router.get(
  '/google',passport.authenticate('google',{
    session: false,scope: ['email','profile'],})
);

router.get('/google/callback',(req,res,next) => {
  passport.authenticate('google',(err,profile,info) => {
    if (err) {
      next(err);
      return;
    }
    if (!profile) {
      res.status(500);
      const error = new Error('Internal Server Error');
      next(error);
      return;
    }
    var responseHTML = '<html><head><title>Main</title></head><body></body><script>res = %value%; window.opener.postMessage(res,"*");window.close();</script></html>'
    responseHTML = responseHTML.replace('%value%',JSON.stringify({
    user: req.user
    }));
    res.status(200).send(responseHTML);
  })(req,next);
});

到目前为止,一切都很好,在我的 Angular 应用程序的客户端,我打开了用于节点 api 的窗口以重定向到 google auth,这样:

角度身份验证服务

loggingWithGoogle(): void {
    const popup = window.open(
      'http://localhost:3000/api/v1/auth/google','mywindow','location=1,status=1,scrollbars=1,width=800,height=800'
    );
    const listener = popup?.addEventListener('message',(message) => {
      console.log(message);
    });
    // return this.http.get(`${this.BASE_URL}/auth/google`);
  }

此时,一个新窗口打开,我可以登录谷歌帐户,登录后窗口应该关闭,我应该在我的 angular 应用程序上获取数据,但窗口永远不会关闭,我收到此错误控制台 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'" 中的消息。登录后如何获取数据。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?