feat: update getHotel to use real hotel api endpoint, support for service tokens, type modifications
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { env } from "@/env/server"
|
||||
|
||||
import { ServiceTokenResponse } from "@/types/tokens"
|
||||
|
||||
const SERVICE_TOKEN_REVALIDATE_SECONDS = 3599 // 59 minutes and 59 seconds.
|
||||
|
||||
async function fetchServiceToken(): Promise<ServiceTokenResponse> {
|
||||
try {
|
||||
const response = await fetch(`${env.CURITY_ISSUER_USER}/oauth/v2/token`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Accept: "application/json",
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
grant_type: "client_credentials",
|
||||
client_id: env.CURITY_CLIENT_ID_SERVICE,
|
||||
client_secret: env.CURITY_CLIENT_SECRET_SERVICE,
|
||||
}),
|
||||
next: {
|
||||
revalidate: SERVICE_TOKEN_REVALIDATE_SECONDS,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to obtain service token")
|
||||
}
|
||||
|
||||
return response.json()
|
||||
} catch (error) {
|
||||
console.error("Error fetching service token:", error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAuthToken(userToken?: string | null): Promise<string> {
|
||||
if (userToken) {
|
||||
return userToken
|
||||
}
|
||||
|
||||
const { access_token } = await fetchServiceToken()
|
||||
return access_token
|
||||
}
|
||||
Reference in New Issue
Block a user