Git Product home page Git Product logo

huzaifac137 / secure-auth-no-local-storage Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 172 KB

Secure authentication done using httpOnly Cookie to store the refresh token on server and generating new access token on user requests

JavaScript 35.06% CSS 6.95% TypeScript 56.99% Dockerfile 1.00%
authentication httponly-cookie mern-stack nextjs nodejs reactjs refresh-token secure secure-authentication xss

secure-auth-no-local-storage's Introduction

secure auth no local storage

npm install

Do this in both frontend and backend folders so project can install all required dependencies

.env File

Include your own .env file with the same variable names as in my code to run the code properly

Description

  1. When user is logged in , its info is not stored in local storage thus discarding the beginners friendly auth approach which is not so secure.
  2. A refresh token is stored on the server side when user is logged in , which is longer lived token and access token is sent back to the frontend client for the auth purpose.
  3. Everytime page is refreshed or any new page is visited which requires user login info a request to the backend is made to check if the refresh token exists .
  4. If the refresh token is existing , a new access token is sent back to client containing the logged in info of user and that info is used to authenticate the user.
  5. The best part is that we eliminate the risk of cross-site-scripting ( XSS) .

When not to use it

It is not always an ideal approach because sometimes app is big enough that sending request to server for new token many times may affect the user experience of app as loading time may increase.

Combining both approaches for convinience and security

What you can rather do is when user is logged in first time , access token sent back to the client you can store in in local storage but for short time and only demand new access toen from server using refresh token when the access token time has been expired. You can check that in my another repository which is realtime private chat app

https://github.com/huzaifac137/realtime_private_chat_app

Thanks for paying attention to my work , i really appreciate it!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.