Skip to content

Commit

Permalink
Merge pull request #14 from passageidentity/PSG-3191-add-identifierex…
Browse files Browse the repository at this point in the history
…ists

Added `identifierExists` method and update native dependencies.
  • Loading branch information
rickycpadilla authored Nov 29, 2023
2 parents 043bd85 + 4d5f22c commit 7f6fa3c
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 54 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ dependencies {
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

implementation "id.passage.android:passage:1.2.0"
implementation "id.passage.android:passage:1.5.1"
implementation "com.google.code.gson:gson:2.9.0"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import com.google.gson.Gson
import id.passage.android.Passage
import id.passage.android.PassageToken
import id.passage.android.exceptions.AddDevicePasskeyCancellationException
import id.passage.android.exceptions.AppInfoException
import id.passage.android.exceptions.GetMagicLinkStatusInvalidException
import id.passage.android.exceptions.GetMagicLinkStatusNotFoundException
import id.passage.android.exceptions.LoginWithPasskeyCancellationException
import id.passage.android.exceptions.OneTimePasscodeActivateExceededAttemptsException
import id.passage.android.exceptions.PassageUserUnauthorizedException
import id.passage.android.exceptions.RegisterWithPasskeyCancellationException

Expand Down Expand Up @@ -68,7 +66,7 @@ class PassageReactNativeModule(reactContext: ReactApplicationContext) :
fun loginWithPasskey(promise: Promise) {
CoroutineScope(Dispatchers.IO).launch {
try {
val authResult = passage.loginWithPasskey("")
val authResult = passage.loginWithPasskey()
val jsonString = Gson().toJson(authResult)
promise.resolve(jsonString)
} catch (e: Exception) {
Expand Down Expand Up @@ -124,6 +122,8 @@ class PassageReactNativeModule(reactContext: ReactApplicationContext) :
val authResult = passage.oneTimePasscodeActivate(otp, otpId)
val jsonString = Gson().toJson(authResult)
promise.resolve(jsonString)
} catch (e: OneTimePasscodeActivateExceededAttemptsException) {
promise.reject("OTP_ACTIVATION_EXCEEDED_ATTEMPTS", e.message, e)
} catch (e: Exception) {
promise.reject("OTP_ERROR", e.message, e)
}
Expand Down Expand Up @@ -223,7 +223,7 @@ class PassageReactNativeModule(reactContext: ReactApplicationContext) :
fun getAppInfo(promise: Promise) {
CoroutineScope(Dispatchers.IO).launch {
try {
val appInfo = passage.appInfo() ?: throw AppInfoException("Error getting app info")
val appInfo = passage.appInfo()
val jsonString = Gson().toJson(appInfo)
promise.resolve(jsonString)
} catch (e: Exception) {
Expand All @@ -232,6 +232,19 @@ class PassageReactNativeModule(reactContext: ReactApplicationContext) :
}
}

@ReactMethod
fun identifierExists(identifier: String, promise: Promise) {
CoroutineScope(Dispatchers.IO).launch {
try {
val user = passage.identifierExists(identifier)
val jsonString = if (user == null) null else Gson().toJson(user)
promise.resolve(jsonString)
} catch (e: Exception) {
promise.reject("IDENTIFIER_EXISTS_ERROR", e.message, e)
}
}
}

// endregion

// region USER METHODS
Expand Down Expand Up @@ -327,7 +340,7 @@ class PassageReactNativeModule(reactContext: ReactApplicationContext) :
CoroutineScope(Dispatchers.IO).launch {
try {
val user = passage.getCurrentUser() ?: throw PassageUserUnauthorizedException("User is not authorized.")
val magicLinkId = user.changeEmail(newEmail)?.id
val magicLinkId = user.changeEmail(newEmail).id
promise.resolve(magicLinkId)
} catch (e: Exception) {
var errorCode = "CHANGE_EMAIL_ERROR"
Expand All @@ -346,7 +359,7 @@ class PassageReactNativeModule(reactContext: ReactApplicationContext) :
CoroutineScope(Dispatchers.IO).launch {
try {
val user = passage.getCurrentUser() ?: throw PassageUserUnauthorizedException("User is not authorized.")
val magicLinkId = user.changePhone(newPhone)?.id
val magicLinkId = user.changePhone(newPhone).id
promise.resolve(magicLinkId)
} catch (e: Exception) {
var errorCode = "CHANGE_PHONE_ERROR"
Expand Down
10 changes: 5 additions & 5 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ PODS:
- hermes-engine/Pre-built (0.71.10)
- libevent (2.1.12)
- OpenSSL-Universal (1.1.1100)
- Passage (1.2.0):
- Passage (1.4.0):
- SwiftKeychainWrapper
- passage-react-native (0.1.4):
- Passage (= 1.2.0)
- passage-react-native (0.4.0):
- Passage (= 1.4.0)
- React-Core
- RCT-Folly (2021.07.22.00):
- boost
Expand Down Expand Up @@ -598,8 +598,8 @@ SPEC CHECKSUMS:
hermes-engine: d27603b55a48402501ad1928c05411dae9cd6b85
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
Passage: 623d579acf6cf9949f6c16c777b7efe6f6902d93
passage-react-native: 96089329c8205f9c49013af37f592506ccd74fe6
Passage: 4b543698ee5cfac577d9fce473f75587315269e5
passage-react-native: 035fe2564bf099c3aed0543f5c1158dfc4c6b0ea
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCTRequired: 8ef706f91e2b643cd32c26a57700b5f24fab0585
RCTTypeSafety: 5fbddd8eb9242b91ac0d901c01da3673f358b1b7
Expand Down
4 changes: 4 additions & 0 deletions ios/PassageReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ @interface RCT_EXTERN_MODULE(PassageReactNative, NSObject)
RCT_EXTERN_METHOD(getAppInfo:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject);

RCT_EXTERN_METHOD(identifierExists:(NSString *)identifier
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject);


// MARK: - User Methods
RCT_EXTERN_METHOD(getCurrentUser:(RCTPromiseResolveBlock)resolve
Expand Down
18 changes: 17 additions & 1 deletion ios/PassageReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ class PassageReactNative: NSObject {
let authResult = try await passage.oneTimePasscodeActivate(otp: otp, otpId: otpId)
resolve(authResult.toJsonString())
} catch {
reject("OTP_ERROR", "\(error)", nil)
var errorCode = "OTP_ERROR"
if case PassageOTPError.exceededAttempts = error {
errorCode = "OTP_ACTIVATION_EXCEEDED_ATTEMPTS"
}
reject(errorCode, "\(error)", nil)
}
}
}
Expand Down Expand Up @@ -252,6 +256,18 @@ class PassageReactNative: NSObject {
}
}

@objc(identifierExists:withResolver:withRejecter:)
internal func identifierExists(
identifier: String,
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
) {
Task {
let user = try? await PassageAuth.getUser(identifier: identifier)
resolve(user?.toJsonString())
}
}

// MARK: - User Methods

@objc(getCurrentUser:withRejecter:)
Expand Down
38 changes: 0 additions & 38 deletions ios/passage-react-native.podspec

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@passageidentity/passage-react-native",
"version": "0.3.0",
"version": "0.4.0",
"description": "test",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
2 changes: 1 addition & 1 deletion passage-react-native.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Pod::Spec.new do |s|

s.dependency "React-Core"

s.dependency 'Passage', '1.2.0'
s.dependency 'Passage', '1.4.0'
s.platform = :ios, '16.0'

# Don't install the dependencies when we run `pod install` in the old architecture.
Expand Down
20 changes: 20 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type GetAuthToken = () => Promise<string | null>;
type IsAuthTokenValid = (authToken: string) => Promise<boolean>;
type RefreshAuthToken = () => Promise<string | null>;
type GetAppInfo = () => Promise<PassageAppInfo>;
type IdentifierExists = (identifier: string) => Promise<PassageUser | null>;
type AddPasskey = () => Promise<Passkey>;
type DeletePasskey = (passkeyId: string) => Promise<void>;
type EditPasskeyName = (
Expand Down Expand Up @@ -424,6 +425,25 @@ class Passage {
}
};

/**
* Check if a user with a given identifier exists. If so, this method will return user info.
*
* @return {Promise<PassageUser | null>} A data object containing user information.
* @throws {PassageError}
*/
identifierExists: IdentifierExists = async (
identifier: string
): Promise<PassageUser | null> => {
try {
const result = await PassageReactNative.identifierExists(identifier);
if (!result) return null;
const parsedResult = JSON.parse(result);
return parsedResult;
} catch (error: any) {
throw new PassageError(error.code, error.message);
}
};

// USER METHODS

/**
Expand Down

0 comments on commit 7f6fa3c

Please sign in to comment.