feat: dashboard redirect

This commit is contained in:
DIYgod 2022-06-21 22:16:55 +01:00
parent 0f365f9603
commit fc3e38e9df
No known key found for this signature in database
GPG Key ID: D159328F47A80DCA
7 changed files with 1258 additions and 59 deletions

View File

@ -63,6 +63,7 @@
"remark-rehype": "^10.1.0",
"sortablejs": "^1.15.0",
"ua-parser-js": "^1.0.2",
"unidata.js": "^0.5.2",
"unified": "^10.1.2",
"unist-builder": "^3.0.0",
"unist-util-visit": "^4.1.0",

File diff suppressed because it is too large Load Diff

View File

@ -2,25 +2,25 @@ 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),
)
// 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
// 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
// await key.wait
// const payload = await decrypt(token, key.value)
// return payload
}
type LoginTokenPayload =
@ -41,6 +41,6 @@ export const generateLoginToken = async (payload: LoginTokenPayload) => {
}
export const decryptLoginToken = async (token: string) => {
const payload = await decryptToken(token)
return payload as LoginTokenPayload
// const payload = await decryptToken(token)
// return payload as LoginTokenPayload
}

View File

@ -7,6 +7,12 @@ import { SiteNavigationItem } from "~/lib/types"
import { nanoid } from "nanoid"
import { getMembership } from "./membership"
import { checkReservedWords } from "~/lib/reserved-words"
import Unidata from "unidata.js"
let unidata: Unidata;
if (typeof window !== "undefined") {
unidata = new Unidata()
}
export const checkSubdomain = async ({
subdomain,
@ -38,27 +44,22 @@ export const checkSubdomain = async ({
}
}
export const getUserLastActiveSite = async (userId: string) => {
const memberships = await prismaRead.membership.findMany({
where: {
userId,
role: {
in: [MembershipRole.OWNER, MembershipRole.ADMIN],
},
},
include: {
site: true,
},
orderBy: {
lastSwitchedTo: "desc",
},
})
export const getUserLastActiveSite = async (address: string) => {
if (unidata) {
const profiles = await unidata.profiles.get({
source: 'Crossbell Profile',
identity: address,
platform: 'Ethereum',
});
const site = memberships[0]?.site
const site = profiles.list?.sort((a: any, b: any) => a.date_updated - b.date_updated)?.[0]
if (!site || site.deletedAt) return null
if (!site) return null
return site
return site
} else {
return null
}
}
export const getSite = async (input: string) => {

View File

@ -3,10 +3,10 @@ import { AUTH_COOKIE_NAME } from "~/lib/env.server"
export default function middleware(req: NextRequest) {
// Redirect unauthenticated users to the home page
if (!req.cookies[AUTH_COOKIE_NAME]) {
const url = req.nextUrl.clone()
url.pathname = "/"
return NextResponse.redirect(url)
}
// if (!req.cookies[AUTH_COOKIE_NAME]) {
// const url = req.nextUrl.clone()
// url.pathname = "/"
// return NextResponse.redirect(url)
// }
return NextResponse.next()
}

View File

@ -1,20 +1,25 @@
import { getAuthUser } from "~/lib/auth.server"
import { redirect, serverSidePropsHandler } from "~/lib/server-side-props"
import { redirect } from "~/lib/server-side-props"
import { getUserLastActiveSite } from "~/models/site.model"
export const getServerSideProps = serverSidePropsHandler(async (ctx) => {
const user = await getAuthUser(ctx.req)
if (!user) {
return redirect("/")
}
const site = await getUserLastActiveSite(user.id)
if (!site) {
return redirect(`/dashboard/new-site`)
}
return redirect(`/dashboard/${site.subdomain}`)
})
import { useAccount } from 'wagmi'
import { useEffect } from "react"
import { useRouter } from 'next/router'
export default function Dashboard() {
return <div>You should never see this</div>
const { data: wagmiData } = useAccount()
const router = useRouter()
useEffect(() => {
if (wagmiData?.address) {
getUserLastActiveSite(wagmiData.address).then((site) => {
if (!site) {
router.push(`/dashboard/new-site`)
}
router.push(`/dashboard/${site.username}`)
})
} else {
router.push("/")
}
})
return <div>Loading...</div>
}

View File

@ -1,12 +1,11 @@
import { GetServerSideProps } from "next"
import { useEffect } from "react"
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 { useState } from "react"
import { useAccount } from 'wagmi'
import { ConnectButton } from '@rainbow-me/rainbowkit'