Merged in fix/SW-2631-check-session-expiry (pull request #2004)
fix(SW-2631): check if session is valid otherwise use service token * fix: check if session is valid otherwise use service token * fix: only use service token for queries and updated mutations to check for valid user token Approved-by: Michael Zetterberg
This commit is contained in:
@@ -16,6 +16,7 @@ import { auth } from "@/auth"
|
|||||||
import HandleErrorCallback from "@/components/HotelReservation/EnterDetails/Payment/PaymentCallback/HandleErrorCallback"
|
import HandleErrorCallback from "@/components/HotelReservation/EnterDetails/Payment/PaymentCallback/HandleErrorCallback"
|
||||||
import HandleSuccessCallback from "@/components/HotelReservation/EnterDetails/Payment/PaymentCallback/HandleSuccessCallback"
|
import HandleSuccessCallback from "@/components/HotelReservation/EnterDetails/Payment/PaymentCallback/HandleSuccessCallback"
|
||||||
import { encrypt } from "@/utils/encryption"
|
import { encrypt } from "@/utils/encryption"
|
||||||
|
import { isValidSession } from "@/utils/session"
|
||||||
|
|
||||||
import type { LangParams, PageArgs } from "@/types/params"
|
import type { LangParams, PageArgs } from "@/types/params"
|
||||||
|
|
||||||
@@ -41,7 +42,7 @@ export default async function PaymentCallbackPage({
|
|||||||
|
|
||||||
let token = ""
|
let token = ""
|
||||||
const session = await auth()
|
const session = await auth()
|
||||||
if (session) {
|
if (isValidSession(session)) {
|
||||||
token = session.token.access_token
|
token = session.token.access_token
|
||||||
} else {
|
} else {
|
||||||
const serviceToken = await getServiceToken()
|
const serviceToken = await getServiceToken()
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { createCounter } from "@/server/telemetry"
|
|||||||
import { router, safeProtectedServiceProcedure } from "@/server/trpc"
|
import { router, safeProtectedServiceProcedure } from "@/server/trpc"
|
||||||
|
|
||||||
import { encrypt } from "@/utils/encryption"
|
import { encrypt } from "@/utils/encryption"
|
||||||
|
import { isValidSession } from "@/utils/session"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
addPackageInput,
|
addPackageInput,
|
||||||
@@ -22,8 +23,18 @@ const refIdPlugin = createRefIdPlugin()
|
|||||||
export const bookingMutationRouter = router({
|
export const bookingMutationRouter = router({
|
||||||
create: safeProtectedServiceProcedure
|
create: safeProtectedServiceProcedure
|
||||||
.input(createBookingInput)
|
.input(createBookingInput)
|
||||||
|
.use(async ({ ctx, next }) => {
|
||||||
|
const token = isValidSession(ctx.session)
|
||||||
|
? ctx.session.token.access_token
|
||||||
|
: ctx.serviceToken
|
||||||
|
|
||||||
|
return next({
|
||||||
|
ctx: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
.mutation(async function ({ ctx, input }) {
|
.mutation(async function ({ ctx, input }) {
|
||||||
const accessToken = ctx.session?.token.access_token ?? ctx.serviceToken
|
|
||||||
const { language, ...inputWithoutLang } = input
|
const { language, ...inputWithoutLang } = input
|
||||||
const { hotelId, checkInDate, checkOutDate } = inputWithoutLang
|
const { hotelId, checkInDate, checkOutDate } = inputWithoutLang
|
||||||
|
|
||||||
@@ -39,7 +50,7 @@ export const bookingMutationRouter = router({
|
|||||||
metricsCreateBooking.start()
|
metricsCreateBooking.start()
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
Authorization: `Bearer ${accessToken}`,
|
Authorization: `Bearer ${ctx.token}`,
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiResponse = await api.post(
|
const apiResponse = await api.post(
|
||||||
@@ -82,18 +93,27 @@ export const bookingMutationRouter = router({
|
|||||||
}),
|
}),
|
||||||
priceChange: safeProtectedServiceProcedure
|
priceChange: safeProtectedServiceProcedure
|
||||||
.concat(refIdPlugin.toConfirmationNumber)
|
.concat(refIdPlugin.toConfirmationNumber)
|
||||||
|
.use(async ({ ctx, next }) => {
|
||||||
|
const token = isValidSession(ctx.session)
|
||||||
|
? ctx.session.token.access_token
|
||||||
|
: ctx.serviceToken
|
||||||
|
|
||||||
|
return next({
|
||||||
|
ctx: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
.mutation(async function ({ ctx }) {
|
.mutation(async function ({ ctx }) {
|
||||||
const { confirmationNumber } = ctx
|
const { confirmationNumber, token } = ctx
|
||||||
|
|
||||||
const priceChangeCounter = createCounter("trpc.booking", "price-change")
|
const priceChangeCounter = createCounter("trpc.booking", "price-change")
|
||||||
const metricsPriceChange = priceChangeCounter.init({ confirmationNumber })
|
const metricsPriceChange = priceChangeCounter.init({ confirmationNumber })
|
||||||
|
|
||||||
metricsPriceChange.start()
|
metricsPriceChange.start()
|
||||||
|
|
||||||
const accessToken = ctx.session?.token.access_token ?? ctx.serviceToken
|
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
Authorization: `Bearer ${accessToken}`,
|
Authorization: `Bearer ${token}`,
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiResponse = await api.put(
|
const apiResponse = await api.put(
|
||||||
@@ -122,9 +142,19 @@ export const bookingMutationRouter = router({
|
|||||||
cancel: safeProtectedServiceProcedure
|
cancel: safeProtectedServiceProcedure
|
||||||
.input(cancelBookingsInput)
|
.input(cancelBookingsInput)
|
||||||
.concat(refIdPlugin.toConfirmationNumbers)
|
.concat(refIdPlugin.toConfirmationNumbers)
|
||||||
|
.use(async ({ ctx, next }) => {
|
||||||
|
const token = isValidSession(ctx.session)
|
||||||
|
? ctx.session.token.access_token
|
||||||
|
: ctx.serviceToken
|
||||||
|
|
||||||
|
return next({
|
||||||
|
ctx: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
.mutation(async function ({ ctx, input }) {
|
.mutation(async function ({ ctx, input }) {
|
||||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
const { confirmationNumbers, token } = ctx
|
||||||
const { confirmationNumbers } = ctx
|
|
||||||
const { language } = input
|
const { language } = input
|
||||||
|
|
||||||
const responses = await Promise.allSettled(
|
const responses = await Promise.allSettled(
|
||||||
@@ -155,9 +185,19 @@ export const bookingMutationRouter = router({
|
|||||||
packages: safeProtectedServiceProcedure
|
packages: safeProtectedServiceProcedure
|
||||||
.input(addPackageInput)
|
.input(addPackageInput)
|
||||||
.concat(refIdPlugin.toConfirmationNumber)
|
.concat(refIdPlugin.toConfirmationNumber)
|
||||||
|
.use(async ({ ctx, next }) => {
|
||||||
|
const token = isValidSession(ctx.session)
|
||||||
|
? ctx.session.token.access_token
|
||||||
|
: ctx.serviceToken
|
||||||
|
|
||||||
|
return next({
|
||||||
|
ctx: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
.mutation(async function ({ ctx, input }) {
|
.mutation(async function ({ ctx, input }) {
|
||||||
const accessToken = ctx.session?.token.access_token ?? ctx.serviceToken
|
const { confirmationNumber, token } = ctx
|
||||||
const { confirmationNumber } = ctx
|
|
||||||
const { language, refId, ...body } = input
|
const { language, refId, ...body } = input
|
||||||
|
|
||||||
const addPackageCounter = createCounter("trpc.booking", "package.add")
|
const addPackageCounter = createCounter("trpc.booking", "package.add")
|
||||||
@@ -169,7 +209,7 @@ export const bookingMutationRouter = router({
|
|||||||
metricsAddPackage.start()
|
metricsAddPackage.start()
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
Authorization: `Bearer ${accessToken}`,
|
Authorization: `Bearer ${token}`,
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiResponse = await api.post(
|
const apiResponse = await api.post(
|
||||||
@@ -200,9 +240,19 @@ export const bookingMutationRouter = router({
|
|||||||
guarantee: safeProtectedServiceProcedure
|
guarantee: safeProtectedServiceProcedure
|
||||||
.input(guaranteeBookingInput)
|
.input(guaranteeBookingInput)
|
||||||
.concat(refIdPlugin.toConfirmationNumber)
|
.concat(refIdPlugin.toConfirmationNumber)
|
||||||
|
.use(async ({ ctx, next }) => {
|
||||||
|
const token = isValidSession(ctx.session)
|
||||||
|
? ctx.session.token.access_token
|
||||||
|
: ctx.serviceToken
|
||||||
|
|
||||||
|
return next({
|
||||||
|
ctx: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
.mutation(async function ({ ctx, input }) {
|
.mutation(async function ({ ctx, input }) {
|
||||||
const accessToken = ctx.session?.token.access_token ?? ctx.serviceToken
|
const { confirmationNumber, token } = ctx
|
||||||
const { confirmationNumber } = ctx
|
|
||||||
const { language, refId, ...body } = input
|
const { language, refId, ...body } = input
|
||||||
|
|
||||||
const guaranteeBookingCounter = createCounter("trpc.booking", "guarantee")
|
const guaranteeBookingCounter = createCounter("trpc.booking", "guarantee")
|
||||||
@@ -214,7 +264,7 @@ export const bookingMutationRouter = router({
|
|||||||
metricsGuaranteeBooking.start()
|
metricsGuaranteeBooking.start()
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
Authorization: `Bearer ${accessToken}`,
|
Authorization: `Bearer ${token}`,
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiResponse = await api.put(
|
const apiResponse = await api.put(
|
||||||
@@ -245,9 +295,19 @@ export const bookingMutationRouter = router({
|
|||||||
update: safeProtectedServiceProcedure
|
update: safeProtectedServiceProcedure
|
||||||
.input(updateBookingInput)
|
.input(updateBookingInput)
|
||||||
.concat(refIdPlugin.toConfirmationNumber)
|
.concat(refIdPlugin.toConfirmationNumber)
|
||||||
|
.use(async ({ ctx, next }) => {
|
||||||
|
const token = isValidSession(ctx.session)
|
||||||
|
? ctx.session.token.access_token
|
||||||
|
: ctx.serviceToken
|
||||||
|
|
||||||
|
return next({
|
||||||
|
ctx: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
.mutation(async function ({ ctx, input }) {
|
.mutation(async function ({ ctx, input }) {
|
||||||
const accessToken = ctx.session?.token.access_token || ctx.serviceToken
|
const { confirmationNumber, token } = ctx
|
||||||
const { confirmationNumber } = ctx
|
|
||||||
const { language, refId, ...body } = input
|
const { language, refId, ...body } = input
|
||||||
|
|
||||||
const updateBookingCounter = createCounter("trpc.booking", "update")
|
const updateBookingCounter = createCounter("trpc.booking", "update")
|
||||||
@@ -263,7 +323,7 @@ export const bookingMutationRouter = router({
|
|||||||
{
|
{
|
||||||
body,
|
body,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${accessToken}`,
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ language }
|
{ language }
|
||||||
@@ -289,9 +349,19 @@ export const bookingMutationRouter = router({
|
|||||||
removePackage: safeProtectedServiceProcedure
|
removePackage: safeProtectedServiceProcedure
|
||||||
.input(removePackageInput)
|
.input(removePackageInput)
|
||||||
.concat(refIdPlugin.toConfirmationNumber)
|
.concat(refIdPlugin.toConfirmationNumber)
|
||||||
|
.use(async ({ ctx, next }) => {
|
||||||
|
const token = isValidSession(ctx.session)
|
||||||
|
? ctx.session.token.access_token
|
||||||
|
: ctx.serviceToken
|
||||||
|
|
||||||
|
return next({
|
||||||
|
ctx: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
.mutation(async function ({ ctx, input }) {
|
.mutation(async function ({ ctx, input }) {
|
||||||
const accessToken = ctx.session?.token.access_token ?? ctx.serviceToken
|
const { confirmationNumber, token } = ctx
|
||||||
const { confirmationNumber } = ctx
|
|
||||||
const { codes, language } = input
|
const { codes, language } = input
|
||||||
|
|
||||||
const removePackageCounter = createCounter(
|
const removePackageCounter = createCounter(
|
||||||
@@ -307,7 +377,7 @@ export const bookingMutationRouter = router({
|
|||||||
metricsRemovePackage.start()
|
metricsRemovePackage.start()
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
Authorization: `Bearer ${accessToken}`,
|
Authorization: `Bearer ${token}`,
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiResponse = await api.remove(
|
const apiResponse = await api.remove(
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ export const bookingQueryRouter = router({
|
|||||||
.concat(refIdPlugin.toConfirmationNumber)
|
.concat(refIdPlugin.toConfirmationNumber)
|
||||||
.use(async ({ ctx, input, next }) => {
|
.use(async ({ ctx, input, next }) => {
|
||||||
const lang = input.lang ?? ctx.lang
|
const lang = input.lang ?? ctx.lang
|
||||||
|
|
||||||
return next({
|
return next({
|
||||||
ctx: {
|
ctx: {
|
||||||
lang,
|
lang,
|
||||||
@@ -88,7 +89,6 @@ export const bookingQueryRouter = router({
|
|||||||
}),
|
}),
|
||||||
findBooking: safeProtectedServiceProcedure
|
findBooking: safeProtectedServiceProcedure
|
||||||
.input(findBookingInput)
|
.input(findBookingInput)
|
||||||
|
|
||||||
.query(async function ({
|
.query(async function ({
|
||||||
ctx,
|
ctx,
|
||||||
input: { confirmationNumber, lastName, firstName, email },
|
input: { confirmationNumber, lastName, firstName, email },
|
||||||
@@ -151,16 +151,14 @@ export const bookingQueryRouter = router({
|
|||||||
.concat(refIdPlugin.toConfirmationNumber)
|
.concat(refIdPlugin.toConfirmationNumber)
|
||||||
.use(async ({ ctx, input, next }) => {
|
.use(async ({ ctx, input, next }) => {
|
||||||
const lang = input.lang ?? ctx.lang
|
const lang = input.lang ?? ctx.lang
|
||||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
|
||||||
return next({
|
return next({
|
||||||
ctx: {
|
ctx: {
|
||||||
lang,
|
lang,
|
||||||
token,
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.query(async function ({ ctx }) {
|
.query(async function ({ ctx }) {
|
||||||
const { confirmationNumber, lang, token } = ctx
|
const { confirmationNumber, lang, serviceToken } = ctx
|
||||||
|
|
||||||
const getLinkedReservationsCounter = createCounter(
|
const getLinkedReservationsCounter = createCounter(
|
||||||
"trpc.booking",
|
"trpc.booking",
|
||||||
@@ -172,7 +170,7 @@ export const bookingQueryRouter = router({
|
|||||||
|
|
||||||
metricsGetLinkedReservations.start()
|
metricsGetLinkedReservations.start()
|
||||||
|
|
||||||
const booking = await getBooking(confirmationNumber, lang, token)
|
const booking = await getBooking(confirmationNumber, lang, serviceToken)
|
||||||
|
|
||||||
if (!booking) {
|
if (!booking) {
|
||||||
return []
|
return []
|
||||||
@@ -180,7 +178,7 @@ export const bookingQueryRouter = router({
|
|||||||
|
|
||||||
const linkedReservationsResults = await Promise.allSettled(
|
const linkedReservationsResults = await Promise.allSettled(
|
||||||
booking.linkedReservations.map((linkedReservation) =>
|
booking.linkedReservations.map((linkedReservation) =>
|
||||||
getBooking(linkedReservation.confirmationNumber, lang, token)
|
getBooking(linkedReservation.confirmationNumber, lang, serviceToken)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user