Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies {

implementation "androidx.browser:browser:1.2.0"

implementation("com.authsignal:authsignal-android:4.1.0")
implementation("com.authsignal:authsignal-android:4.2.0")

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class AuthsignalPushModule(private val reactContext: ReactApplicationContext) :
map.putString("createdAt", credential.createdAt)
map.putString("userId", credential.userId)
map.putString("lastAuthenticatedAt", credential.lastAuthenticatedAt)
map.putString("expiresAt", credential.expiresAt)
promise.resolve(map)
} else {
promise.resolve(null)
Expand Down Expand Up @@ -78,6 +79,7 @@ class AuthsignalPushModule(private val reactContext: ReactApplicationContext) :
map.putString("createdAt", credential.createdAt)
map.putString("userId", credential.userId)
map.putString("lastAuthenticatedAt", credential.lastAuthenticatedAt)
map.putString("expiresAt", credential.expiresAt)
promise.resolve(map)
}
}
Expand Down Expand Up @@ -157,9 +159,9 @@ class AuthsignalPushModule(private val reactContext: ReactApplicationContext) :
}

@ReactMethod
override fun updateCredential(pushToken: String, promise: Promise) {
override fun updateCredential(pushToken: String?, resetExpiry: Boolean, promise: Promise) {
launch(promise) {
val response = it.updateCredential(pushToken)
val response = it.updateCredential(pushToken, resetExpiry)

val credential = response.data

Expand All @@ -173,6 +175,7 @@ class AuthsignalPushModule(private val reactContext: ReactApplicationContext) :
map.putString("userId", credential.userId)
map.putString("lastVerifiedAt", credential.lastVerifiedAt)
map.putString("pushToken", credential.pushToken)
map.putString("expiresAt", credential.expiresAt)
promise.resolve(map)
} else {
promise.resolve(null)
Expand Down
65 changes: 64 additions & 1 deletion example/src/screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useCallback, useEffect, useRef, useState } from 'react';
import {
Clipboard,
Platform,
ScrollView,
StyleSheet,
Expand Down Expand Up @@ -605,6 +606,34 @@ export function HomeScreen() {
addOutput('Push credential found');
addOutput(` Credential ID: ${response.data.credentialId}`);
addOutput(` User ID: ${response.data.userId}`);
if (response.data.expiresAt) {
addOutput(` Expires At: ${response.data.expiresAt}`);
}
} catch (e) {
addOutput(`Error: ${e}`);
}
};

const copyPushPublicKey = async () => {
const authsignal = authsignalRef.current;
if (!authsignal) return;

try {
const response = await authsignal.push.getCredential();

if (!response.data) {
addOutput(
response.error
? `Error: ${response.error}`
: 'No push credential on this device'
);
return;
}

const publicKey = response.data.credentialId;
Clipboard.setString(publicKey);
addOutput('Public key copied to clipboard');
addOutput(` ${publicKey}`);
} catch (e) {
addOutput(`Error: ${e}`);
}
Expand Down Expand Up @@ -722,7 +751,7 @@ export function HomeScreen() {
const pushToken = `example-push-token-${Date.now()}`;

addOutput('Updating push credential...');
const response = await authsignal.push.updateCredential(pushToken);
const response = await authsignal.push.updateCredential({ pushToken });

if (response.data) {
addOutput('Push credential updated');
Expand All @@ -735,6 +764,30 @@ export function HomeScreen() {
}
};

const resetPushCredentialExpiry = async () => {
const authsignal = authsignalRef.current;
if (!authsignal) return;

try {
addOutput('Resetting push credential expiry...');
const response = await authsignal.push.updateCredential({
resetExpiry: true,
});

if (response.data) {
addOutput('Push credential expiry reset');
addOutput(` Credential ID: ${response.data.userAuthenticatorId}`);
if (response.data.expiresAt) {
addOutput(` New Expires At: ${response.data.expiresAt}`);
}
} else {
addOutput(`Failed to reset push credential expiry: ${response.error}`);
}
} catch (e) {
addOutput(`Error: ${e}`);
}
};

const ActionButton = ({
title,
onPress,
Expand Down Expand Up @@ -834,6 +887,11 @@ export function HomeScreen() {
onPress={getPushCredential}
disabled={!isInitialized}
/>
<ActionButton
title="Copy Public Key"
onPress={copyPushPublicKey}
disabled={!isInitialized}
/>
<ActionButton
title="Enroll Push"
onPress={enrollPush}
Expand All @@ -859,6 +917,11 @@ export function HomeScreen() {
onPress={updatePushCredential}
disabled={!isInitialized}
/>
<ActionButton
title="Reset Expiry"
onPress={resetPushCredentialExpiry}
disabled={!isInitialized}
/>
<ActionButton
title="Remove"
onPress={removePushCredential}
Expand Down
1 change: 1 addition & 0 deletions ios/AuthsignalPushModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ @interface RCT_EXTERN_MODULE(AuthsignalPushModule, NSObject)
reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(updateCredential:(NSString)pushToken
withResetExpiry:(BOOL)withResetExpiry
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

Expand Down
18 changes: 12 additions & 6 deletions ios/AuthsignalPushModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ class AuthsignalPushModule: NSObject {
if let error = response.error {
reject(response.errorCode ?? "unexpected_error", error, nil)
} else if let data = response.data {
let credential: [String: String?] = [
let credential: [String: Any?] = [
"credentialId": data.credentialId,
"createdAt": data.createdAt,
"userId": data.userId,
"lastAuthenticatedAt": data.lastAuthenticatedAt,
"expiresAt": data.expiresAt,
]

resolve(credential)
} else {
resolve(nil)
Expand Down Expand Up @@ -83,11 +84,12 @@ class AuthsignalPushModule: NSObject {
if let error = response.error {
reject(response.errorCode ?? "unexpected_error", error, nil)
} else if let data = response.data {
let credential: [String: String?] = [
let credential: [String: Any?] = [
"credentialId": data.credentialId,
"createdAt": data.createdAt,
"userId": data.userId,
"lastAuthenticatedAt": data.lastAuthenticatedAt,
"expiresAt": data.expiresAt,
]

resolve(credential)
Expand Down Expand Up @@ -182,7 +184,8 @@ class AuthsignalPushModule: NSObject {
}

@objc func updateCredential(
_ pushToken: NSString,
_ pushToken: NSString?,
withResetExpiry resetExpiry: Bool,
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
) -> Void {
Expand All @@ -191,17 +194,20 @@ class AuthsignalPushModule: NSObject {
return
}

let pushTokenStr = pushToken as String?

Task.init {
let response = await authsignal.updateCredential(pushToken: pushToken as String)
let response = await authsignal.updateCredential(pushToken: pushTokenStr, resetExpiry: resetExpiry)

if let error = response.error {
reject(response.errorCode ?? "unexpected_error", error, nil)
} else if let data = response.data {
let credential: [String: String?] = [
let credential: [String: Any?] = [
"userAuthenticatorId": data.userAuthenticatorId,
"userId": data.userId,
"lastVerifiedAt": data.lastVerifiedAt,
"pushToken": data.pushToken,
"expiresAt": data.expiresAt,
]

resolve(credential)
Expand Down
2 changes: 1 addition & 1 deletion ios/RequestMetadata.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Authsignal
import Foundation

private let authsignalReactNativeVersion = "3.0.0"
private let authsignalReactNativeVersion = "3.2.0"

enum RequestMetadata {
static func configure() {
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": "3.1.0",
"version": "3.2.0",
"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 @@ -18,7 +18,7 @@ Pod::Spec.new do |s|
s.private_header_files = "ios/**/*.h"

s.dependency "React-Core"
s.dependency 'Authsignal', '~> 2.11.0'
s.dependency 'Authsignal', '~> 2.12.0'

# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
Expand Down
5 changes: 4 additions & 1 deletion src/NativeAuthsignalPushModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export interface Spec extends TurboModule {
withApproval: boolean,
withVerificationCode: string | null
): Promise<boolean>;
updateCredential(pushToken: string): Promise<Object | null>;
updateCredential(
pushToken: string | null,
withResetExpiry: boolean
): Promise<Object | null>;
}

export default TurboModuleRegistry.get<Spec>('AuthsignalPushModule');
11 changes: 9 additions & 2 deletions src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
AppCredential,
AuthsignalResponse,
UpdateChallengeInput,
UpdateCredentialInput,
UpdatedAppCredential,
} from './types';

Expand Down Expand Up @@ -141,13 +142,19 @@ export class AuthsignalPush {
}

async updateCredential(
pushToken: string
input?: UpdateCredentialInput | string
): Promise<AuthsignalResponse<UpdatedAppCredential>> {
await this.ensureModuleIsInitialized();

const { pushToken, resetExpiry = false } =
typeof input === 'string'
? { pushToken: input, resetExpiry: false }
: input ?? {};

try {
const data = (await AuthsignalPushModule.updateCredential(
pushToken
pushToken ?? null,
resetExpiry
)) as UpdatedAppCredential;

return { data };
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@ export interface AppCredential {
createdAt: string;
userId: string;
lastAuthenticatedAt?: string;
expiresAt?: string;
}

export interface UpdateCredentialInput {
pushToken?: string;
resetExpiry?: boolean;
}

export interface UpdatedAppCredential {
userAuthenticatorId: string;
userId: string;
lastVerifiedAt: string;
pushToken: string;
pushToken?: string;
expiresAt?: string;
}

export interface ClaimChallengeInput {
Expand Down
Loading