Motivation
Angular is a powerful frontend framework for building sophisticated Single-Page Applications (SPA). Flask can be used to serve SPAs very easily as explained here. However, incoorporating common security measures that are used in SPAs needs extra effort and therefore, additional extensions are required. A very common extension is flask-security which comes with a lot of functionality for handling authentication, authorization, CSRF protection and much more.
However, leveraging flask-security to handle SPAs is a bit tricky, since flask-security is more focused on forms and contains a rich variety of adjustments that have to get right during initialization. Therefore I want to show which adjustments must be made in order to create a secure and modern SPA using Flask and Angular.
Example application
We have implemented a simple demo application that allows bloggers to write and view news articles. User can register, log in to their account and log out. Similar to GitHub, we want to allow logging in either by username or password.
The frontend and backend logic are separated properly. The frontend code consists of HTML and JavaScript code that could be served independently from the backend code (e.g. by Nginx). The backend is a Flask REST API that only serves as data interface to the database.
The Angular login form look like this: