Skip to content

Commit 695de7f

Browse files
committed
feat: add form_post response mode support for Apple OAuth
- Add responseMode parameter to support different OAuth response types - Update callback endpoint to handle both GET and POST methods - Maintain backward compatibility with existing OAuth implementations
1 parent 6a06ebe commit 695de7f

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/callback-endpoint.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@ import { PluginTypes } from "./types";
1212
export const createCallbackEndpoint = (
1313
pluginOptions: PluginTypes,
1414
): Endpoint => ({
15-
method: "get",
16-
path: pluginOptions.callbackPath || "/oauth/callback",
15+
// Support both GET (default OAuth2) and POST (required for Apple OAuth with form_post)
16+
// - GET: Used by most OAuth providers (Google, GitHub, etc.)
17+
// - POST: Required by Apple when requesting name/email scopes with response_mode=form_post
18+
method: ['get', 'post'],
19+
path: pluginOptions.callbackPath || '/oauth/callback',
1720
handler: async (req) => {
1821
try {
19-
const { code } = req.query;
20-
if (typeof code !== "string")
21-
throw new Error(
22-
`Code not in query string: ${JSON.stringify(req.query)}`,
23-
);
22+
// Handle authorization code from both GET query params and POST body
23+
// This enables support for Apple's form_post response mode while maintaining
24+
// compatibility with traditional OAuth2 GET responses
25+
const code = req.method === 'POST' ? req.body?.code : req.query?.code
26+
// Improved error handling to clearly indicate whether we're missing the code
27+
// from POST body (Apple OAuth) or GET query parameters (standard OAuth)
28+
if (typeof code !== 'string')
29+
throw new Error(
30+
`Code not found in ${req.method === 'POST' ? 'body' : 'query'}: ${JSON.stringify(req.method === 'POST' ? req.body : req.query)}`,
31+
)
2432

2533
// /////////////////////////////////////
2634
// shorthands

src/types.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,32 @@ export interface PluginTypes {
2727
*/
2828
serverURL: string;
2929

30-
/**
30+
/**
3131
* Response mode for the OAuth provider.
32-
* Required for Apple OAuth when requesting name or email scope.
32+
* Specifies how the authorization response should be returned.
33+
*
34+
* Required for Apple OAuth when requesting name or email scopes.
35+
* Apple requires 'form_post' when requesting these scopes to ensure
36+
* secure transmission of user data.
37+
*
3338
* Common values:
3439
* - 'form_post': Response parameters encoded in POST body (required for Apple with name/email scope)
35-
* - 'query': Response parameters encoded in URL query string
40+
* - 'query': Response parameters encoded in URL query string (default for most providers)
41+
* - 'fragment': Response parameters encoded in URL fragment
42+
*
43+
* Example for Apple OAuth:
44+
* ```typescript
45+
* {
46+
* responseMode: 'form_post',
47+
* scopes: ['name', 'email']
48+
* }
49+
* ```
50+
*
3651
* @default undefined
3752
*/
3853
responseMode?: string
3954

55+
4056
/**
4157
* Slug of the collection where user information will be stored
4258
* @default "users"

0 commit comments

Comments
 (0)