is-auth-service
v1.2.11
Published
google login, local login, storing users, and all user and login services
Downloads
890
Maintainers
Readme
Auth Service Package
An Auth service built using Node.js, TypeScript, PostgreSQL, Passport.js (Google Strategy and LocalStrategy), JWT for token management.
Features
- Google OAuth2 Authentication: Securely authenticate users via their Google accounts.- Generate JWT tokens for authenticated users for secure API access.
- Store user data and authentication details securely.
- Utilize TypeScript for type safety and better code maintainability.
- Write unit and integration tests to ensure code reliability.
Installation
- Install
npm install is-auth-service
Usage
- Setup
- app.js
import { AUTH_ROUTES, setAuthConfig, 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 },
})
);
// google passport
passportInit({ app, googleConfig: {
googleClientId: 'YOUR_GOOGLE_CLIENT_ID',
googleSecret: 'YOUR_GOOGLE_SECRET',
callbackUrl: 'https://CLIENT_URL',
}});
// or local stategy
passportInit({ app });
// Register routers
app.use((req: any, res, next) => {
req.jwtSecret = jwtSecret;
req.redirectUrl = process.env.CLIENT_URL ?? 'http://localhost:3000'
next();
}, 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(
'/',
(req, res, next) => {
req.jwtSecret = 'jwtSecret';
next();
},
authenticateJWT,
addFunction
);
- Auth Routers
// app.js
import express from 'express';
import { authRouter } from 'is-auth-service';
const app = express();
app.use(authRouter);
- Access to the routes
- Google login: GET https://YOUR_SERVER_URL/google
- Local login: GET https://YOUR_SERVER_URL/local/login
- Generate jwt: POST https://YOUR_SERVER_URL/generate-token
- Get User: GET https://YOUR_SERVER_URL/user/:id