Feat/SW-3549 pass scandic token * WIP pass scandic token * pass scandic token when booking * Merge branch 'master' of bitbucket.org:scandic-swap/web into feat/SW-3549-pass-scandic-token * pass user token when doing availability search * undo changes * merge * Merged in fix/sw-3551-rsc-bookingflowconfig (pull request #2988) fix(SW-3551): Fix issue with BookingConfigProvider in RSC * wip move config to pages * Move config providing to pages * Merged in fix/update-promo-error-modal-text (pull request #2990) fix: update promo error modal text * fix: update promo error modal text Approved-by: Emma Zettervall * Merged in fix/sw-3514-missing-membership-input-for-multiroom (pull request #2991) fix(SW-3514): Show join Scandic Friends card for SAS multiroom * Show join card for room 2+ Approved-by: Hrishikesh Vaipurkar * Merged in feat/lokalise-rebuild (pull request #2993) Feat/lokalise rebuild * chore(lokalise): update translation ids * chore(lokalise): easier to switch between projects * chore(lokalise): update translation ids * . * . * . * . * . * . * chore(lokalise): update translation ids * chore(lokalise): update translation ids * . * . * . * chore(lokalise): update translation ids * chore(lokalise): update translation ids * . * . * chore(lokalise): update translation ids * chore(lokalise): update translation ids * chore(lokalise): new translations * merge * switch to errors for missing id's * merge * sync translations Approved-by: Linus Flood * Merged in feat/SW-3552-logout-from-social-session-when- (pull request #2994) feat(SW-3552): Removed scandic session on logout Approved-by: Joakim Jäderberg * merge * replace getRedemptionTokenSafely() with context based instead * Refactor user verification and error handling across multiple components; implement safeTry utility for safer async calls * Refactor user verification and error handling across multiple components; implement safeTry utility for safer async calls * merge * Merge branch 'master' of bitbucket.org:scandic-swap/web into feat/SW-3549-pass-scandic-token * add booking scope remove unused getMembershipNumber() Approved-by: Anton Gunnarsson Approved-by: Hrishikesh Vaipurkar
174 lines
4.5 KiB
TypeScript
174 lines
4.5 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
|
|
import { Lang } from "@scandic-hotels/common/constants/language"
|
|
import { logger } from "@scandic-hotels/common/logger"
|
|
import { getServiceToken } from "@scandic-hotels/common/tokenManager"
|
|
|
|
import { env } from "../env/server"
|
|
import {
|
|
badRequestError,
|
|
internalServerError,
|
|
sessionExpiredError,
|
|
unauthorizedError,
|
|
} from "./errors"
|
|
import { langInput } from "./utils"
|
|
import { middleware, procedure } from "."
|
|
|
|
const sentryMiddleware = middleware(
|
|
Sentry.trpcMiddleware({
|
|
attachRpcInput: true,
|
|
})
|
|
)
|
|
|
|
export const baseProcedure = procedure.use(sentryMiddleware)
|
|
|
|
export const publicProcedure = baseProcedure
|
|
|
|
export const languageProcedure = baseProcedure.use(async function (opts) {
|
|
if (!opts.ctx.lang) {
|
|
// When fetching data client side with TRPC we don't pass through middlewares and therefore do not get the lang through headers
|
|
// We can then pass lang as an input in the request and set it to the context in the procedure
|
|
|
|
const input = await opts.getRawInput()
|
|
const parsedInput = langInput.safeParse(input)
|
|
|
|
if (process.env.NODE_ENV === "development" && !parsedInput.success) {
|
|
throw badRequestError({
|
|
message: "Missing lang in tRPC context",
|
|
path: opts.path,
|
|
type: opts.type,
|
|
input: opts.input,
|
|
})
|
|
}
|
|
|
|
return opts.next({
|
|
ctx: {
|
|
lang: parsedInput.success ? parsedInput.data.lang : Lang.en,
|
|
},
|
|
})
|
|
}
|
|
|
|
return opts.next({
|
|
ctx: {
|
|
lang: opts.ctx.lang,
|
|
},
|
|
})
|
|
})
|
|
|
|
export const contentstackBaseProcedure = languageProcedure
|
|
|
|
export const contentstackExtendedProcedureUID = contentstackBaseProcedure.use(
|
|
async function (opts) {
|
|
if (!opts.ctx.uid) {
|
|
throw badRequestError("Missing UID in tRPC context")
|
|
}
|
|
|
|
return opts.next({
|
|
ctx: {
|
|
uid: opts.ctx.uid,
|
|
},
|
|
})
|
|
}
|
|
)
|
|
|
|
export const protectedProcedure = baseProcedure.use(async function (opts) {
|
|
const authRequired = opts.meta?.authRequired ?? true
|
|
const session = await opts.ctx.auth()
|
|
|
|
if (!authRequired && env.NODE_ENV === "development") {
|
|
logger.debug(
|
|
`❌❌❌❌ You are opting out of authorization, if its done on purpose maybe you should use the publicProcedure instead. ❌❌❌❌`
|
|
)
|
|
logger.debug(`path: ${opts.path} | type: ${opts.type}`)
|
|
}
|
|
|
|
if (!session) {
|
|
throw unauthorizedError()
|
|
}
|
|
|
|
if (session?.error === "RefreshAccessTokenError") {
|
|
throw sessionExpiredError()
|
|
}
|
|
|
|
return opts.next({
|
|
ctx: {
|
|
session,
|
|
},
|
|
})
|
|
})
|
|
|
|
export const safeProtectedProcedure = baseProcedure.use(async function (opts) {
|
|
const authRequired = opts.meta?.authRequired ?? true
|
|
let session = await opts.ctx.auth()
|
|
if (!authRequired && env.NODE_ENV === "development") {
|
|
logger.debug(
|
|
`❌❌❌❌ You are opting out of authorization, if its done on purpose maybe you should use the publicProcedure instead. ❌❌❌❌`
|
|
)
|
|
logger.debug(`path: ${opts.path} | type: ${opts.type}`)
|
|
}
|
|
|
|
if (!session || session.error === "RefreshAccessTokenError") {
|
|
session = null
|
|
}
|
|
|
|
return opts.next({
|
|
ctx: {
|
|
session,
|
|
},
|
|
})
|
|
})
|
|
|
|
export const serviceProcedure = baseProcedure.use(async (opts) => {
|
|
const token = await getServiceToken()
|
|
const { access_token } = token
|
|
if (!access_token) {
|
|
throw internalServerError(`[serviceProcedure] No service token`)
|
|
}
|
|
|
|
return opts.next({
|
|
ctx: {
|
|
serviceToken: access_token,
|
|
},
|
|
})
|
|
})
|
|
|
|
export const contentStackUidWithServiceProcedure =
|
|
contentstackExtendedProcedureUID.concat(serviceProcedure)
|
|
|
|
export const contentStackBaseWithServiceProcedure =
|
|
contentstackBaseProcedure.concat(serviceProcedure)
|
|
|
|
export const contentStackBaseWithProtectedProcedure =
|
|
contentstackBaseProcedure.concat(protectedProcedure)
|
|
|
|
export const safeProtectedServiceProcedure =
|
|
safeProtectedProcedure.concat(serviceProcedure)
|
|
|
|
export const languageProtectedProcedure =
|
|
protectedProcedure.concat(languageProcedure)
|
|
|
|
type ExperimentalProcedureCaller = ReturnType<
|
|
typeof baseProcedure.experimental_caller
|
|
>
|
|
export function getProtectedServerActionProcedure(
|
|
serverActionProcedure: ExperimentalProcedureCaller
|
|
) {
|
|
return serverActionProcedure.use(async (opts) => {
|
|
const session = await opts.ctx.auth()
|
|
if (!session) {
|
|
throw unauthorizedError()
|
|
}
|
|
|
|
if (session && session.error === "RefreshAccessTokenError") {
|
|
throw sessionExpiredError()
|
|
}
|
|
|
|
return opts.next({
|
|
ctx: {
|
|
...opts.ctx,
|
|
session,
|
|
},
|
|
})
|
|
})
|
|
}
|