Skip to content

Commit 45fc5d5

Browse files
committed
feat: initial commit
1 parent 9cd0349 commit 45fc5d5

File tree

7 files changed

+107
-12
lines changed

7 files changed

+107
-12
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
# react-native-google-credentials-manager
1+
# [WIP] react-native-google-credentials-manager
22

3-
API to communicate with Google credentials manager
3+
Work in Progress module for React Native to communicate with Google credentials manager
44

5-
## Installation
5+
## Screenshots
6+
7+
![alt text](credman-bottomsheet-animated.gif)
8+
9+
<!-- ## Installation
610
711
```sh
812
npm install react-native-google-credentials-manager
9-
```
13+
``` -->
1014

1115
## Usage
1216

13-
1417
```js
15-
import { multiply } from 'react-native-google-credentials-manager';
18+
import { signIn } from 'react-native-google-credentials-manager';
1619

1720
// ...
1821

19-
const result = multiply(3, 7);
22+
const id = signIn(<server-client-id>);
2023
```
2124

22-
2325
## Contributing
2426

2527
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

android/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def kotlin_version = getExtOrDefault("kotlinVersion")
7474
dependencies {
7575
implementation "com.facebook.react:react-android"
7676
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
77+
implementation "androidx.credentials:credentials:1.5.0"
78+
implementation "androidx.credentials:credentials-play-services-auth:1.5.0"
79+
implementation "com.google.android.libraries.identity.googleid:googleid:1.1.1"
7780
}
7881

7982
react {

android/src/main/java/com/googlecredentialsmanager/GoogleCredentialsManagerModule.kt

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package com.googlecredentialsmanager
22

3+
import android.content.Context
4+
import androidx.credentials.CredentialManager
5+
import androidx.credentials.GetCredentialRequest
6+
import androidx.credentials.GetCredentialResponse
7+
import androidx.credentials.exceptions.GetCredentialException
8+
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
9+
10+
import kotlinx.coroutines.CoroutineScope
11+
import kotlinx.coroutines.Dispatchers
12+
import kotlinx.coroutines.launch
13+
14+
import com.facebook.react.bridge.Promise
315
import com.facebook.react.bridge.ReactApplicationContext
416
import com.facebook.react.module.annotations.ReactModule
517

618
@ReactModule(name = GoogleCredentialsManagerModule.NAME)
7-
class GoogleCredentialsManagerModule(reactContext: ReactApplicationContext) :
19+
class GoogleCredentialsManagerModule(private val reactContext: ReactApplicationContext) :
820
NativeGoogleCredentialsManagerSpec(reactContext) {
921

1022
override fun getName(): String {
@@ -17,6 +29,54 @@ class GoogleCredentialsManagerModule(reactContext: ReactApplicationContext) :
1729
return a * b
1830
}
1931

32+
override fun getAndroidVersion(): String {
33+
return android.os.Build.VERSION.RELEASE ?: "unknown"
34+
}
35+
36+
override fun signIn(serverClientId: String, promise: Promise) {
37+
val activityContext = reactContext.currentActivity
38+
39+
if (activityContext == null) {
40+
promise.reject("NO_ACTIVITY", "Current activity is null")
41+
return
42+
}
43+
44+
val credentialManager = CredentialManager.create(activityContext)
45+
46+
val googleIdOption = GetGoogleIdOption.Builder()
47+
.setServerClientId(serverClientId) // ✅ User-passed value
48+
.setFilterByAuthorizedAccounts(false) // 👈 Show all accounts
49+
.setAutoSelectEnabled(false) // 👈 Force account picker UI
50+
.build()
51+
52+
val request = GetCredentialRequest.Builder()
53+
.addCredentialOption(googleIdOption)
54+
.build()
55+
56+
CoroutineScope(Dispatchers.Main).launch {
57+
try {
58+
val response: GetCredentialResponse = credentialManager.getCredential(
59+
context = activityContext,
60+
request = request
61+
)
62+
63+
val credential = response.credential
64+
val data = credential.data
65+
66+
val idToken = data.getString("googleIdToken") ?: ""
67+
val displayName = data.getString("displayName") ?: ""
68+
val profilePicture = data.getString("profilePictureUri") ?: ""
69+
70+
promise.resolve(idToken) // You can return displayName, etc. as a map too
71+
72+
} catch (e: GetCredentialException) {
73+
promise.reject("CREDENTIAL_ERROR", e.localizedMessage)
74+
} catch (e: Exception) {
75+
promise.reject("UNEXPECTED_ERROR", e.localizedMessage)
76+
}
77+
}
78+
}
79+
2080
companion object {
2181
const val NAME = "GoogleCredentialsManager"
2282
}

credman-bottomsheet-animated.gif

4.45 MB
Loading

example/src/App.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1-
import { Text, View, StyleSheet } from 'react-native';
2-
import { multiply } from 'react-native-google-credentials-manager';
1+
import { Text, View, StyleSheet, Button, Alert } from 'react-native';
2+
import {
3+
multiply,
4+
getAndroidVersion,
5+
signIn,
6+
} from 'react-native-google-credentials-manager';
37

48
const result = multiply(3, 7);
59

610
export default function App() {
11+
const handleGetAndroidVersion = () => {
12+
const version = getAndroidVersion();
13+
Alert.alert('Android Version', version);
14+
};
715
return (
816
<View style={styles.container}>
9-
<Text>Result: {result}</Text>
17+
<Text style={{ color: 'white' }}>Result: {result}</Text>
18+
<Button onPress={handleGetAndroidVersion} title="Android version" />
19+
<Button
20+
onPress={() => {
21+
signIn('serverClientId')
22+
.then((token) => Alert.alert('ID Token', token))
23+
.catch((error) => Alert.alert('Error', error.message));
24+
}}
25+
title="Sign In with Google"
26+
/>
1027
</View>
1128
);
1229
}

src/NativeGoogleCredentialsManager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { TurboModuleRegistry } from 'react-native';
33

44
export interface Spec extends TurboModule {
55
multiply(a: number, b: number): number;
6+
getAndroidVersion(): string;
7+
/**
8+
* Shows Google account picker and returns the ID token.
9+
*/
10+
signIn(serverClientId: string): Promise<string>; // ID Token
611
}
712

813
export default TurboModuleRegistry.getEnforcing<Spec>(

src/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ import GoogleCredentialsManager from './NativeGoogleCredentialsManager';
33
export function multiply(a: number, b: number): number {
44
return GoogleCredentialsManager.multiply(a, b);
55
}
6+
7+
export function getAndroidVersion(): string {
8+
return GoogleCredentialsManager.getAndroidVersion();
9+
}
10+
11+
export function signIn(serverClientId: string): Promise<string> {
12+
return GoogleCredentialsManager.signIn(serverClientId);
13+
}

0 commit comments

Comments
 (0)