0

我正在尝试将 Nodejs 后端 api 与 Angular 集成。我已经在后端应用程序上实现了 passpor-google、passport-github、passport-twitter 策略。

我的后端应用程序网址是http://localhost:3000 我的前端应用程序网址是http://localhost:4200

护照路线

authRouter.get('/github', passport.authenticate('github'));
authRouter.get(
  '/github/callback',
  passport.authenticate('github', {
    failureRedirect: 'http://localhost:4200/login',
  }),
  authController.sendJWTToken
);

当我从前端应用程序发送请求时,它给了我 CORS 错误

身份验证服务.ts

githubAuth(): Observable<LoginRsp> {
    return this.httpClient.get<LoginRsp>(
      `${environment.api_url}/auth/github`
    );
  }

我已经在我的 Nodejs 项目中添加了 CORS。我该如何解决这个问题请帮助我

4

4 回答 4

0

在 AuthService.ts 中只需使用 window.location.href='{environment.api_url}/auth/github'

于 2018-07-10T07:01:00.010 回答
0

你可以像下图那样做这个

于 2018-05-14T09:55:56.620 回答
0

你可以简单地写在你的html模板中

<a href="http://localhost:3000/login/google">Google</a>

在后端你只需要拥有这个

app.get('/login/google',  passport.authenticate('google', { scope: ['profile','email']}) );
于 2020-02-04T12:59:00.687 回答
0

使用 cors 模块cors 或者使用 express cors 中间件express cors

app.use(cors());在此之后任何其他路线

const users = require('./routes/users');

或尝试将 cors 中间件移到顶部

于 2018-05-13T15:27:04.663 回答