mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Add missing logout files
This commit is contained in:
parent
1d2451550b
commit
f6ec88a817
37
framework/commerce/api/endpoints/logout.ts
Normal file
37
framework/commerce/api/endpoints/logout.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import type { LogoutSchema } from '../../types/logout'
|
||||
import { CommerceAPIError } from '../utils/errors'
|
||||
import isAllowedOperation from '../utils/is-allowed-operation'
|
||||
import type { GetAPISchema } from '..'
|
||||
|
||||
const logoutEndpoint: GetAPISchema<
|
||||
any,
|
||||
LogoutSchema
|
||||
>['endpoint']['handler'] = async (ctx) => {
|
||||
const { req, res, operations } = ctx
|
||||
|
||||
if (
|
||||
!isAllowedOperation(req, res, {
|
||||
GET: operations['logout'],
|
||||
})
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const redirectTo = req.query.redirect_to
|
||||
const body = typeof redirectTo === 'string' ? { redirectTo } : {}
|
||||
|
||||
return await operations['logout']({ ...ctx, body })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
|
||||
const message =
|
||||
error instanceof CommerceAPIError
|
||||
? 'An unexpected error ocurred with the Commerce API'
|
||||
: 'An unexpected error ocurred'
|
||||
|
||||
res.status(500).json({ data: null, errors: [{ message }] })
|
||||
}
|
||||
}
|
||||
|
||||
export default logoutEndpoint
|
8
pages/api/logout.ts
Normal file
8
pages/api/logout.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import logout from '@commerce/api/endpoints/logout'
|
||||
import { LogoutAPI, operations } from '@framework/api/logout'
|
||||
import commerce from '@lib/api/commerce'
|
||||
|
||||
export default commerce.endpoint({
|
||||
handler: logout as LogoutAPI['endpoint']['handler'],
|
||||
operations,
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user