繁体   English   中英

如何将 oauth2 身份验证重定向到前端,而不是使用护照在后端重定向?

[英]How redirect oauth2 authentification to frontend instead of redirecting in the backend using passport?

我使用 Vue 和 Node 以及护照通过本地和 oauth2 (passport-github) 策略进行身份验证。

我的本地策略有效,但我的 oauth2 存在问题。

我的登录页面中有一个“使用 github 登录”按钮,单击它时会向我的后端 (http://localhost:3000/auth/github) 发出 axios 请求。

const api = axios.create({
    baseURL: 'http://localhost:3000/',
    withCredentials: true,
    timeout: 1000,
});

async function APIloginWithGitHub() {
    const response = await api.get('/auth/github')
    return response.data
}
router.get('/github', passport.authenticate('github'))

router.get('/auth/github/callback', 
  passport.authenticate('github'),
  function(req, res) {
    res.send('You have been successfully authenticated!');
  });

在后端的中间件级别,我使用以下策略配置:

const GitHubStrategy = require('passport-github').Strategy;
const User = require('../models/User')

module.exports = function (passport) {
  passport.use(new GitHubStrategy({
    clientID: process.env.GITHUB_APP_ID,
    clientSecret: process.env.GITHUB_APP_SECRET,
    callbackURL: "http://localhost:3000/auth/github/callback"
  },
    async (token, tokenSecret, profile, done) => {
      console.log(profile)
    }
  ));
};

这会重定向到后端的github认证页面,涉及问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM