fix: redirect users to /refresh on unauth and mod webview links

This commit is contained in:
Christel Westerberg
2024-05-16 16:57:22 +02:00
parent 777fd1e5b6
commit 9e4f41ee46
29 changed files with 358 additions and 105 deletions

View File

@@ -1,4 +1,4 @@
import { headers } from "next/headers"
import { cookies, headers } from "next/headers"
import { Lang } from "@/constants/languages"
@@ -10,6 +10,7 @@ type CreateContextOptions = {
pathname: string
uid?: string | null
url: string
webToken: string | undefined
}
/** Use this helper for:
@@ -23,6 +24,7 @@ export function createContextInner(opts: CreateContextOptions) {
pathname: opts.pathname,
uid: opts.uid,
url: opts.url,
webToken: opts.webToken,
}
}
@@ -33,20 +35,8 @@ export function createContextInner(opts: CreateContextOptions) {
export function createContext() {
const h = headers()
// const cookie = cookies()
// const webviewTokenCookie = cookie.get("webviewToken")
// if (webviewTokenCookie) {
// // since the token exists, this is a subsequent visit
// // we're done, allow it
// return createContextInner({
// session: {
// token: { access_token: webviewTokenCookie.value },
// },
// })
// }
// const session = await auth()
const cookie = cookies()
const webviewTokenCookie = cookie.get("webviewToken")
return createContextInner({
auth,
@@ -54,6 +44,7 @@ export function createContext() {
pathname: h.get("x-pathname")!,
uid: h.get("x-uid"),
url: h.get("x-url")!,
webToken: webviewTokenCookie?.value,
})
}

View File

@@ -0,0 +1,8 @@
import { z } from "zod"
import { Lang } from "@/constants/languages"
export const getBreadcrumbsInput = z.object({
href: z.string().min(1, { message: "href is required" }),
locale: z.nativeEnum(Lang),
})

View File

@@ -4,7 +4,6 @@ import {
} from "@/lib/graphql/Query/BreadcrumbsMyPages.graphql"
import { request } from "@/lib/graphql/request"
import {
badRequestError,
internalServerError,
notFound,
} from "@/server/errors/trpc"
@@ -17,6 +16,7 @@ import {
} from "@/utils/generateTag"
import { removeMultipleSlashes } from "@/utils/url"
import { getBreadcrumbsInput } from "./input"
import {
getBreadcrumbsSchema,
validateBreadcrumbsConstenstackSchema,

View File

@@ -29,8 +29,7 @@ export const contentstackProcedure = t.procedure.use(async function (opts) {
})
export const protectedProcedure = t.procedure.use(async function (opts) {
const authRequired = opts.meta?.authRequired ?? true
const ctx = await opts.ctx
const session = ctx.session
const session = await opts.ctx.auth()
if (!authRequired && env.NODE_ENV === "development") {
console.info(
`❌❌❌❌ You are opting out of authorization, if its done on purpose maybe you should use the publicProcedure instead. ❌❌❌❌`
@@ -48,7 +47,7 @@ export const protectedProcedure = t.procedure.use(async function (opts) {
return opts.next({
ctx: {
session,
session: session || { token: { access_token: opts.ctx.webToken } },
},
})
})