mirror of https://github.com/MaxLeiter/Drift
refactor to SWR and verifyApiUser; personal post search is broken
parent
6fb81d77b9
commit
ba732dcd71
@ -1,14 +1,9 @@
|
|||||||
{
|
{
|
||||||
"extends": [
|
"plugins": ["@typescript-eslint"],
|
||||||
"next/core-web-vitals",
|
"extends": ["next/core-web-vitals", "plugin:@typescript-eslint/recommended"],
|
||||||
"eslint:recommended",
|
"ignorePatterns": ["node_modules/", "__tests__/"],
|
||||||
"plugin:@typescript-eslint/recommended"
|
"rules": {
|
||||||
],
|
"@typescript-eslint/no-unused-vars": "error",
|
||||||
"parser": "@typescript-eslint/parser",
|
"@typescript-eslint/no-explicit-any": "error"
|
||||||
"plugins": ["@typescript-eslint"],
|
}
|
||||||
"root": true,
|
|
||||||
"ignorePatterns": ["node_modules/", "__tests__/"],
|
|
||||||
"rules": {
|
|
||||||
"no-mixed-spaces-and-tabs": ["off"]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import NewPost from "src/app/(posts)/new/components/new"
|
import NewPost from "src/app/(posts)/new/components/new"
|
||||||
import "./react-datepicker.css"
|
import "./react-datepicker.css"
|
||||||
|
|
||||||
const New = () => <NewPost />
|
export default function New() {
|
||||||
|
return <NewPost />
|
||||||
export default New
|
}
|
||||||
|
|
||||||
export const dynamic = "force-static"
|
export const dynamic = "force-static"
|
||||||
|
|||||||
@ -0,0 +1,12 @@
|
|||||||
|
import { getSession } from "next-auth/react"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a fetch wrapper that adds `userId={userId}` to the query string
|
||||||
|
*/
|
||||||
|
export async function fetchWithUser(url: string, options: RequestInit = {}) {
|
||||||
|
// TODO: figure out if this extra network call hurts performance
|
||||||
|
const session = await getSession()
|
||||||
|
const newUrl = new URL(url, process.env.NEXT_PUBLIC_DRIFT_URL)
|
||||||
|
newUrl.searchParams.append("userId", session?.user.id || "")
|
||||||
|
return fetch(newUrl.toString(), options)
|
||||||
|
}
|
||||||
@ -1,12 +1,19 @@
|
|||||||
|
import type { GetServerSidePropsContext } from "next"
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { unstable_getServerSession } from "next-auth/next"
|
||||||
import { authOptions } from "./auth"
|
import { authOptions } from "./auth"
|
||||||
|
|
||||||
export async function getSession() {
|
type Params = {
|
||||||
return await unstable_getServerSession(authOptions)
|
req: GetServerSidePropsContext["req"]
|
||||||
|
res: GetServerSidePropsContext["res"]
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCurrentUser() {
|
export async function getSession(params?: Params) {
|
||||||
const session = await getSession()
|
if (!params) return await unstable_getServerSession(authOptions)
|
||||||
|
return await unstable_getServerSession(params.req, params.res, authOptions)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getCurrentUser(params?: Params) {
|
||||||
|
const session = await getSession(params)
|
||||||
|
|
||||||
return session?.user
|
return session?.user
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,25 @@
|
|||||||
|
import { Session } from "next-auth"
|
||||||
|
import useSWR from "swr"
|
||||||
|
|
||||||
|
export function useSessionSWR() {
|
||||||
|
const {
|
||||||
|
data: session,
|
||||||
|
error,
|
||||||
|
isLoading,
|
||||||
|
isValidating,
|
||||||
|
mutate
|
||||||
|
} = useSWR<Session>("/api/auth/session")
|
||||||
|
|
||||||
|
return {
|
||||||
|
session,
|
||||||
|
error,
|
||||||
|
isLoading,
|
||||||
|
isValidating,
|
||||||
|
mutate,
|
||||||
|
/** undefined while loading */
|
||||||
|
isAuthenticated: session?.user?.id ? true : isLoading ? undefined : false,
|
||||||
|
/** undefined while loading */
|
||||||
|
isAdmin:
|
||||||
|
session?.user?.id === "admin" ? true : isLoading ? undefined : false
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue