我在本地主机上运行的非嵌入式应用程序中使用了createShopifyAuth()
。导航到http://localhost:3001/auth?shop=fakedemo.myshopify.com
,提示安装后,它似乎挂在重定向上。 *尽管我确实在控制台日志中看到了数据。问题必须与我如何配置npm软件包koa-session-mongoose
使用this setup,但使用createShopifyAuth
代替shopifyAuth
require('isomorphic-fetch');
const Koa = require('koa');
const Router = require('koa-router');
const next = require('next');
const { default: createShopifyAuth } = require('@shopify/koa-shopify-auth');
const dotenv = require('dotenv');
const { verifyRequest } = require('@shopify/koa-shopify-auth');
const session = require('koa-session');
const mongoose = require('mongoose');
const MongooseStore = require('koa-session-mongoose');
dotenv.config();
const port = parseInt(process.env.PORT, 10) || 3001;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const { SHOPIFY_API_SECRET_KEY, SHOPIFY_API_KEY } = process.env;
app.prepare().then(() => {
// MongoDb setup
mongoose.Promise = require('bluebird');
mongoose.connect('mongodb-url', { useNewUrlParser: true})
mongoose.connection
.once('open', () => console.log('Connected to MongoLab instance.'))
.on('error', error => console.log('Error connecting to MongoLab:', error));
const server = new Koa();
const router = new Router();
server.keys = [SHOPIFY_API_SECRET_KEY];
// When uncommented, webpage hangs after auth but
// works perfectly as:
server.use(session(/*{ store: new MongooseStore() },*/ server));
// TODO: Chain .use
server.use(
createShopifyAuth({
apiKey: SHOPIFY_API_KEY,
secret: SHOPIFY_API_SECRET_KEY,
scopes: ['read_products', 'read_customers', 'read_orders'],
// disables embedded app:
accessMode: 'offline',
afterAuth(ctx) {
const { shop, accessToken } = ctx.session;
//.. Save to database. Mongo db?
// * I see data when website hangs
console.log(shop, accessToken)
// redirect never seem to call when using MongooseStore
ctx.redirect('/admin/orders');
},
}),
);
[...] // some router.get() etc
server.use(verifyRequest());
server.use(router.routes())
server.use(async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
ctx.res.statusCode = 200;
return
});
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
})