Attempting to log in with Apple using NextAuth. Authentication is successful, but it redirects to /?error=OAuthCallback
.
The URL being used is:
https://appleid.apple.com/auth/authorize?client_id=com.wheeleasy.org&scope=name%20email&response_type=code&redirect_uri=https%3A%2F%2Fdevelop-sr3snxi-sq2r64cdk53qe.au.platformsh.site%2Fapi%2Fauth%2Fcallback%2Fapple&response_mode=form_post&code_challenge=lelepvmyqyryTiyWL6NthxspYae6t9XYB1PAj3e3c_E&code_challenge_method=S256
enter image description here
This is the code snippet for generating client secret, which was generated successfully.
const { SignJWT } = require("jose");
const fs = require("fs");
const path = require("path");
const { createPrivateKey } = require("crypto");
// Constants for Apple API credentials and key generation
const teamId = process.env.TEAM_ID;
const clientId = process.env.CLIENT_ID;
const keyId = process.env.KEY_ID;
const expiresIn = 86400 * 180;
const privateKeyPath = path.join(__dirname, "AuthKey_CW5NRK3ZSN.p8");
const exp = Math.ceil(Date.now() / 1000) + expiresIn;
const expiresAt = Math.ceil(Date.now() / 1000) + expiresIn;
const expirationTime = exp ?? expiresAt;
let privateKey;
try {
privateKey = fs.readFileSync(privateKeyPath, "utf8");
console.log("Private Key Read Successfully");
} catch (err) {
console.error("Error reading private key:", err);
process.exit(1);
}
(async () => {
try {
const jwt = await new SignJWT({})
.setAudience("https://appleid.apple.com")
.setIssuer(teamId)
.setIssuedAt()
.setExpirationTime(expirationTime)
.setSubject(clientId)
.setProtectedHeader({ alg: "ES256", kid: keyId, typ: "JWT" })
.sign(createPrivateKey(privateKey));
console.log("Client Secret:", jwt);
} catch (err) {
console.error("Error generating JWT:", err);
}
})();
import NextAuth from "next-auth/next";
import GoogleProvider from "next-auth/providers/google";
import FacebookProvider from "next-auth/providers/facebook";
import AppleProvider from "next-auth/providers/apple";
export default NextAuth({
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
}),
AppleProvider({
clientId: process.env.APPLE_CLIENT_ID,
clientSecret: process.env.APPLE_CLIENT_SECRET,
}),
],
callbacks: {
session: async ({ session, token }) => {
if (session?.user) {
session.user.id = token.uid;
}
return session;
},
jwt: async ({ user, token }) => {
if (user) {
token.uid = user.id;
}
return token;
},
},
session: {
strategy: "jwt",
},
});
I have all the necessary information such as team ID, key ID, and AuthKey file to generate a client secret. While trying to login with Apple, I am being redirected to an error page (/?error=OAuthCallback). I aim to successfully integrate Apple login functionality.