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": [
|
||||
"next/core-web-vitals",
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/recommended"
|
||||
],
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"plugins": ["@typescript-eslint"],
|
||||
"root": true,
|
||||
"ignorePatterns": ["node_modules/", "__tests__/"],
|
||||
"rules": {
|
||||
"no-mixed-spaces-and-tabs": ["off"]
|
||||
}
|
||||
"plugins": ["@typescript-eslint"],
|
||||
"extends": ["next/core-web-vitals", "plugin:@typescript-eslint/recommended"],
|
||||
"ignorePatterns": ["node_modules/", "__tests__/"],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-unused-vars": "error",
|
||||
"@typescript-eslint/no-explicit-any": "error"
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
import NewPost from "src/app/(posts)/new/components/new"
|
||||
import "./react-datepicker.css"
|
||||
|
||||
const New = () => <NewPost />
|
||||
|
||||
export default New
|
||||
export default function New() {
|
||||
return <NewPost />
|
||||
}
|
||||
|
||||
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 { authOptions } from "./auth"
|
||||
|
||||
export async function getSession() {
|
||||
return await unstable_getServerSession(authOptions)
|
||||
type Params = {
|
||||
req: GetServerSidePropsContext["req"]
|
||||
res: GetServerSidePropsContext["res"]
|
||||
}
|
||||
|
||||
export async function getCurrentUser() {
|
||||
const session = await getSession()
|
||||
export async function getSession(params?: Params) {
|
||||
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
|
||||
}
|
||||
|
||||
@ -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