npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

is-auth-service

v1.2.26

Published

google login, local login, storing users, and all user and login services

Downloads

70

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

  1. Install
npm install is-auth-service

Usage

  1. 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);
  1. 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);
  1. 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"
}'
  1. Required Frontend pages
  • Google redirect: https://CLIENT_URL/auth/callback?token={JWT_TOKEN}