Git Product home page Git Product logo

angularjs-jwt-authentication-tutorial's Introduction

Authentication on AngularJS with JWTs

In this demo we'll show step by step how to add Authentication to your AngularJS app with an API that uses JWTs.

Running the example

This application has an AngularJS Frontend and a NodeJS backend

Frontend

In order to run the the AngularJS app, all you need to do is to start a server at that folder. I recommend using serve for that. Just run:

  • npm install -g serve
  • bower install
  • serve

Then go to http://localhost:3000 and see the Frontend app running

Backend

The backend is made with node, so you must have node and npm installed.

Then, just run node server.js to start the server on the port 3001.

Branches

In this repository you'll find several branches. Each of the branches represents one step taken to implement the Authentication.

1 - Start Branch

This is the starting point for the application.

If you go to http://localhost:3000/#/ you'll see the main page of the demo.

In there, you'll see 2 buttons to do 2 API calls:

  • The first one, does an API call that doesn't require Authentication
  • The second one does an API call that does require authentication

Try clicking both to see what happens.

2 - Authentication implemented in the Backend and UI in the FrontEnd

In this branch, we've added the UI for both the Signup and Login.

Also, we've implemented the Login and Signup on the Backend. It returns the JWT to the user.

3 - User Authentication Finished

In this branch, user Authentication works in the Frontend. You can see the Login and Signup implemented and working.

Go to http://localhost:3000/#/login to see the Login page and login. You'll see that if you click on the Call secure API button, it'll still throw an error. That's because we need to send the JWT on the Authorization header. Let's do that!

4 - Sending JWTs on requests

Now, we've implemented sending the JWT on the Authorization header on every request. Check it out here

Now, before logging in, try going to http://localhost:3000/#/. You should still see this page even though you're not logged in. Try clicking on the Call secure API button and check that you still get the Unauthorized error. Now, go to the Login page and log in. Try clicking again on Call secure API. Check that it's not returning a correct response since the Authorization header is sent in every request.

5 - Restrict access to routes

In this section, we've restricted access to routes that require authentintication for anonymous users. Go to http://localhost:3000/#/ without being logged in and you'll see that you're redirected to the Login page. You can see the implementation for that here.

angularjs-jwt-authentication-tutorial's People

Contributors

johnw424 avatar mgonto avatar paulbhartzog 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.