Skip to content

REST API powered by TypeScript, JsonWebToken for auth and more.

Notifications You must be signed in to change notification settings

AloisCRR/ts-api-users

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TypeScript API with authentication

This project purpose is to learn about JWT auth flow, using TypeScript.

Run Locally

  1. Install both:

    You will need to have MongoDB running on port 27017.

  2. Clone the project:

    git clone https://github.com/AloisCRR/ts-api-users.git
  3. Go to the project directory:

    cd ts-api-users
  4. Install dependencies:

    npm install
  5. Start the dev server:

    npm run dev

    REST API will run in http://localhost:3000.

  6. To compile TypeScript to JavaScript and run the project:

    npm run build && npm start

API Reference

Sign up or register

POST /signup
Body Type Description
email string Required. User email address
password string Required. Account password

Sign in or login

POST /signin
Body Type Description
email string Required. User email address
password string Required. Account password
GET /auth
Headers Type Description
Authentication JWT Required. Jwt given on sign in or sign up

Screenshots

Basic input validation

Screenshot

Invalid password or email

Screenshot

Successful sign in

Screenshot

Sending token on headers

Screenshot

Authorization

Screenshot

Tech Stack

Name Description
Node.js Business logic
MongoDB Database
Express HTTP Server
TypeScript JavaScript super-set to add static code analysis
JWT Library to generate JWTs
Mongoose ODM (Object Data Modeling)
Passport JWT Passport strategy for authenticating with a JSON Web Token.
Bcrypt Algorithm used to hash passwords.

Lessons Learned

Route creation

import { Router } from "express";
import { signIn, signUp } from "../controllers/user.controller";

const router = Router();

router.post("/signup", signUp);
router.post("/signin", signIn);

export default router;

Route controller

router.get(
  "/auth",
  passport.authenticate("jwt", { session: false }),
  (req, res) => {
    res.status(200).json({ msg: "Auth route succeeded" });
  }
);

Create token

function createToken(user: Iuser) {
  return jwt.sign({ id: user.id, email: user.email }, config.jwtSecret, {
    expiresIn: 86400,
  });
}

Works in this way... With JWT obviously you can generate a token for authentication, a token can hold public data in a stateless way. Public info is like the algorithm used to sign token or the type of token, also included something called "payload" which is content or body of token (this includes all data registered for token).

To generate a token we use a function from jwt module called sign, passing a "payload" that is information that token will save, and a secret used to sign the token.

Token is signed by a private key, and with the same key we can check if token is valid and use it to authenticate an user, passport takes his time in this, with passport-jwt we can use a function called passport.authenticate() which is a middleware that handles all the logic from getting the token from auth header to validate it and attach the token payload to the request object of express.

Roadmap

  • App functionality
  • Testing
  • Hosting, domain, etc.
  • CI/CD

About

REST API powered by TypeScript, JsonWebToken for auth and more.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published