Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way for user authentication with angularjs and flask

I'm currently working my way through Web development with flask. I want to build a webapp with flask as backend and angular.js at the frontend. The Json part is straight forward, and my first steps work out well. But now I got stuck with User Authentication. I read a lot but found out, that WTFForms works not as well with angular (without the CSFR Token), so i can not use something like flask-security.

What is a proper way or maybe the best pratice for implementing User authentication with Angular und Flask? (Maybe someone knows a good example app I can learn from by example)

like image 741
bknux Avatar asked Feb 07 '14 18:02

bknux


People also ask

How do you authenticate a Flask API?

To do this, you need to implement an authentication middleware. Middlewares are created in Flask by creating a decorator; a function can have multiple middlewares, and the order matters a lot. You need to add a secret key to your application; this is what you should pass to JWT.

How is Angular authentication done?

When your users need to log in, your Angular application triggers an authentication event, which it handles by redirecting them to a customizable Auth0 login page. Once your users log in successfully, Auth0 redirects them back to your Angular application, returning tokens with their authentication and user information.


1 Answers

I have written several tutorials on RESTful APIs with Flask, all with examples that are ready to use:

http://blog.miguelgrinberg.com/category/REST

The tutorials are:

  • Designing a RESTful API with Python and Flask

    This is a simple RESTful API written in Flask, with authentication.

  • Writing a Javascript REST client

    In this one I implement a Javascript client for the API built in the first tutorial. Here you can see how the client authenticates. I used Knockout instead of Angular for the client, but the principles are the same.

  • Designing a RESTful API using Flask-RESTful

    This one is another way to write the server, using the Flask-RESTful extension to simplify some tasks.

  • RESTful Authentication with Flask

    This is a tutorial specifically dedicated to authentication.

I hope they are useful.

like image 166
Miguel Avatar answered Oct 17 '22 23:10

Miguel