Coder Social home page Coder Social logo

mahdidoraj / angular-ssr-auth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from manueldois/angular-ssr-auth

0.0 0.0 0.0 523 KB

Demo on implementing SSR with Angular Universe on an app that uses JWT based authorization

TypeScript 79.18% HTML 8.83% SCSS 11.98%

angular-ssr-auth's Introduction

Angular SSR + Auth

The problem

A typical web app that uses Json Web Tokens for authentication stores this JWT on localStorage after user login. This token is appended to requests the app makes to the server, usually in the header 'Authorizarion: Bearer ...'. This way the server knows who is making the request.

Server side rendering responds to route requests (eg. http://yourapp.com/home) with first a fully rendered html page, then followed by the more lenghty JS that makes the app functional, with the goal of showing the user content more rapidly.

A route request, made by your browser, is a plain http/s GET. Anything stored in localStorage is not sent in this first request, and the server doesen't know who is requesting that route.

If the route is private (requires the user to be logged in) it cannot not be rendered on the server, causing that first render to be a login page or home page or a '401 Unauthorized', even though the user is logged in.

The solution ๐Ÿช

Upon login, save a cookie with the user's authentication JWT.

That http GET made by your browser when visiting a webpage does not send the localStorage, but it does send all cookies set for that domain, including the authentication JWT.

Like this, the server knows who is requesting the route, and can render the app accordingly.

Running

npm start

This will start a live dev server on http://localhost:4200. To disable SSR, go to src/server/server.ts and set USE_SSR to false.

To see SSR in action, on chrome: Open dev tools > network > click the dropdown that says Online (top right) and select Fast 3G. Compare the page load with SSR ON and OFF.

You can also see how a naive implementation of SSR behaves by going checking out the MILESTONE: SSR without auth commit. Login, go to /movies and refresh the page. You should see the page flicker as it shows first the homepage and only later the /movies page.

angular-ssr-auth's People

Contributors

manueldois avatar

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.