templating api
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
# api/src/integrations/README.md
|
||||
|
||||
Integrations are api integrations with external services.
|
||||
They might package a bunch of api calls, or just one.
|
||||
@@ -0,0 +1,56 @@
|
||||
import axios from "axios"
|
||||
|
||||
import { AUTH0_DOMAIN } from "@/config"
|
||||
|
||||
const auth0Api = axios.create({
|
||||
baseURL: AUTH0_DOMAIN,
|
||||
})
|
||||
|
||||
export interface Auth0UserInfo {
|
||||
email: string
|
||||
firstName: string
|
||||
lastName: string
|
||||
auth0Subject: string
|
||||
}
|
||||
|
||||
export interface Auth0Response {
|
||||
sub: string // "auth0|6241ec44e5b4a700693df293"
|
||||
given_name: string // "Jane"
|
||||
family_name: string // "Doe"
|
||||
nickname: string // "Jane"
|
||||
name: string // "Jane Doe"
|
||||
picture: string // https://s.gravatar.com/avatar/1234567890abcdef1234567890abcdef?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fmb.png
|
||||
updated_at: string // "2023-10-30T17:25:52.975Z"
|
||||
email: string // "janedoe@gmail.com"
|
||||
email_verified: boolean // true
|
||||
oid?: string // 11111111-2222-3333-4444-555555555555"
|
||||
}
|
||||
|
||||
export class Auth0PayloadError extends Error {
|
||||
constructor(data: unknown) {
|
||||
super(`Payload from Auth0 is strange or failed for: ${JSON.stringify(data)}`)
|
||||
this.name = "Auth0PayloadError"
|
||||
}
|
||||
}
|
||||
|
||||
export const auth0Integration = {
|
||||
async getUserInfo(token: string): Promise<Auth0UserInfo> {
|
||||
const { data }: { data: Auth0Response } = await auth0Api.get("/userinfo", {
|
||||
headers: { authorization: token },
|
||||
})
|
||||
|
||||
const firstName = data.given_name || "UNKNOWN"
|
||||
const lastName = data.family_name || "UNKNOWN"
|
||||
const fallbackEmail = `${firstName}.${lastName}@yukon-no-email.ca`
|
||||
const email = data.email || fallbackEmail
|
||||
|
||||
return {
|
||||
auth0Subject: data.sub,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default auth0Integration
|
||||
@@ -0,0 +1,5 @@
|
||||
export {
|
||||
auth0Integration,
|
||||
Auth0PayloadError,
|
||||
type Auth0UserInfo,
|
||||
} from "./auth0-integration"
|
||||
Reference in New Issue
Block a user