使用Passport FacebookvalidationApollo GraphQL API

我有一个客户端服务器和一个API,都使用阿波罗 。 当用户使用Facebooklogin时,我想向我的客户端服务器发送一个令牌,然后将这个令牌附加到对API所做的每个请求的头部。 没有使用会话。

下面是API的外观,当用户使用Facebooklogin(我也在后台检查数据库中的用户)时,这会成功生成令牌。

// API running on port 3010 app.use(expressJwt({ secret: authConfig.jwt.secret, credentialsRequired: false, getToken: req => req.cookies.id_token, })); app.use(passport.initialize()); app.get('/login/facebook', passport.authenticate('facebook', { scope: ['email'], session: false }), ); app.get('/login/facebook/return', passport.authenticate('facebook', { failureRedirect: 'http://localhost:3000/login', session: false }), (req, res) => { const expiresIn = 60 * 60 * 24 * 1; // 1 day const token = jwt.sign(req.user, authConfig.jwt.secret, { expiresIn }); res.redirect(`http://localhost:3000?token=${token}`); // Redirect to client and append token to param }, ); 

我的问题是,我现在怎么把这个令牌安全的交给客户呢? 我读过这个和这个 ,这导致我把标记传递给一个参数,但是我不确定我所做的是否安全。

以下是我在客户端获得的方式:

 const token = getURLParameterByName('token'); if (token) { localStorage.setItem('token', token); } networkInterface.use([{ applyMiddleware(req, next) { if (!req.options.headers) { req.options.headers = {}; // Create the header object if needed. } // Get the authentication token from local storage if it exists const localToken = localStorage.getItem('token') req.options.headers.authorization = localToken ? `Bearer ${localToken}` : null; next(); }, }]); 

之后,在API中使用它:

 app.use('/graphql', apolloExpress(req => { let token; if (req.headers.authorization) { token = req.headers.authorization; console.log(token); } return { schema: executableSchema, context: { token, }, }; }));