Created
October 8, 2021 14:11
-
-
Save rahsheen/669f6f20d8c60a42623efc7b61b2586d to your computer and use it in GitHub Desktop.
Blitz+Shopify Passport Config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { passportAuth } from "blitz" | |
import db from "db" | |
import { Strategy as ShopifyStrategy } from "passport-shopify" | |
import { OAuth2Strategy as GoogleStrategy } from "passport-google-oauth" | |
export default passportAuth(({ ctx, req, res }) => ({ | |
successRedirectUrl: "/", | |
errorRedirectUrl: "/", | |
strategies: [ | |
{ | |
authenticateOptions: { | |
scope: "write_products,write_customers,write_draft_orders", | |
shop: req.query.shop, | |
}, | |
strategy: new ShopifyStrategy( | |
{ | |
clientID: process.env.SHOPIFY_API_KEY, | |
clientSecret: process.env.SHOPIFY_API_SECRET, | |
callbackURL: `https://7d8d-69-180-3-140.ngrok.io/api/auth/shopify`, | |
shop: req.query.shop, | |
}, | |
async (_accessToken, _refreshToken, profile, done) => { | |
console.log(req.query) | |
const email = profile.emails && profile.emails[0]?.value | |
const name = profile.profileUrl | |
if (!email) { | |
return done(new Error("Shopify OAuth response doesn't have email.")) | |
} | |
const user = await db.user.upsert({ | |
where: { email }, | |
create: { | |
email, | |
name, | |
}, | |
update: { email }, | |
}) | |
const publicData = { | |
userId: user.id, | |
roles: [user.role], | |
source: "shopify", | |
} | |
// This logs correctly | |
console.log("Almost done!", publicData) | |
done(undefined, { publicData }) | |
} | |
), | |
}, | |
{ | |
authenticateOptions: { | |
scope: [ | |
"https://www.googleapis.com/auth/userinfo.email", | |
"https://www.googleapis.com/auth/plus.login", | |
], | |
}, | |
strategy: new GoogleStrategy( | |
{ | |
clientID: process.env.GOOGLE_CLIENT_ID, | |
clientSecret: process.env.GOOGLE_CLIENT_SECRET, | |
callbackURL: | |
process.env.NODE_ENV === "production" | |
? "https://example.com/api/auth/google/callback" | |
: "http://localhost:3000/api/auth/google/callback", | |
includeEmail: true, | |
}, | |
async function (_token, _tokenSecret, profile, done) { | |
console.log(profile) | |
const email = profile.emails && profile.emails[0]?.value | |
if (!email) { | |
// This can happen if you haven't enabled email access in your twitter app permissions | |
return done(new Error("Google OAuth response doesn't have email.")) | |
} | |
const user = await db.user.upsert({ | |
where: { email }, | |
create: { | |
email, | |
name: profile.displayName, | |
}, | |
update: { email }, | |
}) | |
const publicData = { | |
userId: user.id, | |
roles: [user.role], | |
source: "google", | |
} | |
done(undefined, { publicData }) | |
} | |
), | |
}, | |
], | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment