cosmic-auth
v1.0.0
Published
An npm package for generating passwords using truly random numbers from cosmic radiation via cTRNG
Downloads
9
Readme
Cosmic Auth
A Node.js package that generates passwords using truly random numbers from the cTRNG API.
Installation
npm install cosmic-authEnvironment Variables
Create a .env file in your project root with the following variables:
# Auth0 credentials for getting access token
ORBITPORT_API_URL=https://dev-1usujmbby8627ni8.us.auth0.com
ORBITPORT_AUTH_URL=https://op.spacecomputer.io
ORBITPORT_CLIENT_ID=your-client-id
ORBITPORT_CLIENT_SECRET=your-client-secretGetting an Access Token
Before using the package, you need to get an access token from Auth0. Here's how:
- Make sure your
.envfile is set up with the correct credentials - Run the following script to get your access token:
require('dotenv').config();
const fetch = require('node-fetch');
(async () => {
try {
const url = `${process.env.ORBITPORT_API_URL}/oauth/token`;
const body = {
client_id: process.env.ORBITPORT_CLIENT_ID,
client_secret: process.env.ORBITPORT_CLIENT_SECRET,
audience: `${process.env.ORBITPORT_AUTH_URL}/api`,
grant_type: "client_credentials"
};
const res = await fetch(url, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body)
});
const data = await res.json();
console.log(data);
} catch (err) {
console.error("Error:", err);
}
})();The response will include your access token:
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlRRaDYtdGZhTmxhNV9KeGtDeVNKZyJ9...",
"expires_in": 86400,
"token_type": "Bearer"
}Usage
import { generatePassword } from 'cosmic-auth';
// Generate a password with default options
const password = await generatePassword();
// Or with custom options
const customPassword = await generatePassword({
length: 16,
includeUppercase: true,
includeNumbers: true,
includeSpecialChars: true
});API
generatePassword(options?: PasswordOptions): Promise
Generates a random password using the cTRNG API.
Options
length(number): Length of the password (default: 12)includeUppercase(boolean): Include uppercase letters (default: true)includeNumbers(boolean): Include numbers (default: true)includeSpecialChars(boolean): Include special characters (default: true)
Demo App
The demo app shows how to use the package in a Next.js application. It features a beautiful signup form with automatic password generation.
Key Features
- Automatic password generation on first focus
- Manual password generation with refresh button
- Password suggestion with "Use this" option
- Form validation and success feedback
- Modern, responsive UI with Tailwind CSS
Implementation
The demo consists of two main files:
Frontend (
src/app/page.tsx):- Signup form with name, email, DOB, gender, and password fields
- Password field with auto-generation on first focus
- Refresh icon button for manual generation
- Password suggestion display
- Form submission handling
Backend (
src/app/api/generate/route.ts):- API route for password generation
- Default password options
- Error handling
- Response formatting
How It Works
First Focus Generation:
const handlePasswordFocus = () => { if (!hasGenerated.current && !formData.password) { generatePassword(); } };Manual Generation:
<button type="button" onClick={generatePassword} className="absolute right-2 top-1/2 -translate-y-1/2 p-1" > <ArrowPathIcon className={`h-5 w-5 ${loading ? 'animate-spin' : ''}`} /> </button>Password Suggestion:
{suggestedPassword && ( <div className="mt-2 p-2 bg-gray-50 rounded-md"> <p className="text-sm text-black">Suggested password:</p> <div className="flex items-center justify-between gap-2"> <code className="text-sm font-mono">{suggestedPassword}</code> <button onClick={() => setFormData({ ...formData, password: suggestedPassword })}> Use this </button> </div> </div> )}
Running the Demo
- Clone the repository
- Navigate to the demo directory:
cd cosmic-auth-demo/cosmic-auth-demo - Install dependencies:
npm install - Create a
.envfile with your Orbitport credentials - Start the development server:
npm run dev
The demo will be available at http://localhost:3000.
License
MIT
