Add endpoint for reading currently authenticated user (#710)

This commit is contained in:
Keri Warr 2022-08-02 03:06:23 -04:00 committed by GitHub
parent 6901507461
commit cfeb50826c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 0 deletions

View File

@ -46,6 +46,10 @@ Gets a user by their unique ID. Many other API endpoints return this as the `use
Requires no authorization. Requires no authorization.
### GET /v0/me
Returns the authenticated user.
### `GET /v0/groups` ### `GET /v0/groups`
Gets all groups, in no particular order. Gets all groups, in no particular order.

View File

@ -0,0 +1,18 @@
import { User } from 'common/user'
import * as admin from 'firebase-admin'
import { newEndpoint, APIError } from './api'
export const getcurrentuser = newEndpoint(
{ method: 'GET' },
async (_req, auth) => {
const userDoc = firestore.doc(`users/${auth.uid}`)
const [userSnap] = await firestore.getAll(userDoc)
if (!userSnap.exists) throw new APIError(400, 'User not found.')
const user = userSnap.data() as User
return user
}
)
const firestore = admin.firestore()

View File

@ -44,6 +44,7 @@ import { creategroup } from './create-group'
import { resolvemarket } from './resolve-market' 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'
const toCloudFunction = ({ opts, handler }: EndpointDefinition) => { const toCloudFunction = ({ opts, handler }: EndpointDefinition) => {
return onRequest(opts, handler as any) return onRequest(opts, handler as any)
@ -66,6 +67,7 @@ const resolveMarketFunction = toCloudFunction(resolvemarket)
const unsubscribeFunction = toCloudFunction(unsubscribe) const unsubscribeFunction = toCloudFunction(unsubscribe)
const stripeWebhookFunction = toCloudFunction(stripewebhook) const stripeWebhookFunction = toCloudFunction(stripewebhook)
const createCheckoutSessionFunction = toCloudFunction(createcheckoutsession) const createCheckoutSessionFunction = toCloudFunction(createcheckoutsession)
const getCurrentUserFunction = toCloudFunction(getcurrentuser)
export { export {
healthFunction as health, healthFunction as health,
@ -86,4 +88,5 @@ export {
unsubscribeFunction as unsubscribe, unsubscribeFunction as unsubscribe,
stripeWebhookFunction as stripewebhook, stripeWebhookFunction as stripewebhook,
createCheckoutSessionFunction as createcheckoutsession, createCheckoutSessionFunction as createcheckoutsession,
getCurrentUserFunction as getcurrentuser,
} }

View File

@ -25,6 +25,7 @@ import { creategroup } from './create-group'
import { resolvemarket } from './resolve-market' 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'
type Middleware = (req: Request, res: Response, next: NextFunction) => void type Middleware = (req: Request, res: Response, next: NextFunction) => void
const app = express() const app = express()
@ -62,6 +63,7 @@ addJsonEndpointRoute('/creategroup', creategroup)
addJsonEndpointRoute('/resolvemarket', resolvemarket) addJsonEndpointRoute('/resolvemarket', resolvemarket)
addJsonEndpointRoute('/unsubscribe', unsubscribe) addJsonEndpointRoute('/unsubscribe', unsubscribe)
addJsonEndpointRoute('/createcheckoutsession', createcheckoutsession) addJsonEndpointRoute('/createcheckoutsession', createcheckoutsession)
addJsonEndpointRoute('/getcurrentuser', getcurrentuser)
addEndpointRoute('/stripewebhook', stripewebhook, express.raw()) addEndpointRoute('/stripewebhook', stripewebhook, express.raw())
app.listen(PORT) app.listen(PORT)

View File

@ -80,3 +80,7 @@ export function claimManalink(params: any) {
export function createGroup(params: any) { export function createGroup(params: any) {
return call(getFunctionUrl('creategroup'), 'POST', params) return call(getFunctionUrl('creategroup'), 'POST', params)
} }
export function getCurrentUser(params: any) {
return call(getFunctionUrl('getcurrentuser'), 'GET', params)
}

25
web/pages/api/v0/me.ts Normal file
View File

@ -0,0 +1,25 @@
import { User } from 'common/user'
import { NextApiRequest, NextApiResponse } from 'next'
import { fetchBackend } from 'web/lib/api/proxy'
import { LiteUser, ApiError, toLiteUser } from './_types'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<LiteUser | ApiError>
) {
try {
const backendRes = await fetchBackend(req, 'getcurrentuser')
const user = (await backendRes.json()) as User
if (!user) {
res.status(404).json({ error: 'User not found' })
return
}
res.setHeader('Cache-Control', 'no-cache')
res.status(200).json(toLiteUser(user))
return
} catch (err) {
console.error('Error talking to cloud function: ', err)
res.status(500).json({ error: 'Error communicating with backend.' })
}
}