Skip to content

Added Cloud Functions snippet for sign in with Apple on Android #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion packages/sign_in_with_apple/sign_in_with_apple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ In your `android/app/src/main/AndroidManifest.xml` inside `<application>` add
</activity>
```

On the Sign in with Apple callback on your sever (specified in `WebAuthenticationOptions.redirectUri`), redirect safely back to your Android app using the following URL:
#### Server
On the "Sign in with Apple" callback on your server (specified in `WebAuthenticationOptions.redirectUri`), redirect safely back to your Android app using the following URL:

```
intent://callback?${PARAMETERS FROM CALLBACK BODY}#Intent;package=YOUR.PACKAGE.IDENTIFIER;scheme=signinwithapple;end
Expand All @@ -192,6 +193,52 @@ The `PARAMETERS FROM CALLBACK BODY` should be filled with the urlencoded body yo

Furthermore, when handling the incoming credentials on the client, make sure to only overwrite the current (guest) session of the user once your own server have validated the incoming `code` parameter, such that your app is not susceptible to malicious incoming links (e.g. logging out the current user).

##### Setting up Google Cloud Functions as server
A quick and easy way to set up the server side, is to deploy a Google Cloud Function. Please refer to the [Firebase Cloud Functions](https://firebase.google.com/docs/functions/) documentation to add functions to your project.

Once set up, you can create and deploy the following function:

```TypeScript
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import * as express from "express";
import * as bodyParser from "body-parser";
import {Request, Response} from "firebase-functions";

admin.initializeApp();

const pkg = "YOUR_PACKAGE_HERE";
const app = express();

app.use(bodyParser.urlencoded({extended: false}));

// Make all files in 'public' available
// https://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));

// The callback route used for Android, which will send
// the callback parameters from Apple into the Android app.
// This is done using a deeplink, which will cause
// the Chrome Custom Tab to be dismissed
// and providing the parameters from Apple back to the app.
app.post("/", (request: Request, response: Response) => {
const body = new URLSearchParams(request.body).toString();
const scheme = "signinwithapple";
const redirect =
`intent://callback?${body}#Intent;package=${pkg};scheme=${scheme};end`;
// console.log(`Redirecting to ${redirect}`);
response.redirect(307, redirect);
});

exports.sign_in_with_apple = functions.https.onRequest(app);
```

Make sure the `WebAuthenticationOptions.redirectUri` config points to the deployed function, eg:
```
https://us-central1-XXXXXXXXX.cloudfunctions.net/sign_in_with_apple
```


### iOS

At this point you should have added the Sign in with Apple capability to either your own app's capabilities or the test application you created to run the example.
Expand Down