In this article, I would walk through how to use one of the MongoDB python modules – PyMongo to build a Flask user login system. The integration and development logic is similar to the one using the SQLALCHEMY database, however, there are some configuration and setting details that are unique and different from using MongoDB.
If you like to use the non-SQL database to build your Flask user login system and manage your customer database, this piece would be your cup of tea.
Table of Contents: Flask User Login System using PyMongo
- Replace SQLALCHEMY with MongoDB user system logic
- PyMongo Configuration in Flask Application
- Create a User Class
- Sign-up, Login, Profile Page
- Full Python Script of Flask User Login System Using PyMongo
- FAQ
Replace SQLALCHEMY with MongoDB user system logic
In Chapter 57, I shared the logic of the user login system using sqlalchemy. Essentially, the logic can be applied to this piece by using MongoDB and PyMongo Module. That includes the Flask modules needed, such as login manager, current_user, etc. For more details regardin. g the fundamental logic, please refer to this article.
MongoDB is a JSON-based non-SQL database platform. The way to call and respond to data is different with SQL-base. However, it’s much more resilient and easier to handle the data call and response than SQL from my perspective.
The way I use this flask user login system is the same as the Python tutorial 66. For instructions on how to call the data stored in MongoDB, please refer to this article.
PyMongo Configuration in Flask Application
Different from using PyMongo and MongoClient previously in Python Tutorial 66, we need to set up a configuration with the Flask App when using the flask–PyMongo module. Below are the code samples FYI
Step 1: Install the module in the terminal and import the module
Step 2: Configuration and Integration
Create a User Class using UserMixin
If using the SQLALCHEMEY database, this section setting is created by default and we don’t need to add any codes. Nevertheless, using MongoDB which is the 3rd party database platform required setting these up. The purpose of this section is to control the user login and logout status after they sign up or log in. It can automatically detect the user’s identity and you can show different content or service to this user.
Sign-up, Login, Profile Page
As mentioned earlier, the logic of the flask user login system is similar to the one using SQLALCHEMEY. Here shows the difference when using MongoDB.
Sign-up page
Calling the specific user data to verify whether the user existed is different because MongoDB is in JSON-base format. Here are the code samples.
Once detecting this is a new user, we need to add the User class created above by adding the new user sign-up information in the class positional argument.
Additionally, I will suggest adding the id by using the UUID module instead of using the default MongoDB id. It’s because the format is more straightforward and easier to fetch afterward.
Login Page
In MongoDB, the find_one method by using one of the data hints to call the user the full dataset. Moreover, the data format is JSON. So for the user password verification, we can call the hashed password in the MongoDB database by adding [‘password’] after the user variable
Profile Page
On the profile page, we need to feed the customer-related information to the front-end HTML page. We created a get_id() in the User class and it includes the user id we used UUID to create in the sign-up section. The Current_user module can automatically detect the logged-in user. So for fetch. ing the user data and displaying it on the profile page, we can use these codes in the render_template.
Full Python Script of Flask User Login System Using PyMongo
- If you are interested in getting the full version of the script and vibe coding prompt mentioned in this article, please subscribe to our Youtube Channel and comment “flask” in the following video (Flask Login System Script and Vibe Coding Prompt)
- Support our channel through PayPal (paypal.me/Easy2digital)
- Follow our Facebook page
- Sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software