Skip to content

Commit

Permalink
Support preferImmediatelyAvailableCredentials (#18)
Browse files Browse the repository at this point in the history
* Support preferImmediatelyAvailableCredentials

* wip

* wip

* bump
  • Loading branch information
chrisfisher authored Aug 5, 2024
1 parent 2e47108 commit 537b36c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions ios/AuthsignalPasskeyModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ @interface RCT_EXTERN_MODULE(AuthsignalPasskeyModule, NSObject)
RCT_EXTERN_METHOD(signIn:(NSString)action
withToken:(NSString)token
withAutofill:(BOOL)autofill
withPreferImmediatelyAvailableCredentials:(BOOL)preferImmediatelyAvailableCredentials
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)

Expand Down
16 changes: 12 additions & 4 deletions ios/AuthsignalPasskeyModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AuthsignalPasskeyModule: NSObject {
let response = await authsignal!.signUp(token: tokenStr, userName: userNameStr, displayName: displayNameStr)

if (response.error != nil) {
reject("signUp error", response.error, nil)
reject("signUpError", response.error, nil)
} else {
let signUpResponse: [String: String?] = [
"token": response.data!.token,
Expand All @@ -56,6 +56,7 @@ class AuthsignalPasskeyModule: NSObject {
_ action: NSString?,
withToken token: NSString?,
withAutofill autofill: Bool,
withPreferImmediatelyAvailableCredentials preferImmediatelyAvailableCredentials: Bool,
resolver resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) -> Void {
Expand All @@ -68,10 +69,17 @@ class AuthsignalPasskeyModule: NSObject {
let tokenStr = token as String?

Task.init {
let response = await authsignal!.signIn(token: tokenStr, action: actionStr, autofill: autofill)
let response = await authsignal!.signIn(
token: tokenStr,
action: actionStr,
autofill: autofill,
preferImmediatelyAvailableCredentials: preferImmediatelyAvailableCredentials
)

if (response.error != nil) {
reject("signIn error", response.error, nil)
if (response.errorCode == .canceled) {
reject("signInCanceled", "SIGN_IN_CANCELED", nil)
} else if (response.error != nil) {
reject("signInError", response.error, nil)
} else {
let signInResponse: [String: Any?] = [
"isVerified": response.data!.isVerified,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-authsignal",
"version": "0.4.2",
"version": "0.4.3",
"description": "The official Authsignal React Native library.",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
2 changes: 1 addition & 1 deletion react-native-authsignal.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Pod::Spec.new do |s|
s.source_files = "ios/**/*.{h,m,mm,swift}"

s.dependency "React-Core"
s.dependency 'Authsignal', '0.3.0'
s.dependency 'Authsignal', '0.3.1'

# Don't install the dependencies when we run `pod install` in the old architecture.
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
Expand Down
4 changes: 4 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export const LINKING_ERROR =
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n';

export enum ErrorCode {
passkeySignInCanceled = 'passkeySignInCanceled',
}
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AuthsignalPasskey } from './passkey';
import { AuthsignalPush } from './push';

export * from './types';
export { ErrorCode } from './error';

const AuthsignalModule = NativeModules.AuthsignalModule
? NativeModules.AuthsignalModule
Expand Down
14 changes: 11 additions & 3 deletions src/passkey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NativeModules, Platform } from 'react-native';
import { LINKING_ERROR } from './error';
import { LINKING_ERROR, ErrorCode } from './error';
import type {
AuthsignalResponse,
SignInResponse,
Expand All @@ -22,6 +22,7 @@ interface PasskeySignInInput {
action?: string;
token?: string;
autofill?: boolean;
preferImmediatelyAvailableCredentials?: boolean;
}

let initialized = false;
Expand Down Expand Up @@ -81,6 +82,7 @@ export class AuthsignalPasskey {
action,
token,
autofill = false,
preferImmediatelyAvailableCredentials = true,
}: PasskeySignInInput = {}): Promise<AuthsignalResponse<SignInResponse>> {
await this.ensureModuleIsInitialized();

Expand All @@ -97,7 +99,8 @@ export class AuthsignalPasskey {
const data = await AuthsignalPasskeyModule.signIn(
action,
token,
autofill
autofill,
preferImmediatelyAvailableCredentials
);

autofillRequestPending = false;
Expand All @@ -118,7 +121,12 @@ export class AuthsignalPasskey {
autofillRequestPending = false;

if (ex instanceof Error) {
return { error: ex.message };
return ex.message === 'SIGN_IN_CANCELED'
? {
errorCode: ErrorCode.passkeySignInCanceled,
error: 'Passkey sign-in canceled',
}
: { error: ex.message };
}

throw ex;
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ErrorCode } from './error';

export interface AuthsignalResponse<T> {
data?: T;
error?: string;
errorCode?: ErrorCode;
}

export interface TokenPayload {
Expand Down

0 comments on commit 537b36c

Please sign in to comment.