forked from crowetic/commerce
* 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
36 lines
955 B
TypeScript
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
|