As web development becomes more complex, it's important for developers to have a solid understanding of the various tools and technologies available to them. One area that is particularly important is user authentication and login functionality. In this article, we will explore how to implement a login mechanism using the Flask framework and a cloud database. Flask is a lightweight Python web framework that makes it easy to build web applications, and using a cloud database like MongoDB, Oracle Autonomous Database or Firebase allows for easy scalability and accessibility of the login information. We will cover the basics of setting up a Flask application, connecting it to a cloud database, and implementing a secure login mechanism. By the end of this article, you will have a solid understanding of how to use Flask and a cloud database to implement a login system for your web applications.
Credits to these youtube videos which helped me getting startet with the oracle cloud database and user authentication in flask web apps:
https://www.youtube.com/watch?v=3RJ5jMi8YUQ
https://www.youtube.com/watch?v=71EU8gnZqZQ
The technical steps to set up the oracle cloud database are described in the first youtube video. Hence, I will not go through these steps in detail. The first step here is to create a table "User" to store ID, username and password for this simple example. That is to be done in the SQL Developer on your PC:
Once the table is created, the database must be accessed via python. To do so, you need your username, password and DSN ffor your autonomous cloud database. You can the set up a connection using the oracledb.connect() function:
After the connection is set up, the database can be interacted with. For this use case, new data needs to be added to the databe when a user registers. Also, when a user tries to login, data needs to be queried from the database to check wether the user exists and the password is correct. To use some of the built in authentication functionality that comes with flask, a User module needs to be implemented. Particularly important is that the user has the attributes ID, username and password, so that some basic funtionality can be inherited from Flasks UserMixin. The whole User module is displayed below:
Now that the User class is defined, the built in Flask authentication modules need to be connected to the User functions. This mainly involves creating and finding User objects based on the username or user ID. After the Flask app is initialized, the user loader of the login manager needs to be defined. Therefore, a function is required that returns a user object for a respective ID. If no user is found, None needs to be returned. Next, the registration and login forms need to be defined as classes. Both inherit the functionality from FlaskForm. The implementation looks the following:
The last step to complete the connection of Flask authentication with a cloud database is to set up the routing of the web app and some simple HTML sites for the registration, login and logout. therefore, each of these pages is assigned a URL and function in a python file app.py. In the app.py file, the functionality that has been implemented in the previous steps is accessed by importing the two custom modules. The HTML files need to be stored in a folder named "templates" in the project directory, so that they can be found and rendered by the respective function. You can find the HTML templates here. Finally, the app can be run locally on your machine.
Connecting Flask-Login with a cloud database offers numerous benefits for web development. Not only does it provide a secure and efficient way to handle user authentication and authorization, but it also allows for easy scalability and accessibility of user information. By utilizing a cloud database, such as AWS RDS, MongoDB Atlas or Oracle Autonomous Database, developers can easily manage and maintain their user data, while also ensuring that their application can handle a large number of users. Overall, connecting Flask-Login with a cloud database is a practical and valuable solution for any web development project that requires user authentication and authorization.