Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
nkshah2 committed Nov 15, 2023
1 parent d461f09 commit aa9ee11
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
56 changes: 55 additions & 1 deletion examples/with-thirdparty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,58 @@
- Call sign in
- Get id token
- Call ST API
- URL Protocol for github login
- 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
24 changes: 19 additions & 5 deletions examples/with-thirdparty/backend/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
},
],
},
Expand All @@ -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",
}
}]
},
}
],
},
}),
Expand Down
6 changes: 3 additions & 3 deletions examples/with-thirdparty/with-thirdparty/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
<plist version="1.0">
<dict>
<key>GIDServerClientID</key>
<string>580674050145-shkfcshav895dsoj61vuf6s5iml27glr.apps.googleusercontent.com</string>
<string>GOOGLE_WEB_CLIENT_ID</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.580674050145-6gf19vu5ao64kcofa2mj60j9ip7cc8c2</string>
<string>GOOGLE_IOS_URL_SCHEME</string>
</array>
</dict>
</array>
<key>GIDClientID</key>
<string>580674050145-6gf19vu5ao64kcofa2mj60j9ip7cc8c2.apps.googleusercontent.com</string>
<string>GOOGLE_IOS_CLIENT_ID</string>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit aa9ee11

Please sign in to comment.