SSL正在生产中阻止Passport登录,可在localhost上完全使用

时间:2019-07-05 15:59:04

标签: node.js ssl https passport.js cloudflare

我已经使用Passport构建了一个小型身份验证应用程序。它在localhost上正常工作,顺便说一下,这是不安全的。但是在我的域中,使用Cloudflare SSL不确定发生了什么,我无法使用Google和GitHub策略达成共识。我对ssl证书了解不多,想知道服务器上哪一部分出了问题。

我在两种策略中都添加了app.set('trust proxy'),app.use(cors()),并设置了'proxy:true'。

这是我域https://authapp.mambaoro.com中的代码和实时代码的相关部分

在index.js中

app.set('trust proxy', 1);
app.use(cors());
app.use(
session({
secret: process.env.SESSION_KEY_1,
proxy: true,
saveUninitialized: false,
resave: false,
maxAge: 6.048e8,
}),
);
app.use(passport.initialize());
app.use(passport.session());
app.use('/auth', authRoutes);

app.get('/getUser', (req, res) => {
 if (!req.user) {
  return res.send({ isAuthenticated: false });
 }
 res.send(req.user);
});

在passport-setup.js中

passport.use(
new GoogleStrategy(
{
  clientID: process.env.GOOGLE_CLIENT_ID,
  clientSecret: process.env.GOOGLE_CLIENT_SECRET,
  callbackURL: process.env.GOOGLE_CALLBACK_URL,
  proxy: true,
},
async (accesToken, refreshToken, profile, done) => {
  try {
    await User.sync();
    const newUser = await User.findOrCreate({
      where: {
        googleId: profile.id,
        username: profile.displayName,
        profileImageUrl: profile.photos[0].value,
      },
    });
    done(null, newUser[0]);
  } catch (e) {
    done(e);
      }
    },
   ),
  );

 passport.use(
  new GithubStrategy(
   {
    clientID: process.env.GITHUB_CLIENT_ID,
    clientSecret: process.env.GITHUB_CLIENT_SECRET,
    callbackURL: process.env.GITHUB_CALLBACK_URL,
    proxy: true,
   },
  async (accessToken, refreshToken, profile, done) => {
   try {
    await User.sync();
    const newUser = await User.findOrCreate({
      where: {
        githubId: profile.id,
        username: profile.username,
        profileImageUrl: profile.photos[0].value,
      },
    });
    done(null, newUser[0]);
  } catch (e) {
    done(e);
      }
    },
  ),
);

在auth-routes.js中

// auth with google
router.get(
 '/google',
 passport.authenticate('google', {
  scope: ['profile'],
 }),

);

// auth with github
router.get('/github', passport.authenticate('github', { scope: 
 ['profile'] }));

在index.js中,在具有React的客户端上,如果存在cookie,则以下代码用于通过Passport获取用户详细信息。

function SignUser() {
 const [user, setUserData] = useState(null);
 const [isAuthenticated, setIsAuth] = useState(false);
 useEffect(() => {
  const getUser = async () => {
  const res = await axios.get('/getUser');
  res.data.user && setUserData(res.data.user);
  res.data.isAuthenticated && setIsAuth(true);
 };
 getUser();
 }, []);
 ...

0 个答案:

没有答案