Add cloud function to get custom token from API auth
This commit is contained in:
parent
e0196f7107
commit
84a3a26c01
|
@ -78,6 +78,19 @@ export const lookupUser = async (creds: Credentials): Promise<AuthedUser> => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const writeResponseError = (e: unknown, res: Response) => {
|
||||||
|
if (e instanceof APIError) {
|
||||||
|
const output: { [k: string]: unknown } = { message: e.message }
|
||||||
|
if (e.details != null) {
|
||||||
|
output.details = e.details
|
||||||
|
}
|
||||||
|
res.status(e.code).json(output)
|
||||||
|
} else {
|
||||||
|
error(e)
|
||||||
|
res.status(500).json({ message: 'An unknown error occurred.' })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const zTimestamp = () => {
|
export const zTimestamp = () => {
|
||||||
return z.preprocess((arg) => {
|
return z.preprocess((arg) => {
|
||||||
return typeof arg == 'number' ? new Date(arg) : undefined
|
return typeof arg == 'number' ? new Date(arg) : undefined
|
||||||
|
@ -131,16 +144,7 @@ export const newEndpoint = (endpointOpts: EndpointOptions, fn: Handler) => {
|
||||||
const authedUser = await lookupUser(await parseCredentials(req))
|
const authedUser = await lookupUser(await parseCredentials(req))
|
||||||
res.status(200).json(await fn(req, authedUser))
|
res.status(200).json(await fn(req, authedUser))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof APIError) {
|
writeResponseError(e, res)
|
||||||
const output: { [k: string]: unknown } = { message: e.message }
|
|
||||||
if (e.details != null) {
|
|
||||||
output.details = e.details
|
|
||||||
}
|
|
||||||
res.status(e.code).json(output)
|
|
||||||
} else {
|
|
||||||
error(e)
|
|
||||||
res.status(500).json({ message: 'An unknown error occurred.' })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
} as EndpointDefinition
|
} as EndpointDefinition
|
||||||
|
|
27
functions/src/get-custom-token.ts
Normal file
27
functions/src/get-custom-token.ts
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import * as admin from 'firebase-admin'
|
||||||
|
import {
|
||||||
|
APIError,
|
||||||
|
EndpointDefinition,
|
||||||
|
lookupUser,
|
||||||
|
parseCredentials,
|
||||||
|
writeResponseError,
|
||||||
|
} from './api'
|
||||||
|
|
||||||
|
const opts = { method: 'GET', minInstances: 1 }
|
||||||
|
|
||||||
|
export const getcustomtoken: EndpointDefinition = {
|
||||||
|
opts,
|
||||||
|
handler: async (req, res) => {
|
||||||
|
try {
|
||||||
|
const credentials = await parseCredentials(req)
|
||||||
|
if (credentials.kind != 'jwt') {
|
||||||
|
throw new APIError(403, 'API keys cannot mint custom tokens.')
|
||||||
|
}
|
||||||
|
const user = await lookupUser(credentials)
|
||||||
|
const token = await admin.auth().createCustomToken(user.uid)
|
||||||
|
res.status(200).json({ token: token })
|
||||||
|
} catch (e) {
|
||||||
|
writeResponseError(e, res)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
|
@ -65,6 +65,7 @@ import { unsubscribe } from './unsubscribe'
|
||||||
import { stripewebhook, createcheckoutsession } from './stripe'
|
import { stripewebhook, createcheckoutsession } from './stripe'
|
||||||
import { getcurrentuser } from './get-current-user'
|
import { getcurrentuser } from './get-current-user'
|
||||||
import { acceptchallenge } from './accept-challenge'
|
import { acceptchallenge } from './accept-challenge'
|
||||||
|
import { getcustomtoken } from './get-custom-token'
|
||||||
|
|
||||||
const toCloudFunction = ({ opts, handler }: EndpointDefinition) => {
|
const toCloudFunction = ({ opts, handler }: EndpointDefinition) => {
|
||||||
return onRequest(opts, handler as any)
|
return onRequest(opts, handler as any)
|
||||||
|
@ -89,6 +90,7 @@ const stripeWebhookFunction = toCloudFunction(stripewebhook)
|
||||||
const createCheckoutSessionFunction = toCloudFunction(createcheckoutsession)
|
const createCheckoutSessionFunction = toCloudFunction(createcheckoutsession)
|
||||||
const getCurrentUserFunction = toCloudFunction(getcurrentuser)
|
const getCurrentUserFunction = toCloudFunction(getcurrentuser)
|
||||||
const acceptChallenge = toCloudFunction(acceptchallenge)
|
const acceptChallenge = toCloudFunction(acceptchallenge)
|
||||||
|
const getCustomTokenFunction = toCloudFunction(getcustomtoken)
|
||||||
|
|
||||||
export {
|
export {
|
||||||
healthFunction as health,
|
healthFunction as health,
|
||||||
|
@ -111,4 +113,5 @@ export {
|
||||||
createCheckoutSessionFunction as createcheckoutsession,
|
createCheckoutSessionFunction as createcheckoutsession,
|
||||||
getCurrentUserFunction as getcurrentuser,
|
getCurrentUserFunction as getcurrentuser,
|
||||||
acceptChallenge as acceptchallenge,
|
acceptChallenge as acceptchallenge,
|
||||||
|
getCustomTokenFunction as getcustomtoken,
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import { resolvemarket } from './resolve-market'
|
||||||
import { unsubscribe } from './unsubscribe'
|
import { unsubscribe } from './unsubscribe'
|
||||||
import { stripewebhook, createcheckoutsession } from './stripe'
|
import { stripewebhook, createcheckoutsession } from './stripe'
|
||||||
import { getcurrentuser } from './get-current-user'
|
import { getcurrentuser } from './get-current-user'
|
||||||
|
import { getcustomtoken } from './get-custom-token'
|
||||||
|
|
||||||
type Middleware = (req: Request, res: Response, next: NextFunction) => void
|
type Middleware = (req: Request, res: Response, next: NextFunction) => void
|
||||||
const app = express()
|
const app = express()
|
||||||
|
@ -64,6 +65,7 @@ addJsonEndpointRoute('/resolvemarket', resolvemarket)
|
||||||
addJsonEndpointRoute('/unsubscribe', unsubscribe)
|
addJsonEndpointRoute('/unsubscribe', unsubscribe)
|
||||||
addJsonEndpointRoute('/createcheckoutsession', createcheckoutsession)
|
addJsonEndpointRoute('/createcheckoutsession', createcheckoutsession)
|
||||||
addJsonEndpointRoute('/getcurrentuser', getcurrentuser)
|
addJsonEndpointRoute('/getcurrentuser', getcurrentuser)
|
||||||
|
addEndpointRoute('/getcustomtoken', getcustomtoken)
|
||||||
addEndpointRoute('/stripewebhook', stripewebhook, express.raw())
|
addEndpointRoute('/stripewebhook', stripewebhook, express.raw())
|
||||||
|
|
||||||
app.listen(PORT)
|
app.listen(PORT)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user