Authentication services for Google, GitHub, Microsoft, okta and multi-factor authentication using OTP.
npm install login-auth-servicessync-db command
sh
npm install login-auth-services
`
or using yarn:
`sh
yarn add login-auth-services
`
---
š TypeORM & Database Setup
This package uses TypeORM for database interactions. You can use any relational database (MySQL, PostgreSQL, SQLite, etc.).
$3
Create a database.config.ts file to configure TypeORM's DataSource:
`ts
import { DataSource } from "typeorm";
import dotenv from "dotenv";
dotenv.config();
export const AppDataSource = new DataSource({
type: process.env.DATABASE_TYPE as any,
host: process.env.DATABASE_HOST,
port: Number(process.env.DATABASE_PORT),
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: false, // Use sync-db instead
logging: false,
entities: ["src/entity/*.ts"],
});
AppDataSource.initialize()
.then(() => console.log("Database connected"))
.catch((err) => console.error("Database connection failed", err));
`
$3
Entities define the database structure. Example User entity:
`ts
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
email: string;
@Column()
password: string;
}
`
---
š Sync Database
This package includes a sync-db command to synchronize your database schema with TypeORM entities.
$3
Run the following command to apply migrations and synchronize the database:
`sh
npx sync-db
`
This will automatically update the database schema based on TypeORM entities. Ensure synchronize is disabled in database.config.ts to avoid conflicts.
---
š Authentication Usage
$3
`ts
import { GoogleAuthService } from "login-auth-services";
const googleAuth = new GoogleAuthService({
clientId: "your-client-id",
clientSecret: "your-client-secret",
redirectUri: "your-redirect-uri",
});
app.get("/auth/google", async (req, res) => {
const url = googleAuth.getAuthUrl();
res.redirect(url);
});
app.get("/auth/google/callback", async (req, res) => {
const token = await googleAuth.getAccessToken(req.query.code);
res.json(token);
});
`
$3
`ts
import { GitHubAuthService } from "login-auth-services";
const githubAuth = new GitHubAuthService({
clientId: "your-client-id",
clientSecret: "your-client-secret",
redirectUri: "your-redirect-uri",
});
app.get("/auth/github", async (req, res) => {
const url = githubAuth.getAuthUrl();
res.redirect(url);
});
app.get("/auth/github/callback", async (req, res) => {
const token = await githubAuth.getAccessToken(req.query.code);
res.json(token);
});
`
---
š§ Email Verification & Password Reset
$3
`ts
import { sendVerificationEmail } from "login-auth-services";
await sendVerificationEmail(user.email, user.verificationCode);
`
---
š Custom Login Authentication
$3
`ts
import { registerUser } from "login-auth-services";
const user = await registerUser({
email: "user@example.com",
password: "securepassword",
});
`
$3
`ts
import { loginUser } from "login-auth-services";
const token = await loginUser({
email: "user@example.com",
password: "securepassword",
});
`
$3
`ts
import { verifyOTP } from "login-auth-services";
const isValid = await verifyOTP({
email: "user@example.com",
otp: "123456",
});
``