The AppleClientSecret class is used to generate the JWT client secret required for Apple authentication. It handles reading the private key from either a file or a raw string and signs a JWT using the ES256 algorithm.
Constructor Parameters
new AppleClientSecret(config, privateKeyLocation, privateKeyMethod)
config (object): An object containing the following keys:client_id (string): Your Apple Service ID.team_id (string): Your Apple Team ID.redirect_uri (string): Your authorized redirect URI.key_id (string): The Key ID of your private key.
privateKeyLocation (string): Either the file path to your .p8 private key file or the raw text of the private key itself.privateKeyMethod (string, optional): Defines how to treat privateKeyLocation.'file' (default): Treats the location as a file path to be read.'text': Treats the location as the raw private key string.
Usage
Call the .generate() method to receive a Promise that resolves to the generated JWT token string. The token is configured to expire in 6 months.
const AppleClientSecret = require('apple-auth/src/token'); // Adjust path as necessary
const config = {
client_id: 'com.example.service',
team_id: 'TEAMID123',
redirect_uri: 'https://example.com/callback',
key_id: 'KEYID456'
};
// Option 1: Loading from a file
const generator = new AppleClientSecret(config, './path/to/AuthKey_ABC123.p8', 'file');
// Option 2: Loading from a raw string
// const generator = new AppleClientSecret(config, '-----BEGIN PRIVATE KEY-----\n...', 'text');
generator.generate()
.then(token => console.log('Generated Token:', token))
.catch(err => console.error('Error:', err));