is-auth-service
v1.2.26
Published
google login, local login, storing users, and all user and login services
Downloads
50
Maintainers
Readme
Auth Service Package
The is-auth-service package provides a complete authentication and authorization solution for Node.js applications. It includes JWT middleware, Passport-based local and Google authentication, and a built-in User database with basic CRUD operations.
Features
- JWT Middleware: Easily protect your routes with JSON Web Token (JWT) middleware to secure API endpoints. Supports token generation, verification, and decoding for seamless user sessions.
- Passport Authentication:
- Local Authentication: Enables secure email and password login, with customizable login and error handling.
- Google OAuth: Allows users to log in with their Google account, simplifying user onboarding with OAuth 2.0.
- User Database with CRUD: Provides a basic User model with Create, Read, Update, and Delete (CRUD) operations, making user management straightforward and scalable.
Installation
- Install
npm install is-auth-service
Usage
- Server Setup
- app.js
import { authRouter, AuthConfig, passportInit } from 'is-auth-service';
import express from 'express';
const app = express();
app.use(
session({
secret: 'YOUR_SESSION_SECRET',
resave: false,
saveUninitialized: true,
cookie: { secure: false },
})
);
// Initialize AuthConfig
AuthConfig.getInstance(jwtSecret, nodeEnv, redirectUrl);
// Google and Local
passportInit(app, {
googleClientId,
googleSecret,
callbackUrl,
});
// Or Local Only
passportInit(app);
// Register routers
app.use(authRouter);
- Use middleware in router
- protected router
import express from 'express';
import { authenticateJWT } from 'is-auth-service';
import { addFunction } from './controllers/YOUR_ROUTER';
const router = express.Router();
router.post(authenticateJWT, addFunction);
- Avaiable the routes
- Google login: GET https://YOUR_SERVER_URL/google
- Local login: POST https://YOUR_SERVER_URL/local/login
- Local signup: POST https://YOUR_SERVER_URL/local/signup
- Logout: POST https://YOUR_SERVER_URL/logout
- Generate jwt: POST https://YOUR_SERVER_URL/generate-token
- Get User: GET https://YOUR_SERVER_URL/user/:id
- Update User: PUT https://YOUR_SERVER_URL/user/:id
- Add User: POST https://YOUR_SERVER_URL/user
- Remove User: DELETE https://YOUR_SERVER_URL/user/:id
Client Example
// GET Google login client
window.location.href = `${process.env.REACT_APP_AUTH_API_URL}/google`;
// Local login
api.post('/local/login', { username: 'your_email', password: 'your_password' });
// Local signup
api.post('/local/signup', {
email: 'your_email',
password: 'your_password',
username: 'your_username',
});
// Logout
api.post('/logout');
// Get user
api.get('/user/1');
// Update user
api.put('/user/1', { username: 'new_user_name' });
// Add user
api.post('/user', {
username: 'my_username',
password: 'my-pw',
email: '[email protected]',
});
// Delete user
api.delete('/user/1');
Postman example
curl --location '{{SERVER_URL}}generate-token' \
--header 'Content-Type: application/json' \
--data '{
"userId": "1"
}'
- Required Frontend pages
- Google redirect: https://CLIENT_URL/auth/callback?token={JWT_TOKEN}