首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从角度调用passportjs(节点) API?

如何从角度调用passportjs(节点) API?
EN

Stack Overflow用户
提问于 2018-02-05 05:57:15
回答 1查看 993关注 0票数 1

NodeJs使用passportJS OAuth2配置并运行良好,但需求是角应该调用节点api,两者都运行在不同端口的中,从调用所有nodeJS的rest,使用proxy.conf.json运行良好,而从角度获取错误响应调用/googleauth/redirect

答复:

Failed to load https://accounts.google.com/o/oauth2/v2/auth?response_type=code... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

Google设置:

限制

授权的JavaScript来源:

代码语言:javascript
复制
http://localhost:4200
http://localhost:8088

授权重定向城市信息系统:

代码语言:javascript
复制
http://localhost:4200/api/googleauth/redirect
http://localhost:8088/api/googleauth/redirect

更新1: CORS到app.js (新的):但没有更改。

代码语言:javascript
复制
var cors = require('cors');
var app = express();
app.use(cors());
app.options('*', cors());
app.use('/api',cors(),require('./routes/api'));

更新2: api.js

代码语言:javascript
复制
router.get('/google', passport.authenticate('google', { scope: ['profile','email'] }));

护照设置

代码语言:javascript
复制
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20');
const userModel = require('../model/user');

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

passport.deserializeUser((id,done)=>{
  userModel.findById(id).then((user)=>{
    done(null,user);
  })
});

passport.use(
  new GoogleStrategy({
    callbackURL:'/api/googleauth/redirect',
    clientID:'',
    clientSecret:''
  },(accessToken,refreshToken,profile,done) =>{
    console.log("call back function fired");
    //console.log(accessToken);
    userModel.findOne({email:profile.emails[0].value,authType:'google'},function(err,user){
        if(user){
          done(null,user);
        }else{
          done(null,user);
        }
    });
  }))
EN

回答 1

Stack Overflow用户

发布于 2018-02-07 20:41:18

我用教程https://medium.com/@ahsan.ayaz/how-to-handle-cors-in-an-angular2-and-node-express-applications-eb3de412abef解决了这个问题

只是缺少细节..。在从‘@ BrowserXhr /http’导入{CustExtBrowserXhr}的行旁边,从‘./app/path- to /cust-ext-browser-xhr’导入{CustExtBrowserXhr};您应该从‘@LocationStrategy/公用’中添加导入{ LocationStrategy,HashLocationStrategy };

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48616610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档