From aa9ee11433f3d946d553ce4f9711a031bf7c00bb Mon Sep 17 00:00:00 2001 From: Nemi Shah Date: Wed, 15 Nov 2023 11:59:36 +0530 Subject: [PATCH] Update README --- examples/with-thirdparty/README.md | 56 ++++++++++++++++++- examples/with-thirdparty/backend/config.ts | 24 ++++++-- .../with-thirdparty/Info.plist | 6 +- .../LoginScreenViewController.swift | 3 +- 4 files changed, 78 insertions(+), 11 deletions(-) diff --git a/examples/with-thirdparty/README.md b/examples/with-thirdparty/README.md index cd16241..e95b5ae 100644 --- a/examples/with-thirdparty/README.md +++ b/examples/with-thirdparty/README.md @@ -4,4 +4,58 @@ - Call sign in - Get id token - Call ST API -- URL Protocol for github login \ No newline at end of file +- URL Protocol for github login + +# SuperTokens Example App + +## Add dependencies + +This example uses requires the following dependencies: + +- [AppAuth](https://github.com/openid/AppAuth-iOS) +- [GoogleSignIn](https://developers.google.com/identity/sign-in/ios/start-integrating) +- [SuperTokensIOS](https://github.com/supertokens/supertokens-ios) + +This example app uses Swift Package Manager but you can use Cocoapods instead. + +## Setup + +### Google + +- Create OAuth credentials for iOS on [Google cloud console](https://console.cloud.google.com/) +- Create OAuth credentials for Web on [Google cloud console](https://console.cloud.google.com/). This is required because we need to get the authorization code in the app to be able to use SuperTokens. You need to provide all values (including domains and URLs) for Google login to work, you can use dummy values if you do not have a web application. +- Replace all occurences of `GOOGLE_IOS_CLIENT_ID` with the client id for iOS in the app's code (including the info.plist) +- Replace `GOOGLE_IOS_URL_SCHEME` with the value of `GOOGLE_IOS_CLIENT_ID` in reverse, for example if the iOS client id is `com.org.scheme` the value you want to set is `scheme.org.com`. Google cloud console will provide a way to copy the URL scheme to make this easier. +- Replace all occurences of `GOOGLE_WEB_CLIENT_ID` with the client id for Web in both the iOS code (including the info.plist) and the backend code +- Replace all occurences of `GOOGLE_WEB_CLIENT_SECRET` with the client secret in the backend code + +### Github login + +- Create credentials for an OAuth app from Github Developer Settings +- Use com.supertokens.supertokensexample://oauthredirect when configuring the Authorization callback URL. If you are using your own redirect url be sure to update the `onGithubClicked` function in `LoginScreenViewController.swift` +- Replace all occurences of `GITHUB_CLIENT_ID` in both the frontend and backend +- Replace all occurences of `GITHUB_CLIENT_SECRET` in the backend code + +GitHub requires that we pass an additional `Accept: application/json` header when calling the token endpoint but the AppAuth library does not allow us to pass custom headers. In this example app we get around this by registering a custom `URLProtocol` that adds this header for all requests made to the token endpoint. To see how this is done refer to `GithubLoginProtocol.swift`. + +### Apple login + +- Add the Sign in with Apple capability for your app's primary target. This is already done for this example app so no steps are needed. +- If you are not using Xcode's automatic signing you will need to manually add the capability against your bundle id in Apple's dashboard. +- Replace all occurrences of `APPLE_CLIENT_ID`. This should match your bundle id +- Replace all occurrences of `APPLE_KEY_ID`. You will need to create a new key with the Sign in with Apple capability on Apple's dashboard. +- Replace all occurences of `APPLE_PRIVATE_KEY`, when you create a key there will be an option to download the private key. You can only download this once. +- Replace all occurrences of `APPLE_TEAM_ID` with your Apple developer account's team id + +## Running the app + +- Replace the value of the API domain in `Constants.swift` and `/backend/config.ts` to match your machines local IP address +- Navigate to the `/backend` folder and run `npm run start` +- Open the app in Xcode and run it on an emulator or simulator + +## How it works + +- On app launch we check if a session exists and redirect to login if it doesnt +- We register the `SuperTokensURLProtocol` so that the SuperTokens SDK can manage session tokens for us +- After logging in we call APIs exposed by the SuperTokens backend SDKs to create a session and redirect to the home screen +- On the home screen we call a protected API to fetch session information \ No newline at end of file diff --git a/examples/with-thirdparty/backend/config.ts b/examples/with-thirdparty/backend/config.ts index 0855b52..deccf34 100644 --- a/examples/with-thirdparty/backend/config.ts +++ b/examples/with-thirdparty/backend/config.ts @@ -10,7 +10,7 @@ export const SuperTokensConfig: TypeInput = { }, appInfo: { appName: "SuperTokens Demo App", - apiDomain: "http://192.168.29.87:3001", + apiDomain: "http://192.168.1.102:3001", websiteDomain: "http://localhost:3000", // this value does not matter for the android app }, // recipeList contains all the modules that you want to @@ -27,8 +27,8 @@ export const SuperTokensConfig: TypeInput = { clients: [ { clientId: - "580674050145-shkfcshav895dsoj61vuf6s5iml27glr.apps.googleusercontent.com", - clientSecret: "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", + "GOOGLE_WEB_CLIENT_ID", + clientSecret: "GOOGLE_WEB_CLIENT_SECRET", }, ], }, @@ -38,12 +38,26 @@ export const SuperTokensConfig: TypeInput = { thirdPartyId: "github", clients: [ { - clientId: "eee1670bbc37d98c1d30", - clientSecret: "9b0c5134a89ba98a813adb72e56d9765dd36c966", + clientId: "GITHUB_CLIENT_ID", + clientSecret: "GITHUB_CLIENT_SECRET", }, ], }, }, + { + config: { + thirdPartyId: "apple", + clients: [{ + clientId: "APPLE_CLIENT_ID", + additionalConfig: { + keyId: "APPLE_KEY_ID", + privateKey: + "APPLE_PRIVATE_KEY", + teamId: "APPLE_TEAM_ID", + } + }] + }, + } ], }, }), diff --git a/examples/with-thirdparty/with-thirdparty/Info.plist b/examples/with-thirdparty/with-thirdparty/Info.plist index 993a69a..275b620 100644 --- a/examples/with-thirdparty/with-thirdparty/Info.plist +++ b/examples/with-thirdparty/with-thirdparty/Info.plist @@ -3,18 +3,18 @@ GIDServerClientID - 580674050145-shkfcshav895dsoj61vuf6s5iml27glr.apps.googleusercontent.com + GOOGLE_WEB_CLIENT_ID CFBundleURLTypes CFBundleURLSchemes - com.googleusercontent.apps.580674050145-6gf19vu5ao64kcofa2mj60j9ip7cc8c2 + GOOGLE_IOS_URL_SCHEME GIDClientID - 580674050145-6gf19vu5ao64kcofa2mj60j9ip7cc8c2.apps.googleusercontent.com + GOOGLE_IOS_CLIENT_ID UIApplicationSceneManifest UIApplicationSupportsMultipleScenes diff --git a/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginScreenViewController.swift b/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginScreenViewController.swift index 81608ce..8b5acad 100644 --- a/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginScreenViewController.swift +++ b/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginScreenViewController.swift @@ -15,7 +15,6 @@ class LoginScreenViewController: UIViewController { } @IBAction func onGoogleCliked() { - print("Google") GIDSignIn.sharedInstance.signIn(withPresenting: self) { signInResult, error in guard error == nil else { return } @@ -67,7 +66,7 @@ class LoginScreenViewController: UIViewController { let configuration = OIDServiceConfiguration(authorizationEndpoint: authorizationEndpoint, tokenEndpoint: tokenEndpoint) let request = OIDAuthorizationRequest.init(configuration: configuration, - clientId: "eee1670bbc37d98c1d30", + clientId: "GITHUB_CLIENT_ID", scopes: ["user"], redirectURL: URL(string: "com.supertokens.supertokensexample://oauthredirect")!, responseType: OIDResponseTypeCode,