Skip to content

Commit

Permalink
Add admin impersonation feature in dev
Browse files Browse the repository at this point in the history
This gives devs the ability to impersonate users to reproduce reported bugs
  • Loading branch information
maxwofford committed Nov 13, 2024
1 parent 8a5b24b commit d726269
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/app/api/admin/impersonate/[slackId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getSelfPerson } from '@/app/utils/airtable'
import { redirect } from 'next/navigation'
import { NextRequest } from 'next/server'

import { HsSession, signAndSet } from '@/app/utils/auth'

export async function GET(
_request: NextRequest,
{ params }: { params: { slackId: string } },
) {
if (process.env.NODE_ENV === 'development') {
// only allow impersonation in development while testing
const { slackId } = params
// look for airtable user with this record
const person = await getSelfPerson(slackId)
const id = person.id
const email = person.fields.email

const session: HsSession = {
personId: id,
authType: 'impersonation',
slackId,
email,
}

await signAndSet(session)
}

redirect('/signpost')
}
4 changes: 2 additions & 2 deletions src/app/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface HsSession {
/// The Person record ID in the high seas base
personId: string

authType: 'slack-oauth' | 'magic-link'
authType: 'slack-oauth' | 'magic-link' | 'impersonation'
slackId: string
name?: string
firstName?: string
Expand Down Expand Up @@ -75,7 +75,7 @@ async function hashSession(session: HsSession) {
return hashHex
}

async function signAndSet(session: HsSession) {
export async function signAndSet(session: HsSession) {
session.sig = await hashSession(session)

cookies().set(sessionCookieName, JSON.stringify(session), {
Expand Down

0 comments on commit d726269

Please sign in to comment.