Fix useSessionSWR() expecting Drift's API interface instead of next-auths
This commit is contained in:
parent
a54a22f142
commit
3041da80e2
11 changed files with 97 additions and 111 deletions
|
@ -16,7 +16,6 @@ import Button from "@components/button"
|
|||
import Input from "@components/input"
|
||||
import ButtonDropdown from "@components/button-dropdown"
|
||||
import { useToasts } from "@components/toasts"
|
||||
import { useSessionSWR } from "@lib/use-session-swr"
|
||||
import { fetchWithUser } from "src/app/lib/fetch-with-user"
|
||||
|
||||
import dynamic from "next/dynamic"
|
||||
|
@ -41,8 +40,6 @@ function Post({
|
|||
initialPost?: PostWithFiles
|
||||
newPostParent?: string
|
||||
}): JSX.Element {
|
||||
const { isAuthenticated } = useSessionSWR()
|
||||
|
||||
const { setToast } = useToasts()
|
||||
const router = useRouter()
|
||||
const [title, setTitle] = useState(
|
||||
|
@ -91,6 +88,7 @@ function Post({
|
|||
if (res.ok) {
|
||||
const json = (await res.json()) as { id: string }
|
||||
router.push(`/post/${json.id}`)
|
||||
return
|
||||
} else {
|
||||
const json = (await res.json()) as { error: string }
|
||||
console.error(json)
|
||||
|
@ -177,11 +175,6 @@ function Post({
|
|||
[]
|
||||
)
|
||||
|
||||
if (isAuthenticated === false) {
|
||||
router.push("/signin")
|
||||
return <></>
|
||||
}
|
||||
|
||||
function onClosePasswordModal() {
|
||||
setPasswordModalVisible(false)
|
||||
setSubmitting(false)
|
||||
|
|
|
@ -6,8 +6,6 @@ export default function New() {
|
|||
return <NewPost />
|
||||
}
|
||||
|
||||
export const dynamic = "force-static"
|
||||
|
||||
export const metadata = getMetadata({
|
||||
title: "New post",
|
||||
hidden: true
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
import { redirect } from "next/navigation"
|
||||
import { getPostsByUser, serverPostToClientPost } from "@lib/server/prisma"
|
||||
import PostList from "@components/post-list"
|
||||
import { getCurrentUser } from "@lib/server/session"
|
||||
import { authOptions } from "@lib/server/auth"
|
||||
import { Suspense } from "react"
|
||||
import ErrorBoundary from "@components/error/fallback"
|
||||
import { getMetadata } from "src/app/lib/metadata"
|
||||
import { redirect } from "next/navigation"
|
||||
|
||||
export default async function Mine() {
|
||||
const userId = (await getCurrentUser())?.id
|
||||
|
||||
if (!userId) {
|
||||
return redirect(authOptions.pages?.signIn || "/new")
|
||||
// should be handled by middleware
|
||||
return redirect("/signup")
|
||||
}
|
||||
|
||||
const posts = (await getPostsByUser(userId, true)).map(serverPostToClientPost)
|
||||
|
|
|
@ -155,31 +155,30 @@ const Header = () => {
|
|||
isAuthLoading ? (
|
||||
<NavButtonPlaceholder width={SIGN_IN_WIDTH} key="signin" />
|
||||
) : undefined,
|
||||
!isAuthLoading ? (
|
||||
isAuthenticated ? (
|
||||
<FadeIn key="signout-fade">
|
||||
<NavButton
|
||||
name="Sign Out"
|
||||
icon={<UserX />}
|
||||
value="signout"
|
||||
onClick={() => {
|
||||
signOut()
|
||||
mutateSession(undefined)
|
||||
}}
|
||||
width={SIGN_IN_WIDTH}
|
||||
/>
|
||||
</FadeIn>
|
||||
) : (
|
||||
<FadeIn key="signin-fade">
|
||||
<NavButton
|
||||
name="Sign In"
|
||||
icon={<User />}
|
||||
value="signin"
|
||||
href="/signin"
|
||||
width={SIGN_IN_WIDTH}
|
||||
/>
|
||||
</FadeIn>
|
||||
)
|
||||
isAuthenticated === true ? (
|
||||
<FadeIn key="signout-fade">
|
||||
<NavButton
|
||||
name="Sign Out"
|
||||
icon={<UserX />}
|
||||
value="signout"
|
||||
onClick={() => {
|
||||
signOut()
|
||||
mutateSession(undefined)
|
||||
}}
|
||||
width={SIGN_IN_WIDTH}
|
||||
/>
|
||||
</FadeIn>
|
||||
) : undefined,
|
||||
isAuthenticated === false ? (
|
||||
<FadeIn key="signin-fade">
|
||||
<NavButton
|
||||
name="Sign In"
|
||||
icon={<User />}
|
||||
value="signin"
|
||||
href="/signin"
|
||||
width={SIGN_IN_WIDTH}
|
||||
/>
|
||||
</FadeIn>
|
||||
) : undefined,
|
||||
isAdmin ? (
|
||||
<FadeIn>
|
||||
|
|
|
@ -8,6 +8,8 @@ export function isAllowedVisibilityForWebpage(
|
|||
return ALLOWED_VISIBILITIES_FOR_WEBPAGE.includes(visibility)
|
||||
}
|
||||
|
||||
export const SIGNED_IN_COOKIE = "next-auth.session-token"
|
||||
|
||||
// Code files for uploading with drag and drop and syntax highlighting
|
||||
export const allowedFileTypes = [
|
||||
"application/json",
|
||||
|
|
|
@ -243,7 +243,6 @@ export const createUser = async (
|
|||
config.registration_password &&
|
||||
serverPassword !== config.registration_password
|
||||
) {
|
||||
console.log("Registration password mismatch")
|
||||
throw new Error("Wrong registration password")
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,9 @@ export function useSessionSWR() {
|
|||
isLoading,
|
||||
isValidating,
|
||||
mutate
|
||||
} = useSWR<Session>("/api/auth/session")
|
||||
} = useSWR<Session>("/api/auth/session", {
|
||||
fetcher: (url) => fetch(url).then((res) => res.json()) as Promise<Session>
|
||||
})
|
||||
|
||||
return {
|
||||
session,
|
||||
|
|
|
@ -6,20 +6,20 @@ export default withAuth(
|
|||
async function middleware(req) {
|
||||
const token = await getToken({ req })
|
||||
|
||||
const isAuth = !!token
|
||||
const isAuthed = !!token
|
||||
const isAuthPage =
|
||||
req.nextUrl.pathname.startsWith("/signup") ||
|
||||
req.nextUrl.pathname.startsWith("/signin")
|
||||
|
||||
if (isAuthPage) {
|
||||
if (isAuth) {
|
||||
if (isAuthed) {
|
||||
return NextResponse.redirect(new URL("/new", req.url))
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
if (!isAuth) {
|
||||
if (!isAuthed) {
|
||||
return NextResponse.redirect(new URL("/signin", req.url))
|
||||
}
|
||||
},
|
||||
|
@ -42,5 +42,6 @@ export const config = {
|
|||
"/signin",
|
||||
"/signup",
|
||||
"/new",
|
||||
"/mine",
|
||||
]
|
||||
}
|
||||
|
|
|
@ -8,15 +8,12 @@ import { getHtmlFromFile } from "@lib/server/get-html-from-drift-file"
|
|||
import { verifyApiUser } from "@lib/server/verify-api-user"
|
||||
|
||||
async function handlePost(req: NextApiRequest, res: NextApiResponse<unknown>) {
|
||||
console.log("Handling post request")
|
||||
try {
|
||||
const userId = await verifyApiUser(req, res)
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: "Unauthorized" })
|
||||
}
|
||||
|
||||
console.log("User is authenticated")
|
||||
|
||||
const files = req.body.files as (Omit<ServerFile, "content" | "html"> & {
|
||||
content: string
|
||||
html: string
|
||||
|
@ -26,7 +23,6 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<unknown>) {
|
|||
if (missingTitles.length > 0) {
|
||||
throw new Error("All files must have a title")
|
||||
}
|
||||
console.log("All files have titles")
|
||||
|
||||
if (files.length === 0) {
|
||||
throw new Error("You must submit at lea st one file")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue