Skip to content

Commit 3769184

Browse files
authored
feat(auth): Optional lambda context in auth related functions (#420)
1 parent c2a21a6 commit 3769184

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

packages/api/src/auth/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const getAuthenticationContext = async ({
110110
}: {
111111
authDecoder?: Decoder | Decoder[]
112112
event: APIGatewayProxyEvent | Request
113-
context: LambdaContext
113+
context?: LambdaContext
114114
}): Promise<undefined | AuthContextPayload> => {
115115
const cookieHeader = parseAuthorizationCookie(event)
116116
const typeFromHeader = getAuthProviderHeader(event)

packages/graphql-server/src/functions/__tests__/fixtures/auth.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { parseJWT } from '@cedarjs/api'
1+
import { Decoded, parseJWT } from '@cedarjs/api'
22
import { AuthenticationError, ForbiddenError } from '@cedarjs/graphql-server'
3-
import type { APIGatewayEvent } from 'aws-lambda'
4-
5-
interface Context extends Record<string, any> {}
63

74
import { context } from '@cedarjs/context'
85

@@ -18,9 +15,9 @@ type RedwoodUser = Record<string, unknown> & { roles?: string[] }
1815
* if the user is authenticated or has role-based access
1916
*
2017
* @param decoded - The decoded access token containing user info and JWT claims like `sub`. Note could be null.
21-
* @param { token, SupportedAuthTypes type } - The access token itself as well as the auth provider type
22-
* @param { APIGatewayEvent event, Context context } - An object which contains information from the invoker
23-
* such as headers and cookies, and the context information about the invocation such as IP Address
18+
* @param authInfo
19+
* @param authInfo.token - The access token itself
20+
* @param authInfo.type - The auth provider type
2421
*
2522
* !! BEWARE !! Anything returned from this function will be available to the
2623
* client--it becomes the content of `currentUser` on the web side (as well as
@@ -33,11 +30,8 @@ type RedwoodUser = Record<string, unknown> & { roles?: string[] }
3330
* @returns RedwoodUser
3431
*/
3532
export const getCurrentUser = async (
36-
decoded,
37-
/* eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars */
38-
{ token, type },
39-
/* eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars */
40-
req?: { event: APIGatewayEvent, context: Context }
33+
decoded: Decoded,
34+
{ token: _token, type: _type }: { token: string; type: string },
4135
): Promise<RedwoodUser | null> => {
4236
if (!decoded) {
4337
return null

packages/graphql-server/src/functions/__tests__/useRequireAuth.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const mockedAuthenticationEvent = ({
6161

6262
const handler = async (
6363
_event: APIGatewayEvent,
64-
_context: Context,
64+
_context?: Context,
6565
): Promise<any> => {
6666
// @MARK
6767
// Don't use globalContext until beforeAll runs
@@ -142,8 +142,8 @@ const handlerWithError = async (
142142
}
143143

144144
const getCurrentUserWithError = async (
145-
_decoded,
146-
{ token: _token },
145+
_decoded: unknown,
146+
{ token: _token }: { token: string },
147147
): Promise<RedwoodUser> => {
148148
throw Error('Something went wrong getting the user info')
149149
}
@@ -163,7 +163,7 @@ describe('useRequireAuth', () => {
163163
authDecoder: async (
164164
token: string,
165165
_type: string,
166-
_req: { event: APIGatewayEvent; context: Context },
166+
_req: { event: APIGatewayEvent; context?: Context },
167167
) => {
168168
return { token }
169169
},
@@ -197,7 +197,7 @@ describe('useRequireAuth', () => {
197197
authDecoder: async (
198198
token: string,
199199
_type: string,
200-
_req: { event: APIGatewayEvent; context: Context },
200+
_req: { event: APIGatewayEvent; context?: Context },
201201
) => {
202202
return jwt.decode(token) as Record<string, any>
203203
},

packages/graphql-server/src/functions/useRequireAuth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface Args {
1212
authDecoder?: Decoder | Decoder[]
1313
handlerFn: (
1414
event: APIGatewayEvent,
15-
context: LambdaContext,
15+
context?: LambdaContext,
1616
...others: any
1717
) => any
1818
getCurrentUser?: GetCurrentUser
@@ -23,7 +23,7 @@ export type UseRequireAuth = (
2323
args: Args,
2424
) => (
2525
event: APIGatewayEvent,
26-
context: LambdaContext,
26+
context?: LambdaContext,
2727
...rest: any
2828
) => Promise<ReturnType<Args['handlerFn']>>
2929

@@ -34,7 +34,7 @@ export const useRequireAuth: UseRequireAuth = ({
3434
}) => {
3535
return async (
3636
event: APIGatewayEvent,
37-
context: LambdaContext,
37+
context?: LambdaContext,
3838
...rest: any
3939
) => {
4040
const authEnrichedFunction = async () => {

0 commit comments

Comments
 (0)