fix referrer logic to work with lnauth/nostr auth

This commit is contained in:
keyan 2024-04-02 20:25:35 -05:00
parent a764837776
commit ffa86abdb3

View File

@ -51,6 +51,17 @@ function getCallbacks (req) {
// token won't have an id on it for new logins, we add it // token won't have an id on it for new logins, we add it
// note: token is what's kept in the jwt // note: token is what's kept in the jwt
token.id = Number(user.id) token.id = Number(user.id)
// if referrer exists, set on user
// isNewUser doesn't work for nostr/lightning auth because we create the user before nextauth can
// this means users can update their referrer if they don't have one, which is fine
if (req.cookies.sn_referrer && user?.id) {
const referrer = await prisma.user.findUnique({ where: { name: req.cookies.sn_referrer } })
if (referrer) {
await prisma.user.updateMany({ where: { id: user.id, referrerId: null }, data: { referrerId: referrer.id } })
notifyReferral(referrer.id)
}
}
} }
if (token?.id) { if (token?.id) {
@ -60,18 +71,8 @@ function getCallbacks (req) {
token.sub = Number(token.id) token.sub = Number(token.id)
} }
if (isNewUser) {
// if referrer exists, set on user
if (req.cookies.sn_referrer && user?.id) {
const referrer = await prisma.user.findUnique({ where: { name: req.cookies.sn_referrer } })
if (referrer) {
await prisma.user.update({ where: { id: user.id }, data: { referrerId: referrer.id } })
notifyReferral(referrer.id)
}
}
// sign them up for the newsletter // sign them up for the newsletter
if (user?.email && process.env.LIST_MONK_URL && process.env.LIST_MONK_AUTH) { if (isNewUser && user?.email && process.env.LIST_MONK_URL && process.env.LIST_MONK_AUTH) {
fetch(process.env.LIST_MONK_URL + '/api/subscribers', { fetch(process.env.LIST_MONK_URL + '/api/subscribers', {
method: 'POST', method: 'POST',
headers: { headers: {
@ -87,7 +88,6 @@ function getCallbacks (req) {
}) })
}).then(async r => console.log(await r.json())).catch(console.log) }).then(async r => console.log(await r.json())).catch(console.log)
} }
}
return token return token
}, },