All Projects → philuchansky → react-express-jwt

philuchansky / react-express-jwt

Licence: other
Example NodeJS, Express, Mongoose, React app with JWT auth for beginners WITHOUT redux.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to react-express-jwt

mongoose-to-swagger
Conversion library for transforming Mongoose schema objects into Swagger schema definitions.
Stars: ✭ 50 (-1.96%)
Mutual labels:  mongoose
docker-node-express-boilerplate
Boilerplate for quickly bootstrapping production-ready RESTful APIs / microservices
Stars: ✭ 113 (+121.57%)
Mutual labels:  mongoose
api
docs.nekos.moe/
Stars: ✭ 31 (-39.22%)
Mutual labels:  mongoose
express-mongodb-crud
Web Application CRUD using Nodejs and Mongodb
Stars: ✭ 87 (+70.59%)
Mutual labels:  mongoose
react-graphql
react-graphql 快速开发方案
Stars: ✭ 15 (-70.59%)
Mutual labels:  mongoose
E-Commerce-Website-Using-NodeJS
🌟 💻 ᴛʜɪꜱ ɪꜱ ᴀ ᴍᴏɴᴏʟɪᴛʜɪᴄ ᴀᴘᴘʟɪᴄᴀᴛɪᴏɴ ᴜꜱɪɴɢ ɴᴏᴅᴇ ᴊꜱ ᴀꜱ ᴀ ʙᴀᴄᴋᴇɴᴅ ᴛᴇᴄʜɴᴏʟᴏɢʏ 🎯 🚀
Stars: ✭ 12 (-76.47%)
Mutual labels:  mongoose
nestjs-rest-sample
NestJS RESTful APIs Sample
Stars: ✭ 204 (+300%)
Mutual labels:  mongoose
express-boilerplate
ExpressJS boilerplate with Socket.IO, Mongoose for scalable projects.
Stars: ✭ 83 (+62.75%)
Mutual labels:  mongoose
mern-stack-application
A MERN stack e-commerce website.
Stars: ✭ 45 (-11.76%)
Mutual labels:  mongoose
node-fs
node-fs
Stars: ✭ 55 (+7.84%)
Mutual labels:  mongoose
reversevoice
倒放挑战 - ReverseVoice Ts Node Taro
Stars: ✭ 24 (-52.94%)
Mutual labels:  mongoose
IRC-Server
IRC server based on TCP/IP protocol to rfc1459 standard
Stars: ✭ 27 (-47.06%)
Mutual labels:  client-server
Vaser
Vaser is a powerful high performance event based network engine library for C# .Net. It’s possible to start multiple servers in one program and use the same network code for all servers. In the network communication are all strings are omitted, instead it is based on a unique binary identifier, which the CPU and memory relieves massively.
Stars: ✭ 23 (-54.9%)
Mutual labels:  client-server
ShiTTYchat
A bare-bones terminal chat room.
Stars: ✭ 24 (-52.94%)
Mutual labels:  client-server
dhiwise-nodejs
DhiWise Node.js API generator allows you to instantly generate secure REST APIs. Just supply your database schema to DhiWise, and a fully documented CRUD APIs will be ready for consumption in a few simple clicks. The generated code is clean, scalable, and customizable.
Stars: ✭ 224 (+339.22%)
Mutual labels:  mongoose
react-nodejs-mongodb-crud
👨‍💻 Fullstack web app built with MongoDB, NodeJs, React and Redux. Features: Protected routes client/server side, MaterialUI layout
Stars: ✭ 91 (+78.43%)
Mutual labels:  mongoose
reddit-api-clone-2
Build a Reddit API clone. We cover models and build out the rest of our routes!
Stars: ✭ 15 (-70.59%)
Mutual labels:  mongoose
node-js-project-structure
No description or website provided.
Stars: ✭ 21 (-58.82%)
Mutual labels:  mongoose
nest-rest-mongo-boilerplate
🍱 backend with nest (typescript), mongoose, and authentication
Stars: ✭ 180 (+252.94%)
Mutual labels:  mongoose
wily
Build Node.js APIs from the command line (Dead Project 😵)
Stars: ✭ 14 (-72.55%)
Mutual labels:  mongoose

React Application with JWT Authentication

Overview

This is an example application that serves an ExpressJS JSON api to a React client application. The React application is configured for a basic JWT authentication flow WITHOUT using redux. Great for those of you that are somewhat familiar with Node, Express, and Mongoose, but want to see an implementation of React + React Router with JWT authentication.

The React client app could easily be restructured to keep current user information in a Redux Store. Give it a shot!

Installation + Development

  1. git clone this repository to your local machine.

  2. run npm install from the cloned repo directory.

  3. create a .env file at the root of the application, adjacent to server.js.

    The only environment variable you have to declare in development is JWT_SECRET

    In the .env file, you can declare the following environment variables: JWT_SECRET, MONGODB_URI, and PORT. For example:

    JWT_SECRET=BOOOOOOOOOOOOOM
    MONGODB_URI=mongodb://localhost/react-express-jwt
    PORT=3001
    
  4. It's recommended that you run the api server on port 3001 while developing locally, as the client app will default to port 3000.

  5. Make sure mongod is running by running… ahem… mongod

  6. From that point you can run the api server either by using nodemon or just running node server.js

  7. Now for the client application. cd client

  8. Install the client app's dependencies with npm install

  9. From the client directory, run npm start to boot up the client application.

  10. $$$ Profit

Usage

It's common to identify the user making an authenticated request on the server side. In this application, the verifyToken middleware (declared in /serverAuth.js) decodes a provided token, and makes sure the request is coming from a valid user. When the user is validated, it is added to the req object as req.user.

Here's an example of how you can access the 'current user' from the server side app, assuming a user is logged in and sending an authenticated request:

const express = require('express')
const mySpecialRouter = new express.Router()

// JWT AUTH MIDDLEWARE:
const { verifyToken }  = require('../serverAuth.js')

const Comment = require('../models/Comment.js')

// all routes declared after this middleware require a token
mySpecialRouter.use(verifyToken)
mySpecialRouter.post("/comments", (req, res) => {
  // since this route succeeds 'verifyToken', it has the current user in req.user
  // so we can easily associate new mongo documents to the current user:
  Comment.create({ ...req.body, user: req.user }, (err, comment) => {
    if(err) return console.log(err)
    res.json({ success: true, message: "Comment created.", comment })
  })
})

module.exports = mySpecialRouter

Technologies

  • React client application business in the front
  • NodeJS + Express + Mongoose party in the back
  • React Router 4.*
  • Milligram CSS so it doesn't look like garbage
  • JSON Web Token authentication flow

Important Notes

  • While the Mongoose user schema enforces email uniqueness, there's no handler for duplicate user emails on the client side. (A user wouldn't know why they couldn't create their account if they came across this scenario).
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].