Passport-Bnet和Apollo GraphQL

时间:2018-08-01 18:28:39

标签: node.js passport.js graphql apollo

当前正在使用通过Passport.JS通过其BattleNet帐户对用户进行身份验证的项目。

我已经成功通过OAuth / Passport处理了实际的登录,并且似乎正在通过express-session创建会话,但是我不知道如何从Apollo解析器内部访问该会话。

以下是我当前的Express配置。它还包含Webpack的内容,因为我将在前端与React一起工作,但到目前为止还没有到此:

import fs from 'fs';
import https from 'https';
import express from 'express';
import cookieParser from 'cookie-parser';
import session from 'express-session';
import webpack from 'webpack';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackDevMiddleware from 'webpack-dev-middleware';
import passport from 'passport';
import { Strategy as BnetStrategy } from 'passport-bnet';
import { ApolloServer } from 'apollo-server-express';

// GQL Schema/Resolvers for Apollo
import typeDefs from './graphql/schema';
import resolvers from './graphql/resolvers';

// Webpack Config
import wpConfig from './webpack.config';

// Express
const app = express();

// Options for HTTPS server (certs)
const httpsOptions = {
    key: fs.readFileSync('./ssl/selfsigned.key'),
    cert: fs.readFileSync('./ssl/selfsigned.crt')
};

// Options for BNet OAuth Service (KEY/SECRET from ENV)
const bnetOptions = {
    clientID: process.env.BNET_OAUTH_KEY,
    clientSecret: process.env.BNET_OAUTH_SECRET,
    callbackURL: "https://localhost:3000/auth/callback",
    region: "us"
}

// WP Compiler
const compiler = webpack(wpConfig);

// Serialize/deserialize passport user
passport.serializeUser((user, done) => {
    console.log(user);
    return done(null, user.id);
})

passport.deserializeUser((userObj, done) => {
    console.log(userObj);
    return done(null, userObj);
})

// Use strategy within Passport
passport.use(new BnetStrategy(
    bnetOptions,
    (accessToken, refreshToken, profile, done) => {
        process.nextTick(() => {
            return done(null, profile);
        });
    }));

// Apollo Init
const gqlServer = new ApolloServer({
    typeDefs,
    resolvers,
    context: ({ req }) => req
});

// Middlewares
app.use(cookieParser());
app.use(session({
    name: 'WarMode',
    secret: 'blizzard',
    saveUninitialized: false,
    resave: false
}))
app.use(passport.initialize());
app.use(passport.session());
gqlServer.applyMiddleware({ app });
app.use(webpackDevMiddleware(compiler));
app.use(webpackHotMiddleware(compiler));

// BNet Auth
app.get('/auth', passport.authenticate('bnet'));

// BNet Auth Callback
app.get('/auth/callback',
    passport.authenticate('bnet', { failureRedirect: '/'}),
    (req, res) => {
        console.log(req);
        res.redirect('/')
    }
);

// Main route
app.get('/', (req, res) => {
    res.sendFile('index.html', {
        root: __dirname
    })
});

// 

// Launch server, using HTTPS.
https.createServer(httpsOptions, app).listen(3000, (err) => {
    if (err) {
        console.log(err)
    } else {
        console.log('Server running on port 3000');
    }
});

我已经阅读了文档,似乎应该使用context中的ApolloServer()选项将这些内容传递给我的GraphQL解析器,但似乎无法正常工作

我在某处缺少某个步骤吗?检查浏览器的开发工具后,似乎正在建立会话的cookie,但我不知道为什么在GraphQL解析器的context参数中看不到任何相关信息。

任何帮助将不胜感激。

0 个答案:

没有答案