/
index.ts
44 lines (40 loc) · 1.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { GlobalContext } from 'src/globalContext'
import type { APIGatewayProxyEvent, Context as LambdaContext } from 'aws-lambda'
import type { SupportedAuthTypes } from '@redwoodjs/auth'
import { netlify } from './netlify'
import { auth0 } from './auth0'
const noop = (token: string) => token
const typesToDecoders: Record<SupportedAuthTypes, Function> = {
auth0: auth0,
netlify: netlify,
goTrue: netlify,
magicLink: noop,
firebase: noop,
supabase: noop,
custom: noop,
}
export const decodeToken = async (
type: SupportedAuthTypes,
token: string,
req: {
event: APIGatewayProxyEvent
context: GlobalContext & LambdaContext
}
): Promise<null | string | Record<string, unknown>> => {
if (!typesToDecoders[type]) {
// Make this a warning, instead of a hard error
// Allow users to have multiple custom types if they choose to
if (process.env.NODE_ENV === 'development') {
console.warn(
`The auth type "${type}" is not officially supported, we currently support: ${Object.keys(
typesToDecoders
).join(', ')}`
)
console.warn(
'Please ensure you have handlers for your custom auth in getCurrentUser in src/lib/auth.{js,ts}'
)
}
}
const decoder = typesToDecoders[type] || noop
return decoder(token, req)
}