so now we're on fly.io
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { NextApiHandler } from "next"
|
||||
import { z } from "zod"
|
||||
import { generateCookie } from "~/lib/auth.server"
|
||||
import { IS_PROD } from "~/lib/constants"
|
||||
import { prismaPrimary } from "~/lib/db.server"
|
||||
import { OUR_DOMAIN } from "~/lib/env"
|
||||
|
||||
const handler: NextApiHandler = async (req, res) => {
|
||||
const data = z
|
||||
.object({
|
||||
id: z.string(),
|
||||
path: z.string().default("/"),
|
||||
host: z.string(),
|
||||
})
|
||||
.parse({
|
||||
id: req.query.id,
|
||||
path: req.query.path,
|
||||
host: req.headers.host,
|
||||
})
|
||||
|
||||
const isCustomDomain = data.host && !data.host.endsWith(`.${OUR_DOMAIN}`)
|
||||
|
||||
// Set cookie again for custom domain and subdomain.localhost (because *.localhost in cookie domain doesn't work)
|
||||
if (isCustomDomain || !IS_PROD) {
|
||||
const accessToken = await prismaPrimary.accessToken.findUnique({
|
||||
where: {
|
||||
publicId: data.id,
|
||||
},
|
||||
})
|
||||
|
||||
if (
|
||||
!accessToken ||
|
||||
!accessToken.publicIdExpiresAt ||
|
||||
accessToken.publicIdExpiresAt < new Date()
|
||||
) {
|
||||
throw new Error("invalid id or id expired")
|
||||
}
|
||||
|
||||
await prismaPrimary.accessToken.update({
|
||||
where: {
|
||||
id: accessToken.id,
|
||||
},
|
||||
data: {
|
||||
publicId: null,
|
||||
publicIdExpiresAt: null,
|
||||
},
|
||||
})
|
||||
|
||||
res.setHeader(
|
||||
"set-cookie",
|
||||
generateCookie({
|
||||
type: "auth",
|
||||
domain: data.host,
|
||||
token: accessToken.token,
|
||||
})
|
||||
)
|
||||
res.redirect(data.path)
|
||||
return
|
||||
}
|
||||
|
||||
res.redirect(data.path)
|
||||
}
|
||||
|
||||
export default handler
|
||||
@@ -0,0 +1,140 @@
|
||||
import { z } from "zod"
|
||||
import { prismaPrimary } from "~/lib/db.server"
|
||||
import { nanoid } from "nanoid"
|
||||
import UAParser from "ua-parser-js"
|
||||
import dayjs from "dayjs"
|
||||
import { generateCookie } from "~/lib/auth.server"
|
||||
import { OUR_DOMAIN } from "~/lib/env"
|
||||
import { IS_PROD } from "~/lib/constants"
|
||||
import { NextApiHandler } from "next"
|
||||
import { subscribeToSite } from "~/models/site.model"
|
||||
import { createGate } from "~/lib/gate.server"
|
||||
|
||||
const handler: NextApiHandler = async (req, res) => {
|
||||
const data = z
|
||||
.object({
|
||||
token: z.string(),
|
||||
next: z.string(),
|
||||
userAgent: z.string(),
|
||||
subscribe: z
|
||||
.object({
|
||||
siteId: z.string(),
|
||||
email: z.boolean().optional(),
|
||||
telegram: z.boolean().optional(),
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
.parse({
|
||||
token: req.query.token,
|
||||
next: req.query.next,
|
||||
userAgent: req.headers["user-agent"],
|
||||
subscribe:
|
||||
req.query.subscribe && JSON.parse(req.query.subscribe as string),
|
||||
})
|
||||
|
||||
const loginToken = await prismaPrimary.loginToken.findUnique({
|
||||
where: {
|
||||
id: data.token,
|
||||
},
|
||||
})
|
||||
|
||||
if (!loginToken) {
|
||||
throw new Error("invalid token")
|
||||
}
|
||||
|
||||
if (loginToken.expiresAt < new Date()) {
|
||||
throw new Error(`token expired`)
|
||||
}
|
||||
|
||||
let user = await prismaPrimary.user.findUnique({
|
||||
where: {
|
||||
email: loginToken.email,
|
||||
},
|
||||
include: {
|
||||
memberships: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
user = await prismaPrimary.user.create({
|
||||
data: {
|
||||
email: loginToken.email,
|
||||
name: loginToken.email.split("@")[0],
|
||||
username: nanoid(7),
|
||||
},
|
||||
include: {
|
||||
memberships: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if (data.subscribe) {
|
||||
// Subscribe the user to the site
|
||||
const gate = createGate({ user })
|
||||
await subscribeToSite(gate, {
|
||||
siteId: data.subscribe.siteId,
|
||||
email: data.subscribe.email,
|
||||
telegram: data.subscribe.telegram,
|
||||
})
|
||||
}
|
||||
|
||||
await prismaPrimary.loginToken.delete({
|
||||
where: {
|
||||
id: loginToken.id,
|
||||
},
|
||||
})
|
||||
|
||||
const ua = new UAParser(data.userAgent)
|
||||
|
||||
const publicId = nanoid(32)
|
||||
const accessToken = await prismaPrimary.accessToken.create({
|
||||
data: {
|
||||
user: {
|
||||
connect: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
token: nanoid(32),
|
||||
name: `Login via ${ua.getOS().name} ${ua.getBrowser().name}`,
|
||||
publicId,
|
||||
publicIdExpiresAt: dayjs().add(1, "minute").toDate(),
|
||||
},
|
||||
})
|
||||
|
||||
if (!IS_PROD) {
|
||||
console.log("login with token", accessToken.token)
|
||||
}
|
||||
|
||||
// Custom domain
|
||||
const nextUrl = new URL(data.next)
|
||||
|
||||
if (nextUrl.host !== OUR_DOMAIN && !nextUrl.host.endsWith(`.${OUR_DOMAIN}`)) {
|
||||
// Check if the host belong to a site
|
||||
const existing = await prismaPrimary.domain.findUnique({
|
||||
where: {
|
||||
domain: nextUrl.hostname,
|
||||
},
|
||||
})
|
||||
if (!existing) {
|
||||
throw new Error("invalid next url")
|
||||
}
|
||||
}
|
||||
|
||||
nextUrl.searchParams.set("id", publicId)
|
||||
nextUrl.searchParams.set("path", nextUrl.pathname)
|
||||
nextUrl.pathname = "/api/login-complete"
|
||||
console.log("redirecting to", nextUrl.href)
|
||||
|
||||
res.setHeader(
|
||||
"set-cookie",
|
||||
generateCookie({
|
||||
type: "auth",
|
||||
domain: OUR_DOMAIN,
|
||||
token: accessToken.token,
|
||||
})
|
||||
)
|
||||
|
||||
res.redirect(nextUrl.href)
|
||||
}
|
||||
|
||||
export default handler
|
||||
@@ -0,0 +1,14 @@
|
||||
import { NextApiHandler } from "next"
|
||||
import { generateCookie } from "~/lib/auth.server"
|
||||
|
||||
const handler: NextApiHandler = async (req, res) => {
|
||||
res.setHeader("set-cookie", generateCookie({ type: "clear" }))
|
||||
const next = req.query.next as string | undefined
|
||||
if (next) {
|
||||
res.redirect(next)
|
||||
} else {
|
||||
res.redirect("/")
|
||||
}
|
||||
}
|
||||
|
||||
export default handler
|
||||
@@ -0,0 +1,10 @@
|
||||
import * as trpcNext from "@trpc/server/adapters/next"
|
||||
import { getTRPCContext } from "~/lib/trpc.server"
|
||||
|
||||
import { appRouter } from "~/router"
|
||||
|
||||
// export API handler
|
||||
export default trpcNext.createNextApiHandler({
|
||||
router: appRouter,
|
||||
createContext: getTRPCContext,
|
||||
})
|
||||
@@ -0,0 +1,32 @@
|
||||
import { NextApiHandler } from "next"
|
||||
import Iron from "@hapi/iron"
|
||||
import { ENCRYPT_SECRET } from "~/lib/env.server"
|
||||
import { unsubscribeFromSite } from "~/models/site.model"
|
||||
import { prismaPrimary } from "~/lib/db.server"
|
||||
import { createGate } from "~/lib/gate.server"
|
||||
|
||||
const handler: NextApiHandler = async (req, res) => {
|
||||
const token = req.query.token as string
|
||||
const payload: {
|
||||
siteId: string
|
||||
userId: string
|
||||
} = await Iron.unseal(token, ENCRYPT_SECRET, Iron.defaults)
|
||||
|
||||
const user = await prismaPrimary.user.findUnique({
|
||||
where: {
|
||||
id: payload.userId,
|
||||
},
|
||||
include: {
|
||||
memberships: true,
|
||||
},
|
||||
})
|
||||
if (!user) return res.send("invalid token")
|
||||
|
||||
const gate = createGate({ user })
|
||||
|
||||
await unsubscribeFromSite(gate, { siteId: payload.siteId })
|
||||
|
||||
res.send("unsubscribed!")
|
||||
}
|
||||
|
||||
export default handler
|
||||
Reference in New Issue
Block a user