Skip to content

Commit

Permalink
dbAuth Vercel Fix (#3309)
Browse files Browse the repository at this point in the history
Co-authored-by: Rob Cameron <rob.cameron@fastmail.com>
  • Loading branch information
2 people authored and dac09 committed Sep 1, 2021
1 parent 4a6ed30 commit 3008210
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
16 changes: 10 additions & 6 deletions packages/api/src/functions/dbAuth/DbAuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,16 @@ export class DbAuthHandler {

// parses the event body into JSON, whether it's base64 encoded or not
_parseBody() {
if (this.event.isBase64Encoded) {
return JSON.parse(
Buffer.from(this.event.body || '', 'base64').toString('utf-8')
)
if (this.event.body) {
if (this.event.isBase64Encoded) {
return JSON.parse(
Buffer.from(this.event.body || '', 'base64').toString('utf-8')
)
} else {
return JSON.parse(this.event.body)
}
} else {
return this.event.body && JSON.parse(this.event.body)
return {}
}
}

Expand Down Expand Up @@ -483,7 +487,7 @@ export class DbAuthHandler {
_ok(body: string, headers = {}, options = { statusCode: 200 }) {
return {
statusCode: options.statusCode,
body,
body: typeof body === 'string' ? body : JSON.stringify(body),
headers: { 'Content-Type': 'application/json', ...headers },
}
}
Expand Down
64 changes: 62 additions & 2 deletions packages/api/src/functions/dbAuth/__tests__/DbAuthHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('dbAuth', () => {
expect(dbAuth.options).toEqual(options)
})

it('parses a plain text body', () => {
it('parses params from a plain text body', () => {
event = { headers: {}, body: `{"foo":"bar", "baz":123}` }
context = { foo: 'bar' }
options = { db: db }
Expand All @@ -183,7 +183,28 @@ describe('dbAuth', () => {
expect(dbAuth.params).toEqual({ foo: 'bar', baz: 123 })
})

it('parses a base64 encoded body', () => {
it('parses an empty plain text body and still sets params', () => {
event = { isBase64Encoded: false, headers: {}, body: '' }
context = { foo: 'bar' }
options = { db: db }
const dbAuth = new DbAuthHandler(event, context, options)

expect(dbAuth.params).toEqual({})
})

it('parses params from an undefined body when isBase64Encoded == false', () => {
event = {
isBase64Encoded: false,
headers: {},
}
context = { foo: 'bar' }
options = { db: db }
const dbAuth = new DbAuthHandler(event, context, options)

expect(dbAuth.params).toEqual({})
})

it('parses params from a base64 encoded body', () => {
event = {
isBase64Encoded: true,
headers: {},
Expand All @@ -196,6 +217,31 @@ describe('dbAuth', () => {
expect(dbAuth.params).toEqual({ foo: 'bar', baz: 123 })
})

it('parses params from an undefined body when isBase64Encoded == true', () => {
event = {
isBase64Encoded: true,
headers: {},
}
context = { foo: 'bar' }
options = { db: db }
const dbAuth = new DbAuthHandler(event, context, options)

expect(dbAuth.params).toEqual({})
})

it('parses params from an empty body when isBase64Encoded == true', () => {
event = {
isBase64Encoded: true,
headers: {},
body: '',
}
context = { foo: 'bar' }
options = { db: db }
const dbAuth = new DbAuthHandler(event, context, options)

expect(dbAuth.params).toEqual({})
})

it('sets header-based CSRF token', () => {
event = { headers: { 'csrf-token': 'qwerty' } }
const dbAuth = new DbAuthHandler(event, context, options)
Expand Down Expand Up @@ -907,6 +953,20 @@ describe('dbAuth', () => {

expect(response.statusCode).toEqual(201)
})

it('stringifies a JSON body', () => {
const dbAuth = new DbAuthHandler(event, context, options)
const response = dbAuth._ok({ foo: 'bar' }, {}, { statusCode: 201 })

expect(response.body).toEqual('{"foo":"bar"}')
})

it('does not stringify a body that is a string already', () => {
const dbAuth = new DbAuthHandler(event, context, options)
const response = dbAuth._ok('{"foo":"bar"}', {}, { statusCode: 201 })

expect(response.body).toEqual('{"foo":"bar"}')
})
})

describe('_notFound', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/auth/src/authClients/dbAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export const dbAuth = (): AuthClient => {
`${global.__REDWOOD__API_PROXY_PATH}/auth?method=getToken`
)
const token = await response.text()
return token

if (token.length === 0) {
return null
} else {
return token
}
}

const login = async (attributes: LoginAttributes) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ export const handler = async (event, context) => {
loginExpires: 60 * 60 * 24 * 365 * 10,
})

return authHandler.invoke()
return await authHandler.invoke()
}

0 comments on commit 3008210

Please sign in to comment.