Skip to main content

Basic usage

Import the necessary functions from the package:

import {
signUpWithPasskeys,
signUpWithPassword,
signUpWithGoogle,
signOut,
signIn,
} from 'react-native-credentials-manager';

Authentication Methods

1. Passkeys Authentication

To implement passkey-based authentication:

// Signup with Passkeys
const passkeyRequest = {
challenge: 'c29tZS1yYW5kb20tY2hhbGxlbmdl',
rp: {
name: 'YourAppName',
id: 'your.domain.com',
},
user: {
id: 'dXNlcl9pZF8xMjM0NTY=',
name: 'username',
displayName: 'User Display Name',
},
pubKeyCredParams: [
{
type: 'public-key',
alg: -7,
},
{
type: 'public-key',
alg: -257,
},
],
timeout: 1800000,
attestation: 'none',
excludeCredentials: [],
authenticatorSelection: {
authenticatorAttachment: 'platform',
requireResidentKey: true,
residentKey: 'required',
userVerification: 'required',
},
};

try {
const response = await signUpWithPasskeys(passkeyRequest);
console.log('Passkey registration successful:', response);
} catch (error) {
console.error('Passkey registration failed:', error);
}

2. Password Authentication

For traditional username/password authentication:

// Sign up with password
try {
signUpWithPassword({
username: 'user@example.com',
password: 'securePassword123!'
});
} catch (error) {
console.error('Password registration failed:', error);
}

3. Google Sign-In

To implement Google Sign-In:

const GOOGLE_WEB_CLIENT_ID = 'your-web-client-id';

// Sign up with Google
try {
const googleCredential = await signUpWithGoogle({
serverClientId: GOOGLE_WEB_CLIENT_ID,
autoSelectEnabled: true,
});

console.log('Google sign-in successful:', {
id: googleCredential.id,
idToken: googleCredential.idToken,
displayName: googleCredential.displayName,
});
} catch (error) {
console.error('Google sign-in failed:', error);
}

Sign In

The library supports multiple authentication methods in a single sign-in flow:

try {
const credential = await signIn(
['passkeys', 'password', 'google-signin'],
{
passkeys: {
challenge: 'your-challenge-string',
timeout: 1800000,
userVerification: 'required',
rpId: 'your.domain.com',
},
googleSignIn: {
serverClientId: GOOGLE_WEB_CLIENT_ID,
autoSelectEnabled: true,
},
}
);

// Handle different credential types
switch (credential.type) {
case 'passkey':
console.log('Passkey auth response:', credential.authenticationResponseJson);
break;
case 'password':
console.log('Password auth:', credential.username);
break;
case 'google-signin':
console.log('Google auth:', credential.idToken);
break;
}
} catch (error) {
console.error('Sign-in failed:', error);
}

Sign Out

To sign out the user:

try {
await signOut();
console.log('Successfully signed out');
} catch (error) {
console.error('Sign-out failed:', error);
}