codeflash-internal/js/cf-api/auth0.ts

24 lines
726 B
TypeScript

import fetch, { type RequestInit, type Response } from "node-fetch"
// TODO use node-auth0 sdk for this
export async function userNickname(userId: string): Promise<string | null> {
const url = new URL(`${process.env.AUTH0_ISSUER_BASE_URL}/api/v2/users/${userId}`)
const params = new URLSearchParams({
fields: "nickname",
include_fields: "true",
})
url.search = params.toString()
const options: RequestInit = {
method: "GET",
headers: {
authorization: `Bearer ${process.env.AUTH0_MANAGEMENT_API_TOKEN}`,
},
}
const response: Response = await fetch(url, options)
if (response.ok) {
const user: { nickname } = await response.json()
return user.nickname
} else return null
}