|
| 1 | +import { PayloadRequest } from "payload"; |
| 2 | +import { OAuth2Plugin } from "../src/index"; |
| 3 | + |
| 4 | +//////////////////////////////////////////////////////////////////////////////// |
| 5 | +// Apple OAuth |
| 6 | +//////////////////////////////////////////////////////////////////////////////// |
| 7 | +export const appleOAuth = OAuth2Plugin({ |
| 8 | + enabled: |
| 9 | + typeof process.env.APPLE_CLIENT_ID === "string" && |
| 10 | + typeof process.env.APPLE_CLIENT_SECRET === "string", |
| 11 | + strategyName: "apple", |
| 12 | + useEmailAsIdentity: true, |
| 13 | + serverURL: process.env.NEXT_PUBLIC_URL || "http://localhost:3000", |
| 14 | + clientId: process.env.APPLE_CLIENT_ID || "", |
| 15 | + clientSecret: process.env.APPLE_CLIENT_SECRET || "", |
| 16 | + authorizePath: "/oauth/apple", |
| 17 | + callbackPath: "/oauth/apple/callback", |
| 18 | + authCollection: "users", |
| 19 | + tokenEndpoint: "https://appleid.apple.com/auth/token", |
| 20 | + scopes: ["name", "email"], |
| 21 | + providerAuthorizationUrl: "https://appleid.apple.com/auth/authorize", |
| 22 | + // Required for Apple OAuth when requesting name or email scopes |
| 23 | + responseMode: "form_post", |
| 24 | + getUserInfo: async (accessToken: string, req: PayloadRequest) => { |
| 25 | + try { |
| 26 | + // For Apple, the ID token is a JWT that contains user info |
| 27 | + const tokenParts = accessToken.split("."); |
| 28 | + if (tokenParts.length !== 3) { |
| 29 | + throw new Error("Invalid ID token format"); |
| 30 | + } |
| 31 | + |
| 32 | + // Decode the base64 payload |
| 33 | + const payload = JSON.parse(Buffer.from(tokenParts[1], "base64").toString()); |
| 34 | + |
| 35 | + if (!payload.email) { |
| 36 | + throw new Error("No email found in payload"); |
| 37 | + } |
| 38 | + |
| 39 | + return { |
| 40 | + email: payload.email, |
| 41 | + sub: payload.sub, |
| 42 | + // Apple provides name only on first login |
| 43 | + firstName: payload.given_name || "", |
| 44 | + lastName: payload.family_name || "", |
| 45 | + }; |
| 46 | + } catch (error) { |
| 47 | + req.payload.logger.error("Error parsing Apple token:", error); |
| 48 | + throw error; |
| 49 | + } |
| 50 | + }, |
| 51 | + getToken: async (code: string, req: PayloadRequest) => { |
| 52 | + try { |
| 53 | + const redirectUri = `${process.env.NEXT_PUBLIC_URL || "http://localhost:3000"}/api/users/oauth/apple/callback`; |
| 54 | + |
| 55 | + // Make the token exchange request |
| 56 | + const params = new URLSearchParams({ |
| 57 | + client_id: process.env.APPLE_CLIENT_ID || "", |
| 58 | + client_secret: process.env.APPLE_CLIENT_SECRET || "", |
| 59 | + code: code, |
| 60 | + grant_type: "authorization_code", |
| 61 | + redirect_uri: redirectUri, |
| 62 | + }); |
| 63 | + |
| 64 | + const response = await fetch("https://appleid.apple.com/auth/token", { |
| 65 | + method: "POST", |
| 66 | + headers: { |
| 67 | + "Content-Type": "application/x-www-form-urlencoded", |
| 68 | + }, |
| 69 | + body: params.toString(), |
| 70 | + }); |
| 71 | + |
| 72 | + if (!response.ok) { |
| 73 | + const error = await response.text(); |
| 74 | + throw new Error(`Token exchange failed: ${error}`); |
| 75 | + } |
| 76 | + |
| 77 | + const tokenResponse = await response.json(); |
| 78 | + |
| 79 | + // Return the id_token which contains the user info |
| 80 | + return tokenResponse.id_token; |
| 81 | + } catch (error) { |
| 82 | + req.payload.logger.error("Error in getToken:", error); |
| 83 | + throw error; |
| 84 | + } |
| 85 | + }, |
| 86 | + successRedirect: (req) => { |
| 87 | + // Check user roles to determine redirect |
| 88 | + const user = req.user; |
| 89 | + if (user && Array.isArray(user.roles)) { |
| 90 | + if (user.roles.includes("admin")) { |
| 91 | + return "/admin"; |
| 92 | + } |
| 93 | + } |
| 94 | + return "/"; // Default redirect for customers |
| 95 | + }, |
| 96 | + failureRedirect: (req, err) => { |
| 97 | + req.payload.logger.error(err); |
| 98 | + return "/login?error=apple-auth-failed"; |
| 99 | + }, |
| 100 | +}); |
0 commit comments