npm.io
1.0.0 • Published 3 months ago

react-native-amazon-login

Licence
MIT
Version
1.0.0
Deps
1
Size
57.5 MB
Vulns
0
Weekly
0

react-native-amazon-login

Amazon Login module for React Native and Expo applications. Supports both iOS and Android.

Features

  • iOS Support (LoginWithAmazon SDK)
  • Android Support (Login with Amazon SDK)
  • Expo Config Plugin for easy setup
  • TypeScript support
  • Promise-based API
  • Event listeners for login/logout

Installation

npm install react-native-amazon-login

Configuration

1. Get Your Amazon API Keys
For iOS:
  1. Go to Amazon Developer Console
  2. Create a Security Profile
  3. Add iOS Settings with your bundle identifier
  4. Copy the API Key (JWT token)
For Android:
  1. Go to Amazon Developer Console
  2. Add Android Settings
  3. Register your package name and app signature
  4. Copy the API Key
2. Add the Expo Config Plugin

In your app.json:

{
  "expo": {
    "plugins": [
      [
        "react-native-amazon-login",
        {
          "amazonApiKey": "YOUR_AMAZON_API_KEY"
        }
      ]
    ]
  }
}
3. Platform-Specific Setup
iOS Setup

The iOS SDK is included in the package. No additional steps needed!

Android Setup
  1. Download the Amazon Login SDK from Amazon Developer Portal
  2. Copy login-with-amazon-sdk.aar to node_modules/react-native-amazon-login/android/libs/

See ANDROID_INSTALLATION.md for detailed instructions.

4. Prebuild and Run
npx expo prebuild --clean
npx expo run:ios
npx expo run:android

Usage

Basic Login/Logout
import {
  login,
  logout,
  isLoggedIn,
  getUserProfile,
} from "react-native-amazon-login";

// Login
const handleLogin = async () => {
  try {
    const userProfile = await login();
    console.log("User:", userProfile);
    // { userId, name, email, postalCode, accessToken }
  } catch (error) {
    console.error("Login failed:", error);
  }
};

// Logout
const handleLogout = async () => {
  try {
    await logout();
    console.log("Logged out");
  } catch (error) {
    console.error("Logout failed:", error);
  }
};

// Check login status
const loggedIn = isLoggedIn();

// Get user profile
const profile = await getUserProfile();
Event Listeners
import {
  addLoginSuccessListener,
  addLoginFailureListener,
  addLogoutListener,
} from "react-native-amazon-login";

// Listen for login success
const successSubscription = addLoginSuccessListener((userData) => {
  console.log("Login successful:", userData);
});

// Listen for login failure
const failureSubscription = addLoginFailureListener((error) => {
  console.error("Login failed:", error);
});

// Listen for logout
const logoutSubscription = addLogoutListener(() => {
  console.log("User logged out");
});

// Clean up
successSubscription.remove();
failureSubscription.remove();
logoutSubscription.remove();
Complete Example
import React, { useEffect, useState } from 'react';
import { View, Button, Text } from 'react-native';
import {
  login,
  logout,
  addLoginSuccessListener,
  addLoginFailureListener
} from 'react-native-amazon-login';

export default function App() {
  const [user, setUser] = useState(null);

  useEffect(() => {
    const successSub = addLoginSuccessListener((userData) => {
      setUser(userData);
    });

    const failureSub = addLoginFailureListener((error) => {
      console.error('Login error:', error);
    });

    return () => {
      successSub.remove();
      failureSub.remove();
    };
  }, []);

  const handleLogin = async () => {
    try {
      const userData = await login();
      setUser(userData);
    } catch (error) {
      console.error('Login failed:', error);
    }
  };

  const handleLogout = async () => {
    try {
      await logout();
      setUser(null);
    } catch (error) {
      console.error('Logout failed:', error);
    }
  };

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      {user ? (
        <>
          <Text>Welcome, {user.name || user.email}!</Text>
          <Button title="Logout" onPress={handleLogout} />
        </>
      ) : (
        <Button title="Login with Amazon" onPress={handleLogin} />
      )}
    </View>
  );
}

API Reference

Methods
login(): Promise<AmazonUserProfile>

Initiates the Amazon login flow.

Returns: Promise that resolves with user profile data.

const user = await login();
// { userId, name, email, postalCode, accessToken }
logout(): Promise<void>

Signs out the current user.

await logout();
isLoggedIn(): boolean

Checks if a user is currently logged in.

const loggedIn = isLoggedIn();
getUserProfile(): Promise<AmazonUserProfile>

Fetches the current user's profile.

const profile = await getUserProfile();
Event Listeners
addLoginSuccessListener(callback)

Listens for successful login events.

addLoginFailureListener(callback)

Listens for login failure events.

addLogoutListener(callback)

Listens for logout events.

Types
interface AmazonUserProfile {
  userId: string;
  name?: string;
  email?: string;
  postalCode?: string;
  accessToken?: string;
}

interface LoginFailureEventPayload {
  type: string;
  error: string;
  userDidCancel: boolean;
  code?: string;
  domain?: string;
}

Troubleshooting

iOS Issues

"LoginWithAmazon not found"

  • Run npx expo prebuild --clean
  • Check that the plugin is configured in app.json

"Invalid API Key"

  • Verify your API key in Amazon Developer Console
  • Check that your bundle identifier matches
Android Issues

"SDK not found"

"Invalid API Key"

  • Verify your package name matches
  • Check that your app signature is registered
  • Make sure you're using the correct keystore
General Issues

"Plugin not found"

  • Run npm install to ensure the package is installed
  • Run npx expo prebuild --clean to regenerate native code

Documentation

Requirements

  • Expo SDK 50+
  • iOS 15.1+
  • Android API 24+
  • React Native 0.70+

License

MIT

Author

samsonroy samsonmaben@gmail.com