Facebook
From mh, 3 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 101
  1. app.post("/sign_in_with_apple", async (request, response) => {
  2.   const auth = new AppleAuth(
  3.     {
  4.       // use the bundle ID as client ID for native apps, else use the service ID for web-auth flows
  5.       // https://forums.developer.apple.com/thread/118135
  6.       client_id:
  7.         request.query.useBundleId === "true"
  8.           ? process.env.BUNDLE_ID
  9.           : process.env.SERVICE_ID,
  10.       team_id: process.env.TEAM_ID,
  11.       redirect_uri:
  12.         "https://flutter-sign-in-with-apple-example.glitch.me/callbacks/sign_in_with_apple", // does not matter here, as this is already the callback that verifies the token after the redirection
  13.       key_id: process.env.KEY_ID
  14.     },
  15.     process.env.KEY_CONTENTS.replace(/\|/g, "\n"),
  16.     "text"
  17.   );
  18.  
  19.   console.log(request.query);
  20.  
  21.   const accessToken = await auth.accessToken(request.query.code);
  22.  
  23.   const idToken = jwt.decode(accessToken.id_token);
  24.  
  25.   const userID = idToken.sub;
  26.  
  27.   console.log(idToken);
  28.  
  29.   // `userEmail` and `userName` will only be provided for the initial authorization with your app
  30.   const userEmail = idToken.email;
  31.   const userName = `${request.query.firstName} ${request.query.lastName}`;
  32.  
  33.   // ??‍♀️ TODO: Use the values provided create a new session for the user in your system
  34.   const sessionID = `NEW SESSION ID for ${userID} / ${userEmail} / ${userName}`;
  35.  
  36.   console.log(`sessionID = ${sessionID}`);
  37.  
  38.   response.json({ sessionId: sessionID });
  39. });