refactor: remove useless code and files
This commit is contained in:
@@ -4,8 +4,6 @@ import { useAccount, useDisconnect, useBalance } from "wagmi"
|
||||
import { Avatar } from "~/components/ui/Avatar"
|
||||
import type { HeaderLinkType } from "~/components/site/SiteHeader"
|
||||
import { DashboardIcon } from "../icons/DashboardIcon"
|
||||
import { IS_PROD } from "~/lib/constants"
|
||||
import { OUR_DOMAIN } from "~/lib/env"
|
||||
import { UniLink } from "../ui/UniLink"
|
||||
import { useEffect, useState } from "react"
|
||||
import { DuplicateIcon, LogoutIcon } from "@heroicons/react/outline"
|
||||
@@ -41,7 +39,7 @@ export const ConnectButton: React.FC<{
|
||||
{
|
||||
icon: <DashboardIcon />,
|
||||
label: "Writer dashboard",
|
||||
url: `${IS_PROD ? "https" : "http"}://${OUR_DOMAIN}/dashboard`,
|
||||
url: `/dashboard`,
|
||||
},
|
||||
{
|
||||
icon: <LogoutIcon className="w-4 h-4" />,
|
||||
|
||||
@@ -1,12 +1,4 @@
|
||||
import {
|
||||
APP_DESCRIPTION,
|
||||
APP_NAME,
|
||||
DOCS_DOMAIN,
|
||||
GITHUB_LINK,
|
||||
OUR_DOMAIN,
|
||||
DISCORD_LINK,
|
||||
} from "~/lib/env"
|
||||
import { useStore } from "~/lib/store"
|
||||
import { APP_DESCRIPTION, APP_NAME, GITHUB_LINK, DISCORD_LINK } from "~/lib/env"
|
||||
import { SEOHead } from "../common/SEOHead"
|
||||
import { UniLink } from "../ui/UniLink"
|
||||
import { ConnectButton } from "../common/ConnectButton"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import clsx from "clsx"
|
||||
import { useRouter } from "next/router"
|
||||
import { logout } from "~/lib/auth.client"
|
||||
import { IS_PROD } from "~/lib/constants"
|
||||
import { SITE_URL, CSB_SCAN, CSB_IO } from "~/lib/env"
|
||||
import { useStore } from "~/lib/store"
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import { OUR_DOMAIN } from "./env"
|
||||
|
||||
export const logout = () => {
|
||||
if (!confirm(`Are you sure you want to log out?`)) return
|
||||
|
||||
const url = new URL(`${location.protocol}//${OUR_DOMAIN}/api/logout`)
|
||||
if (location.host !== OUR_DOMAIN) {
|
||||
url.searchParams.set(
|
||||
"next",
|
||||
`${location.protocol}//${location.host}/api/logout`,
|
||||
)
|
||||
}
|
||||
location.href = url.href
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
import type { User, Membership } from "~/lib/db.server"
|
||||
import { prismaRead } from "./db.server"
|
||||
import dayjs from "dayjs"
|
||||
import Cookie, { CookieSerializeOptions } from "cookie"
|
||||
import { AUTH_COOKIE_NAME } from "./env.server"
|
||||
import { IS_PROD } from "./constants"
|
||||
import { IncomingMessage } from "http"
|
||||
import { OUR_DOMAIN } from "./env"
|
||||
|
||||
export type AuthUser = User & {
|
||||
memberships: Membership[]
|
||||
}
|
||||
|
||||
export const getAuthTokenFromRequest = (request: IncomingMessage) => {
|
||||
let token = request.headers.authorization?.replace(/[Bb]earer\s/, "")
|
||||
if (!token) {
|
||||
const cookieHeader = request.headers.cookie
|
||||
if (cookieHeader) {
|
||||
const value = Cookie.parse(cookieHeader)[AUTH_COOKIE_NAME]
|
||||
token = value
|
||||
}
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
const findUserFromToken = async (token: string) => {
|
||||
const accessToken = await prismaRead.accessToken.findUnique({
|
||||
where: {
|
||||
token,
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
include: {
|
||||
memberships: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return accessToken?.user
|
||||
}
|
||||
|
||||
export const getAuthUser = async <TRequireAuth extends boolean | undefined>(
|
||||
request: IncomingMessage,
|
||||
requireAuth?: TRequireAuth,
|
||||
) => {
|
||||
const token = getAuthTokenFromRequest(request)
|
||||
|
||||
let user: AuthUser | null | undefined = null
|
||||
|
||||
if (token) {
|
||||
user = await findUserFromToken(token)
|
||||
}
|
||||
|
||||
if (!user && requireAuth) {
|
||||
throw new Error("require auth")
|
||||
}
|
||||
|
||||
return user as TRequireAuth extends true
|
||||
? AuthUser
|
||||
: AuthUser | null | undefined
|
||||
}
|
||||
|
||||
export const getAuthCookieOptions = ({
|
||||
domain,
|
||||
clearCookie,
|
||||
}: {
|
||||
domain?: string
|
||||
clearCookie?: boolean
|
||||
}): CookieSerializeOptions => {
|
||||
return {
|
||||
path: "/",
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
expires: clearCookie ? new Date() : dayjs().add(6, "month").toDate(),
|
||||
secure: IS_PROD,
|
||||
domain: IS_PROD ? `.${domain || OUR_DOMAIN}` : undefined,
|
||||
}
|
||||
}
|
||||
|
||||
export const generateCookie = (
|
||||
options:
|
||||
| { type: "clear"; domain?: string }
|
||||
| { type: "auth"; token: string; domain?: string },
|
||||
) => {
|
||||
if (options.type === "clear") {
|
||||
return Cookie.serialize(
|
||||
AUTH_COOKIE_NAME,
|
||||
"",
|
||||
getAuthCookieOptions({ domain: options.domain, clearCookie: true }),
|
||||
)
|
||||
}
|
||||
return Cookie.serialize(
|
||||
AUTH_COOKIE_NAME,
|
||||
options.token,
|
||||
getAuthCookieOptions({ domain: options.domain }),
|
||||
)
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
import { MembershipRole, type Site, type Page } from "~/lib/db.server"
|
||||
import type { AuthUser } from "./auth.server"
|
||||
import { PageVisibilityEnum } from "./types"
|
||||
|
||||
type Action =
|
||||
| {
|
||||
type: "can-delete-page"
|
||||
/** The site id this page belongs to */
|
||||
siteId: string
|
||||
}
|
||||
| {
|
||||
type: "can-list-page"
|
||||
visibility: PageVisibilityEnum
|
||||
siteId: string
|
||||
}
|
||||
| {
|
||||
type: "can-create-page"
|
||||
siteId: string
|
||||
}
|
||||
| { type: "can-update-page"; siteId: string }
|
||||
| {
|
||||
type: "can-read-page"
|
||||
page: Page
|
||||
}
|
||||
| {
|
||||
type: "can-update-site"
|
||||
site: Site
|
||||
}
|
||||
| {
|
||||
type: "can-notify-site-subscribers"
|
||||
site: Site
|
||||
}
|
||||
| {
|
||||
type: "can-update-membership"
|
||||
membership: { id: string }
|
||||
}
|
||||
export const createGate = <TRequiredAuth extends boolean | undefined>({
|
||||
user,
|
||||
}: {
|
||||
user: AuthUser | null | undefined
|
||||
}) => {
|
||||
const isSiteMember = (siteId: string, roles: MembershipRole[]) => {
|
||||
if (!user) return false
|
||||
return user.memberships.some(
|
||||
(m) => m.siteId === siteId && roles.includes(m.role),
|
||||
)
|
||||
}
|
||||
|
||||
const userHasMembership = (membershipId: string) => {
|
||||
if (!user) return false
|
||||
return user.memberships.some((m) => m.id === membershipId)
|
||||
}
|
||||
|
||||
return {
|
||||
getUser<TRequiredAuth extends boolean | undefined>(
|
||||
requireAuth?: TRequiredAuth,
|
||||
) {
|
||||
if (requireAuth && !user) {
|
||||
throw new Error("unauthorized")
|
||||
}
|
||||
return user as TRequiredAuth extends true
|
||||
? AuthUser
|
||||
: AuthUser | null | undefined
|
||||
},
|
||||
|
||||
hasRoles(siteId: string, roles: MembershipRole[]) {
|
||||
return isSiteMember(siteId, roles)
|
||||
},
|
||||
|
||||
isOwnerOrAdmin(siteId: string) {
|
||||
return this.hasRoles(siteId, [MembershipRole.OWNER, MembershipRole.ADMIN])
|
||||
},
|
||||
|
||||
allows(action: Action): boolean {
|
||||
if (action.type === "can-delete-page") {
|
||||
return isSiteMember(action.siteId, [
|
||||
MembershipRole.ADMIN,
|
||||
MembershipRole.OWNER,
|
||||
])
|
||||
}
|
||||
|
||||
if (action.type === "can-list-page") {
|
||||
if (action.visibility === PageVisibilityEnum.Published) {
|
||||
return true
|
||||
}
|
||||
return isSiteMember(action.siteId, [
|
||||
MembershipRole.ADMIN,
|
||||
MembershipRole.OWNER,
|
||||
])
|
||||
}
|
||||
|
||||
if (action.type === "can-create-page") {
|
||||
return isSiteMember(action.siteId, [
|
||||
MembershipRole.ADMIN,
|
||||
MembershipRole.OWNER,
|
||||
])
|
||||
}
|
||||
|
||||
if (action.type === "can-update-page") {
|
||||
return isSiteMember(action.siteId, [
|
||||
MembershipRole.ADMIN,
|
||||
MembershipRole.OWNER,
|
||||
])
|
||||
}
|
||||
|
||||
if (action.type === "can-read-page") {
|
||||
const isPublished =
|
||||
action.page.published &&
|
||||
action.page.publishedAt &&
|
||||
action.page.publishedAt <= new Date()
|
||||
|
||||
return isPublished
|
||||
? !action.page.deletedAt
|
||||
: !action.page.deletedAt &&
|
||||
isSiteMember(action.page.siteId, [
|
||||
MembershipRole.ADMIN,
|
||||
MembershipRole.OWNER,
|
||||
])
|
||||
}
|
||||
|
||||
if (action.type === "can-update-site") {
|
||||
return isSiteMember(action.site.id, [
|
||||
MembershipRole.ADMIN,
|
||||
MembershipRole.OWNER,
|
||||
])
|
||||
}
|
||||
|
||||
if (action.type === "can-notify-site-subscribers") {
|
||||
return isSiteMember(action.site.id, [
|
||||
MembershipRole.ADMIN,
|
||||
MembershipRole.OWNER,
|
||||
])
|
||||
}
|
||||
|
||||
if (action.type === "can-update-membership") {
|
||||
return userHasMembership(action.membership.id)
|
||||
}
|
||||
|
||||
return false
|
||||
},
|
||||
permissionError(message = "not allowed") {
|
||||
return new Error(message)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export type Gate = ReturnType<typeof createGate>
|
||||
@@ -1,46 +0,0 @@
|
||||
import { decrypt, encrypt, getDerivedKey } from "@proselog/jwt"
|
||||
import { ENCRYPT_SECRET } from "./env.server"
|
||||
import { singletonAsync } from "./singleton.server"
|
||||
|
||||
// const key = singletonAsync("encryption_key", () =>
|
||||
// getDerivedKey(ENCRYPT_SECRET),
|
||||
// )
|
||||
|
||||
export const generateEncryptedToken = async (
|
||||
payload: any,
|
||||
expiresIn = "20m",
|
||||
) => {
|
||||
// await key.wait
|
||||
// const token = await encrypt(payload, key.value, {
|
||||
// expiresIn,
|
||||
// })
|
||||
// return token
|
||||
}
|
||||
|
||||
export const decryptToken = async (token: string) => {
|
||||
// await key.wait
|
||||
// const payload = await decrypt(token, key.value)
|
||||
// return payload
|
||||
}
|
||||
|
||||
type LoginTokenPayload =
|
||||
| { type: "login"; email: string }
|
||||
| { type: "subscribe"; email: string; siteId: string }
|
||||
| {
|
||||
type: "unsubscribe"
|
||||
siteId: string
|
||||
userId: string
|
||||
}
|
||||
|
||||
export const generateLoginToken = async (payload: LoginTokenPayload) => {
|
||||
const token = await generateEncryptedToken(
|
||||
payload,
|
||||
payload.type === "subscribe" ? "7d" : "20m",
|
||||
)
|
||||
return token
|
||||
}
|
||||
|
||||
export const decryptLoginToken = async (token: string) => {
|
||||
// const payload = await decryptToken(token)
|
||||
// return payload as LoginTokenPayload
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
import * as trpc from "@trpc/server"
|
||||
import { IncomingMessage } from "http"
|
||||
import { AuthUser, getAuthUser } from "./auth.server"
|
||||
import { createGate, Gate } from "./gate.server"
|
||||
|
||||
export type TRPCContext = { user: AuthUser | null | undefined; gate: Gate }
|
||||
|
||||
export const getTRPCContext = async ({
|
||||
req,
|
||||
}: {
|
||||
req: IncomingMessage
|
||||
}): Promise<TRPCContext> => {
|
||||
const user = await getAuthUser(req)
|
||||
const gate = createGate({ user })
|
||||
return {
|
||||
user,
|
||||
gate,
|
||||
}
|
||||
}
|
||||
|
||||
export const createRouter = () => {
|
||||
return trpc.router<TRPCContext>()
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { AuthUser } from "./auth.server"
|
||||
import { Viewer } from "./types"
|
||||
|
||||
export const getViewer = (user: AuthUser | null | undefined): Viewer | null => {
|
||||
if (!user) return null
|
||||
return {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
avatar: user.avatar,
|
||||
username: user.username,
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,6 @@
|
||||
import { nanoid } from "nanoid"
|
||||
import {
|
||||
prismaPrimary,
|
||||
Prisma,
|
||||
prismaRead,
|
||||
MembershipRole,
|
||||
PageEmailStatus,
|
||||
} from "~/lib/db.server"
|
||||
import { type Gate } from "~/lib/gate.server"
|
||||
import { Rendered, renderPageContent } from "~/markdown"
|
||||
import { notFound } from "~/lib/server-side-props"
|
||||
import { PageVisibilityEnum, Notes, Note } from "~/lib/types"
|
||||
import { isUUID } from "~/lib/uuid"
|
||||
import { stripHTML } from "~/lib/utils"
|
||||
import unidata from "~/lib/unidata"
|
||||
import { indexer, getContract } from "~/lib/crossbell"
|
||||
import { NoteEntity, CharacterEntity } from "crossbell.js"
|
||||
|
||||
+18
-26
@@ -1,10 +1,5 @@
|
||||
import { prismaPrimary, prismaRead } from "~/lib/db.server"
|
||||
import { isUUID } from "~/lib/uuid"
|
||||
import { MembershipRole, PageType, type Site } from "~/lib/db.server"
|
||||
import { Gate } from "~/lib/gate.server"
|
||||
import { SiteNavigationItem, Profile } from "~/lib/types"
|
||||
import { nanoid } from "nanoid"
|
||||
import { getMembership } from "./membership"
|
||||
import { checkReservedWords } from "~/lib/reserved-words"
|
||||
import unidata from "~/lib/unidata"
|
||||
|
||||
@@ -15,27 +10,24 @@ export const checkSubdomain = async ({
|
||||
subdomain: string
|
||||
updatingSiteId?: string
|
||||
}) => {
|
||||
checkReservedWords(subdomain)
|
||||
|
||||
const existingSite = await prismaPrimary.site.findUnique({
|
||||
where: {
|
||||
subdomain,
|
||||
},
|
||||
})
|
||||
|
||||
if (existingSite?.deletedAt) {
|
||||
// Actuall delete the site so that the subdomain can be used again
|
||||
await prismaPrimary.site.delete({
|
||||
where: {
|
||||
id: existingSite.id,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (existingSite && (!updatingSiteId || existingSite.id !== updatingSiteId)) {
|
||||
throw new Error(`Subdomain already taken`)
|
||||
}
|
||||
// checkReservedWords(subdomain)
|
||||
// const existingSite = await prismaPrimary.site.findUnique({
|
||||
// where: {
|
||||
// subdomain,
|
||||
// },
|
||||
// })
|
||||
// if (existingSite?.deletedAt) {
|
||||
// // Actuall delete the site so that the subdomain can be used again
|
||||
// await prismaPrimary.site.delete({
|
||||
// where: {
|
||||
// id: existingSite.id,
|
||||
// },
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
// if (existingSite && (!updatingSiteId || existingSite.id !== updatingSiteId)) {
|
||||
// throw new Error(`Subdomain already taken`)
|
||||
// }
|
||||
}
|
||||
|
||||
export const getUserSites = async (address?: string) => {
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
import { prismaPrimary } from "~/lib/db.server"
|
||||
import { Gate } from "~/lib/gate.server"
|
||||
import { checkReservedWords } from "~/lib/reserved-words"
|
||||
|
||||
export const userModel = {
|
||||
async updateProfile(
|
||||
gate: Gate,
|
||||
payload: {
|
||||
name?: string
|
||||
username?: string
|
||||
avatar?: string
|
||||
bio?: string
|
||||
email?: string
|
||||
},
|
||||
) {
|
||||
const user = gate.getUser(true)
|
||||
|
||||
if (payload.email) {
|
||||
const userByEmail = await prismaPrimary.user.findUnique({
|
||||
where: {
|
||||
email: payload.email,
|
||||
},
|
||||
})
|
||||
if (userByEmail && userByEmail.id !== user.id) {
|
||||
throw new Error("email already in use")
|
||||
}
|
||||
}
|
||||
|
||||
if (payload.username) {
|
||||
checkReservedWords(payload.username)
|
||||
|
||||
const userByUsername = await prismaPrimary.user.findUnique({
|
||||
where: {
|
||||
username: payload.username,
|
||||
},
|
||||
})
|
||||
if (userByUsername && userByUsername.id !== user.id) {
|
||||
throw new Error("username already in use")
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prismaPrimary.user.update({
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
data: {
|
||||
name: payload.name,
|
||||
avatar: payload.avatar,
|
||||
username: payload.username,
|
||||
email: payload.email,
|
||||
emailVerified: payload.email !== user.email ? null : undefined,
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
updated,
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -1,19 +1,13 @@
|
||||
import { GetServerSideProps } from "next"
|
||||
import { SiteLayout } from "~/components/site/SiteLayout"
|
||||
import { getAuthUser } from "~/lib/auth.server"
|
||||
import { createSSGHelpers } from "@trpc/react/ssg"
|
||||
import { getTRPCContext } from "~/lib/trpc.server"
|
||||
import { SitePage } from "~/components/site/SitePage"
|
||||
import { serverSidePropsHandler } from "~/lib/server-side-props"
|
||||
import { getViewer } from "~/lib/viewer"
|
||||
import { Viewer, Profile, Note } from "~/lib/types"
|
||||
import { queryClientServer } from "~/lib/query-client.server"
|
||||
import { prefetchGetSite } from "~/queries/site.server"
|
||||
import { useGetSite } from "~/queries/site"
|
||||
import { dehydrate, QueryClient } from "@tanstack/react-query"
|
||||
import { fetchGetPage } from "~/queries/page.server"
|
||||
import { useGetPage } from "~/queries/page"
|
||||
import { PageVisibilityEnum } from "~/lib/types"
|
||||
import { notFound } from "~/lib/server-side-props"
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = serverSidePropsHandler(
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import { GetServerSideProps } from "next"
|
||||
import { SiteLayout } from "~/components/site/SiteLayout"
|
||||
import { getAuthUser } from "~/lib/auth.server"
|
||||
import { createSSGHelpers } from "@trpc/react/ssg"
|
||||
import { getTRPCContext } from "~/lib/trpc.server"
|
||||
import { serverSidePropsHandler } from "~/lib/server-side-props"
|
||||
import { SiteArchives } from "~/components/site/SiteArchives"
|
||||
import { getViewer } from "~/lib/viewer"
|
||||
import { Viewer, Profile, Notes } from "~/lib/types"
|
||||
import { queryClientServer } from "~/lib/query-client.server"
|
||||
import { prefetchGetSite } from "~/queries/site.server"
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
import { GetServerSideProps } from "next"
|
||||
import { SiteHome } from "~/components/site/SiteHome"
|
||||
import { SiteLayout } from "~/components/site/SiteLayout"
|
||||
import { createSSGHelpers } from "@trpc/react/ssg"
|
||||
import { getTRPCContext } from "~/lib/trpc.server"
|
||||
import { Viewer, Profile, Notes } from "~/lib/types"
|
||||
import { getViewer } from "~/lib/viewer"
|
||||
import { useEffect, useState } from "react"
|
||||
import { queryClientServer } from "~/lib/query-client.server"
|
||||
import { prefetchGetSite } from "~/queries/site.server"
|
||||
import { useGetSite } from "~/queries/site"
|
||||
|
||||
@@ -6,7 +6,6 @@ import { DashboardLayout } from "~/components/dashboard/DashboardLayout"
|
||||
import { SettingsLayout } from "~/components/dashboard/SettingsLayout"
|
||||
import { Button } from "~/components/ui/Button"
|
||||
import { Input } from "~/components/ui/Input"
|
||||
import { OUR_DOMAIN } from "~/lib/env"
|
||||
import { useGetSite, useUpdateSite } from "~/queries/site"
|
||||
|
||||
export default function SettingsDomainsPage() {
|
||||
|
||||
@@ -3,7 +3,6 @@ import { useEffect, useState } from "react"
|
||||
import { DashboardIcon } from "~/components/icons/DashboardIcon"
|
||||
import { MainLayout } from "~/components/main/MainLayout"
|
||||
import { UniLink } from "~/components/ui/UniLink"
|
||||
import { getAuthUser } from "~/lib/auth.server"
|
||||
import { FLY_REGION } from "~/lib/env.server"
|
||||
import { useStore } from "~/lib/store"
|
||||
import { useAccount } from "wagmi"
|
||||
|
||||
Reference in New Issue
Block a user