4
0
forked from crowetic/commerce
Catalin Pinte c75b0fc001
Dynamic API routes (#836)
* Add dynamic API endpoints

* Add missing dependency

* Update api handlers

* Updates

* Fix build errors

* Update package.json

* Add checkout endpoint parser & update errors

* Update tsconfig.json

* Update cart.ts

* Update parser

* Update errors.ts

* Update errors.ts

* Move to Edge runtime

* Revert to local

* Fix switchable runtimes

* Make nodejs default runtime

* Update pnpm-lock.yaml

* Update handlers

* Fix build errors

* Change headers
2022-10-30 13:41:21 -05:00

36 lines
955 B
TypeScript

import type { LoginEndpoint } from '.'
import { FetcherError } from '@vercel/commerce/utils/errors'
import { CommerceAPIError } from '@vercel/commerce/api/utils/errors'
const invalidCredentials = /invalid credentials/i
const login: LoginEndpoint['handlers']['login'] = async ({
body: { email, password },
config,
commerce,
}) => {
try {
const res = new Response()
await commerce.login({ variables: { email, password }, config, res })
return {
status: res.status,
headers: res.headers,
}
} catch (error) {
// Check if the email and password didn't match an existing account
if (error instanceof FetcherError) {
throw new CommerceAPIError(
invalidCredentials.test(error.message)
? 'Cannot find an account that matches the provided credentials'
: error.message,
{ status: error.status || 401 }
)
} else {
throw error
}
}
}
export default login