@open_auth/auth
v0.1.7
Published
A simple and lightweight authentication library for web apps using **One-Time Passwords (OTP)** and **Password-based login**. π Built in **TypeScript**, compiled for both **CommonJS** and **ESModules**.
Downloads
46
Readme
@open_auth/auth π
A simple and lightweight authentication library for web apps using One-Time Passwords (OTP) and Password-based login. π
Built in TypeScript, compiled for both CommonJS and ESModules.
β Supports:
- βοΈ Sign Up (OTP and Password based)
- β Sign In (OTP and Password based)
- π Resend OTP
- πͺ Sign Out
π How It Works
- This auth system uses JWT (JSON Web Tokens) for user sessions.
- JWTs are stored securely in HTTP-only cookies.
- The backend generates tokens after sign-in, and the client stores them in the browser using cookies.
- This setup is secure and works well across multiple platforms.
π¦ Installation
# npm
npm install @open_auth/auth
# yarn
yarn add @open_auth/authπ§© Setup
π Environment Variables
Backend App
These variables are required in your backend server (.env file):
| Variable | Description |
|------------------|-------------------------------------------|
| DATABASE_URL | PostgreSQL connection string |
| FROM_EMAIL | Your email for sending OTPs |
| APP_PASSWORD | Google app password for email |
| AUTH_SECRET | Secret used to sign JWT tokens |
DATABASE_URL=postgresql://user:pass@localhost:5432/dbname
[email protected]
APP_PASSWORD=your-google-app-password
AUTH_SECRET=your-secure-auth-secretπ Note: PostgreSQL is required as the backend database.
π Usage Note
Each auth function is meant to be used on its specific page:
| Route Path | Function to Call |
|------------------|--------------------------|
| /signUp | signUp() |
| /signUpPassword | signUpPassword() |
| /signIn | signIn() |
| /signInPassword | signInPassword() |
βοΈ Calling these functions outside their intended pages may cause issues.
π¦ Initialize Client
You must create an instance of the client using the backend URL:
import { CreateOpenAuthClient } from "@open_auth/auth/client";
const openAuth = new CreateOpenAuthClient({
backendUrl: "http://localhost:3000" // or process.env.NEXT_PUBLIC_BACKEND_URL
});
export default openAuth;You can place this in lib/open_auth.ts and import it throughout your app.
Alternatively, use directly where needed.
π§ Project Configuration
Make sure your project includes this in tsconfig.json or jsconfig.json:
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler"
}
}π§ͺ Usage
Import from your openAuth instance:
import openAuth from "@/lib/open_auth";
await openAuth.signUp({ username: "user", email: "[email protected]" });Each function returns { err: string } or a success response.
𧬠Examples
π£ signUp
React
await openAuth.signUp({ username: "user", email: "[email protected]" });Vanilla JS (CommonJS)
const { CreateOpenAuthClient } = require("@open_auth/auth/client");
const openAuth = new CreateOpenAuthClient({ backendUrl: "http://localhost:3000" });
openAuth.signUp({ username: "user", email: "[email protected]" });π£ signUpPassword
await openAuth.signUpPassword({ password: 123456 });const { CreateOpenAuthClient } = require("@open_auth/auth/client");
const openAuth = new CreateOpenAuthClient({ backendUrl: "http://localhost:3000" });
openAuth.signUpPassword({ password: 123456 });π’ signIn
await openAuth.signIn({ email: "[email protected]" });const { CreateOpenAuthClient } = require("@open_auth/auth/client");
const openAuth = new CreateOpenAuthClient({ backendUrl: "http://localhost:3000" });
openAuth.signIn({ email: "[email protected]" });π’ signInPassword
await openAuth.signInPassword({ password: 123456 });const { CreateOpenAuthClient } = require("@open_auth/auth/client");
const openAuth = new CreateOpenAuthClient({ backendUrl: "http://localhost:3000" });
openAuth.signInPassword({ password: 123456 });π resendPass
await openAuth.resendPass();const { CreateOpenAuthClient } = require("@open_auth/auth/client");
const openAuth = new CreateOpenAuthClient({ backendUrl: "http://localhost:3000" });
openAuth.resendPass();πͺ signOut
openAuth.signOut();const { CreateOpenAuthClient } = require("@open_auth/auth/client");
const openAuth = new CreateOpenAuthClient({ backendUrl: "http://localhost:3000" });
openAuth.signOut();π οΈ Backend Server Setup
π§© Express.js Example
const express = require("express");
const cors = require("cors");
import { OpenAuthBackend } from '@open_auth/auth'
require("dotenv").config();
const app = express();
app.use(cors());
app.use(express.json());
const port = process.env.PORT || 3000;
const open_auth_backend = new OpenAuthBackend();
app.post("/api/auth/open_auth", (req, res) => {
open_auth_backend.Main(req.headers.from, req.body).then(data => {
res.json(data);
});
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});π΅ Next.js Example (app/api route)
import { NextRequest, NextResponse } from 'next/server'
import { OpenAuthBackend } from '@open_auth/auth'
export async function POST(req) { // req: NextRequest for route.ts
const data = await req.json()
const from = req.headers.get('from')
const open_auth_backend = new OpenAuthBackend();
const res = await open_auth_backend.Main(from , data)
return NextResponse.json(res)
}π API Reference
| Function | Params | Returns |
|-------------------|-----------------------------------|----------------------------------------|
| signUp | { username, email } | Promise<{ err: any } | undefined> |
| signUpPassword | { password } | Promise<{ err: any } | undefined> |
| signIn | { email } | Promise<{ err: any } | undefined> |
| signInPassword | { password } | Promise<{ err: any } | undefined> |
| resendPass | none | Promise<{ err: any } or { message }> |
| signOut | none | { message: string } |
π License
MIT
