Skip to content
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

chore: Add example app using thirdparty recipe #65

Merged
merged 6 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 15 additions & 0 deletions examples/with-thirdparty/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions examples/with-thirdparty/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/with-thirdparty/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions examples/with-thirdparty/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/with-thirdparty/.idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions examples/with-thirdparty/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions examples/with-thirdparty/.idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions examples/with-thirdparty/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions examples/with-thirdparty/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## SuperTokens Example App

### Add dependencies

This example uses requires the following dependencies in addition to the default dependencies installed when creating an app:
- [supertokens-android](https://github.com/supertokens/supertokens-android)
- [play-services-auth](https://mvnrepository.com/artifact/com.google.android.gms/play-services-auth?repo=google)
- [retrofit](https://square.github.io/retrofit/)
- [AppAuth](https://github.com/openid/AppAuth-Android)

Add the following to your project level `settings.gradle.kts`. Note: This app uses the settings file for dependency resolution but the older method of using the project level `build.gradle` can also be used.

```gradle
dependencyResolutionManagement {
...
repositories {
...
maven { url = uri("https://jitpack.io") }
}
}
```

Add the folliwing to your app level `build.gradle`

```gradle
implementation("com.github.supertokens:supertokens-android:0.3.5")
implementation ("com.google.android.gms:play-services-auth:20.7.0")
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("net.openid:appauth:0.11.1")
```

### Setup

#### Google Login

1. Create OAuth credentials for Android on [Google cloud console](https://console.cloud.google.com/). You will need to add your keystore's SHA-1 fingerprint when creating the credential
2. Create OAuth credentials for Web on [Google cloud console](https://console.cloud.google.com/). This is required because we need to get the id token in the Android 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.
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
3. Replace all occurences of `GOOGLE_WEB_CLIENT_ID` with the client id for Web in both the Android code and the backend code
4. Replace all occurences of `GOOGLE_WEB_CLIENT_SECRET` with the client secret in the backend code

#### Github login
1. Create credentials for an OAuth app from [Github Developer Settings](https://github.com/settings/developers)
2. Use `com.supertokens.supertokensexample://oauthredirect` when configuring the Authorization callback URL.
3. Replace all occurences of `GITHUB_CLIENT_ID` in both the frontend and backend
4. Replace all occurences of `GITHUB_CLIENT_SECRET` in the backend code
5. If you are using `http://` or `https://` for the callback URL in your Github developer settings you also need to update the `AndroidManifest.xml` to update the scheme for `net.openid.appauth.RedirectUriReceiverActivity`

### Running the app

1. Replace the value of the API domain in `backend/config` and `app/src/main/java/com/supertokens/supertokenxexample/resources/Constants.kt` to match your machines local IP address
2. Navigate to the `/backend` folder and run `npm run start`
3. Run open the app in Android studio and run on a device or emulator

### How it works
- On app launch we check if a session exists and redirect to login if it doesnt
- We add the SuperTokens interceptor to the retrofit client 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
- In the case of Google we use the id token received after logging in to call the SuperTokens API
- For Github we use the code and state sent by GitHub to call the SuperTokens API
- On the home screen we call a protected API to fetch session information
1 change: 1 addition & 0 deletions examples/with-thirdparty/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
51 changes: 51 additions & 0 deletions examples/with-thirdparty/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "com.supertokens.supertokensexample"
compileSdk = 33

defaultConfig {
applicationId = "com.supertokens.supertokensexample"
minSdk = 29
targetSdk = 33
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {

implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.github.supertokens:supertokens-android:0.3.5")
implementation ("com.google.android.gms:play-services-auth:20.7.0")
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("net.openid:appauth:0.11.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
21 changes: 21 additions & 0 deletions examples/with-thirdparty/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.supertokens.supertokensexample

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.supertokens.supertokensexample", appContext.packageName)
}
}
47 changes: 47 additions & 0 deletions examples/with-thirdparty/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:name=".MyApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SuperTokensExample"
tools:targetApi="31"
android:usesCleartextTraffic="true">
<activity
android:name=".HomeActivity"
android:exported="false" />
<activity
android:name=".LoginActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="net.openid.appauth.RedirectUriReceiverActivity"
tools:node="replace"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="com.supertokens.supertokensexample"/>
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.supertokens.supertokensexample

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.widget.AppCompatButton
import com.supertokens.session.SuperTokens
import com.supertokens.supertokensexample.services.network.APIManager
import kotlin.concurrent.thread

class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)

val userId = findViewById<TextView>(R.id.tvUserId)
userId.text = SuperTokens.getUserId(this)

val sessionInfoTv = findViewById<TextView>(R.id.tvSessionInfo)

val callApiButton = findViewById<AppCompatButton>(R.id.btCallApi)
callApiButton.setOnClickListener {
thread {
val response = APIManager.getInstance().retrofitService.sessionInfo().execute()
val body = response.body()

if (body != null) {
runOnUiThread {
sessionInfoTv.text = body.string()
}
}
}
}

val signOutButton = findViewById<AppCompatButton>(R.id.btSignOut)
signOutButton.setOnClickListener {
thread {
SuperTokens.signOut(baseContext)

runOnUiThread {
val intent = Intent(baseContext, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
}
}
}
}
}
Loading
Loading