Skip to content

Commit

Permalink
approach this from a different angle, introduce a service user
Browse files Browse the repository at this point in the history
  • Loading branch information
slewis74 committed Oct 25, 2024
1 parent 74465e2 commit 946be6a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/User.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import type { JwtPayload } from './context'
import { isNotNil } from './utils'
import { UserBase, UserType } from './user-base'

export class User {
export class User extends UserBase {
public readonly token: string
public readonly claims: JwtPayload

constructor(claims: JwtPayload, accessToken: string) {
super()
this.claims = claims
this.token = accessToken
}

get userType(): UserType {
return 'interactive'
}

get email(): string | undefined {
return (
this.claims.email ??
Expand Down
5 changes: 2 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { randomUUID } from 'crypto'
import type { Request } from 'express'
import { pick } from 'lodash'
import { User } from './User'
import { UserBase } from './user-base'

export interface GraphQLContext<
TLogger extends Logger = Logger,
TRequestInfo extends BaseRequestInfo = RequestInfo,
TUser = User | undefined,
TUser = UserBase | undefined,
> {
logger: TLogger
requestInfo: TRequestInfo
Expand All @@ -29,7 +30,6 @@ export interface BaseRequestInfo extends Record<string, unknown> {
correlationId?: string
arrLogId?: string
clientIp?: string
authHeader?: string
}

export interface LambdaContext {
Expand Down Expand Up @@ -108,7 +108,6 @@ export const createContextFactory = <TContext extends AnyGraphqlContext = GraphQ
arrLogId: req.headers['x-arr-log-id']?.toString() ?? undefined,
clientIp: req.headers['x-forwarded-for']?.toString() ?? req.socket.remoteAddress,
correlationId: req.headers['x-correlation-id']?.toString() ?? undefined,
authHeader: req.headers['Authorization']?.toString() ?? undefined,
}

// add lambda info from the context, if present
Expand Down
14 changes: 14 additions & 0 deletions src/service-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UserBase, UserType } from './user-base'

export class ServiceUser extends UserBase {
public readonly name: string

constructor(name: string) {
super()
this.name = name
}

get userType(): UserType {
return 'service'
}
}
5 changes: 5 additions & 0 deletions src/user-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type UserType = 'interactive' | 'service'

export abstract class UserBase {
abstract get userType(): UserType
}

0 comments on commit 946be6a

Please sign in to comment.