refactor(SW-188): replace anon or auth procedure with serviceProcedure

This commit is contained in:
Chuma McPhoy
2024-08-13 15:59:07 +02:00
parent a1a0a73e3a
commit ed379202c8
7 changed files with 55 additions and 80 deletions
+16 -29
View File
@@ -4,27 +4,23 @@ import { ServiceTokenResponse } from "@/types/tokens"
const SERVICE_TOKEN_REVALIDATE_SECONDS = 3599 // 59 minutes and 59 seconds.
async function fetchServiceToken(): Promise<ServiceTokenResponse> {
export async function fetchServiceToken(): Promise<ServiceTokenResponse> {
try {
const response = await fetch(
`${env.CURITY_ISSUER_SERVICE}/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,
scope: "hotel",
}),
next: {
revalidate: SERVICE_TOKEN_REVALIDATE_SECONDS,
},
}
)
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")
@@ -36,12 +32,3 @@ async function fetchServiceToken(): Promise<ServiceTokenResponse> {
throw error
}
}
export async function getAuthToken(userToken?: string | null): Promise<string> {
if (userToken) {
return userToken
}
const { access_token } = await fetchServiceToken()
return access_token
}