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

auth-typescript-template

v1.0.3

Published

The **auth-typescript-template** is a npm package designed to simplify the setup of authentication routes (login, signup, and logout) in your project. After installation, it creates a separate folder in your project root directory and makes necessary chan

Downloads

6

Readme

auth-typescript-template

The auth-typescript-template is a npm package designed to simplify the setup of authentication routes (login, signup, and logout) in your project. After installation, it creates a separate folder in your project root directory and makes necessary changes to index.js, allowing you to test these routes directly without additional coding.

Process

Important Notes :

  • This npm package is only designed to help you make the Typescript template of Authentication.
  • In future versions it will be available for other tech stacks as well.

Quick Installation :

  • npm i npm i auth-typescript-template
  • npx auth-typescript-template
  • npm install express mongoose jsonwebtoken bcryptjs dotenv cors cookie-parser
  • npm install --save-dev @types/express @types/mongoose @types/jsonwebtoken @types/bcryptjs @types/dotenv @types/cors @types/cookie-parser @types/nodemon
  • Go to .env file in your project(which is generated from the auth-typescript-template) and here you have to give MONGO_URI and JWT_SECRET and PORT.
  • All done 👍. Now run your server.

Docs :

Installation in details

  • It is advised not to create any files for your project initially after npm init -y. This npm package automatically creates an index.ts file to run the auth routes, so installing the package first is recommended.
  • installation cmd :
  npm i auth-typescript-template
  npx auth-typescript-template
  • Now after successful installation of the package you have to install some packages to run your project.
  npm install express mongoose jsonwebtoken bcryptjs dotenv cors cookie-parser nodemon
  npm install --save-dev @types/express @types/mongoose @types/jsonwebtoken @types/bcryptjs @types/dotenv @types/cors @types/cookie-parser @types/nodemon
  • Why these packages are necessary?

express : Express is used to create the web server and handle routing. It provides a simple way to define routes for login, signup, and logout, which are core features of your authentication package.

mongoose : Mongoose is used to interact with MongoDB. It allows for defining schemas and models for users, managing data relationships, and performing database operations, which are crucial for storing and retrieving user information securely.

jsonwebtoken : JSON Web Token (JWT) is used for token-based authentication. It helps in securely transmitting information between parties as a JSON object. This is essential for managing user sessions and ensuring that only authenticated users can access certain routes.

bcryptjs : bcrypt.js is used for hashing and comparing passwords. It ensures that user passwords are stored securely in the database, protecting against potential breaches.

dotenv : dotenv is used to manage environment variables. It allows you to keep sensitive data such as JWT secrets and database URLs out of your codebase, enhancing security and flexibility.

cors : CORS (Cross-Origin Resource Sharing) middleware is used to enable requests from different origins. This is important for your project if you want to allow your frontend application (hosted on a different domain) to communicate with your backend server.

cookie-parser: cookie-parser is used to parse cookies attached to client requests. This is useful for handling session cookies and other cookie-based authentication mechanisms.

nodemon : nodemon is used during development to automatically restart the server when file changes are detected. This enhances productivity by eliminating the need to manually restart the server after every change.

  • Althogh nodemon is not required to run this package but it makes your experiance better.

  • Change the package.json file to use the nodemon.

  "scripts": {
    "dev": "nodemon index.ts",
    "start": "node index.ts",
  },
  "type": "module",

After completing the all installation

  • Your folder structure will look like this.

config : It is basically for connecting mongoDB database.

controllers : It has one file named authController.ts which handles all the Login,Logout and Signup process.

| Function | Parameter | |-----------------|-------------------------------------------------------------------------------------------------------------| | SignUp | RequiredfullName,username,email,password. | | Login | Required username, password| | Logout | Nothing Required|

  • You can modify all the functions as all the code is available. Add variables or parameters as needed, and update the user model and function code accordingly.

  • In updated versions of this package, we will try to give customizations of this parameters.

middleware :The protectRoute middleware function verifies the JWT token from cookies, retrieves the corresponding user from the database, and attaches the user object to the request for protected routes.

models : user models. | Fields | Data types | |-----------------|-------------------------------------------------------------------------------------------------------------| | username | Required String| | fullName | Required String| | password | Required String min length 6| | email | Required String|

routes :The code sets up an Express router with routes for user authentication (/signup, /login, /logout) and a protected route (/me) using the protectRoute middleware to retrieve the authenticated user's details(This /me is in development mode..so you will not get it in your package..but you can add it my yourself..protectRoute middleware is available for your use.).

| Routes | Method | |-----------------|-------------------------------------------------------------------------------------------------------------| | /signup | Post| | /login | Post| | /logout | Post|

utils : The generateTokenAndSetCookie function creates a JWT for a given user ID, sets it as an HTTP-only, same-site cookie with a 15-day expiration on the response, enhancing security against XSS and CSRF attacks.

  • You will also get a .env file (You should use the the .env file which is generated from the auth-package not you previous .env file).

| env | Description | |-----------------|-------------------------------------------------------------------------------------------------------------| | MONGO_URI | Required URI for connecting to your MongoDB database. | | JWT_SECRET | Required contains the secret key used to sign and verify JSON Web Tokens (JWT).| | NODE_ENV | By default Development| | PORT | Required PORT required for running the server|

Now all is set you can run your server by npm run dev and test your server by Postman or other applications.

Created by :

Avik Mukherjee