npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nicelogin-react

v0.1.0

Published

NiceLogin React SDK - Simple authentication for React applications

Readme

nicelogin-react

React SDK for NiceLogin authentication. Simple, minimal, zero external dependencies.

Installation

npm install nicelogin-react

Quick Start

1. Wrap your app with NiceLoginProvider

import { NiceLoginProvider } from 'nicelogin-react';

function App() {
  return (
    <NiceLoginProvider apiKey="nicelogin_your_api_key">
      <YourApp />
    </NiceLoginProvider>
  );
}

2. Use the useNiceLogin hook

import { useNiceLogin } from 'nicelogin-react';

function LoginPage() {
  const { login, isAuthenticated, user, isLoading } = useNiceLogin();

  if (isLoading) return <div>Loading...</div>;

  if (isAuthenticated) {
    return <div>Welcome, {user.email}!</div>;
  }

  const handleLogin = async () => {
    try {
      await login('[email protected]', 'password123');
    } catch (error) {
      console.error(error.message);
    }
  };

  return <button onClick={handleLogin}>Login</button>;
}

API Reference

NiceLoginProvider

Context provider that wraps your application.

<NiceLoginProvider
  apiKey="nicelogin_xxx"          // Required: Your API key
  baseUrl="https://custom.api"    // Optional: Custom API URL
  storage="localStorage"          // Optional: "localStorage" (default) or "memory"
>
  <App />
</NiceLoginProvider>

| Prop | Type | Default | Description | |------|------|---------|-------------| | apiKey | string | Required | Your NiceLogin API key | | baseUrl | string | https://api.v1.nicelogin.com | Custom API base URL | | storage | "localStorage" \| "memory" | "localStorage" | Token storage method |


useNiceLogin()

Main hook for authentication. Must be used within NiceLoginProvider.

const {
  user,                  // User | null
  token,                 // string | null
  isAuthenticated,       // boolean
  isLoading,             // boolean
  login,                 // (email, password) => Promise<void>
  logout,                // () => void
  register,              // (email, password, userData?) => Promise<void>
  requestPasswordReset,  // (email) => Promise<string>
} = useNiceLogin();

Return Values

| Property | Type | Description | |----------|------|-------------| | user | User \| null | Current authenticated user | | token | string \| null | Raw JWT token | | isAuthenticated | boolean | true if user is logged in | | isLoading | boolean | true during initial auth check | | login | function | Login with email and password | | logout | function | Logout and clear token | | register | function | Register new user | | requestPasswordReset | function | Request password reset token |

User Object

interface User {
  id: string;         // User UUID
  email: string;      // User email
  companyId: string;  // Company UUID
  userData?: object;  // Custom user data
}

Usage Examples

Login

const { login } = useNiceLogin();

try {
  await login('[email protected]', 'password123');
  // Success - user is now authenticated
} catch (error) {
  console.error(error.message); // "Invalid credentials"
}

Logout

const { logout } = useNiceLogin();

logout(); // Clears token and resets state

Register

const { register } = useNiceLogin();

try {
  await register('[email protected]', 'password123', {
    name: 'John Doe',
    plan: 'premium'
  });
  // Success - user is registered and logged in
} catch (error) {
  console.error(error.message);
}

Request Password Reset

const { requestPasswordReset } = useNiceLogin();

try {
  const resetToken = await requestPasswordReset('[email protected]');
  // Use resetToken to complete password reset
} catch (error) {
  console.error(error.message);
}

Protected Route

import { Navigate } from 'react-router-dom';
import { useNiceLogin } from 'nicelogin-react';

function ProtectedRoute({ children }) {
  const { isAuthenticated, isLoading } = useNiceLogin();

  if (isLoading) return <div>Loading...</div>;
  if (!isAuthenticated) return <Navigate to="/login" />;

  return children;
}

// Usage
<ProtectedRoute>
  <Dashboard />
</ProtectedRoute>

Access Raw Token

const { token } = useNiceLogin();

// Use token for authenticated API requests
fetch('/api/data', {
  headers: {
    'Authorization': `Bearer ${token}`
  }
});

Token Storage

localStorage (default)

  • Persists across page refreshes
  • Persists across browser tabs
  • Auto-login on page load

memory

  • Lost on page refresh
  • More secure for sensitive applications
  • No persistence
<NiceLoginProvider apiKey="xxx" storage="memory">

Error Handling

All methods throw NiceLoginError on failure:

import { NiceLoginError } from 'nicelogin-react';

try {
  await login(email, password);
} catch (error) {
  if (error instanceof NiceLoginError) {
    console.log(error.message); // Error message
    console.log(error.status);  // HTTP status code
  }
}

Types

import type {
  User,
  TokenPayload,
  LoginResponse,
  RegisterResponse,
  PasswordResetResponse,
  NiceLoginConfig,
  AuthState,
  AuthContextValue,
} from 'nicelogin-react';

Utilities

decodeToken

Decode JWT payload (no validation):

import { decodeToken } from 'nicelogin-react';

const payload = decodeToken(token);
console.log(payload.sub);   // User ID
console.log(payload.email); // User email
console.log(payload.exp);   // Expiration timestamp

isTokenExpired

Check if token is expired:

import { isTokenExpired } from 'nicelogin-react';

if (isTokenExpired(token)) {
  console.log('Token has expired');
}

Requirements

  • React >= 18.0.0
  • Modern browser with fetch API

License

MIT