DEV Community

Cover image for Building a Clean Flask Login System with MongoDB, Sessions, and Password Reset
Ritik Barnwal
Ritik Barnwal

Posted on

Building a Clean Flask Login System with MongoDB, Sessions, and Password Reset

Flask + MongoDB has always been a reliable combo for quick backend systems. I recently rebuilt a login system from scratch that includes user registration, session-based login, password hashing, and even a simple password reset flow โ€” all connected to MongoDB.

Just sharing the breakdown in case someoneโ€™s looking to structure a similar system. ๐Ÿ‘‡


๐Ÿ”ง Stack Overview

  • Flask for the backend framework
  • MongoDB as the database
  • pymongo for DB interaction
  • bcrypt for secure password hashing
  • Flask sessions to manage user states
  • dotenv for secure config handling
  • Basic HTML + Jinja2 for templating

๐Ÿ“ Project Structure

login_db/
โ”œโ”€โ”€ app.py
โ”œโ”€โ”€ .env
โ”œโ”€โ”€ templates/
โ”‚ โ”œโ”€โ”€ login.html
โ”‚ โ”œโ”€โ”€ register.html
โ”‚ โ”œโ”€โ”€ forgot.html
โ”‚ โ”œโ”€โ”€ reset.html
โ”‚ โ””โ”€โ”€ dashboard.html


โœจ Key Features

๐Ÿ” Registration

New users register with a username and password, which gets hashed and stored securely in MongoDB.

๐Ÿ”‘ Login

Checks credentials and sets a session. Redirects to a simple dashboard once authenticated.

๐Ÿ”ƒ Forgot + Reset Password

Users can reset their password if they forget it โ€” handled with basic username matching (no email flow yet, keeping it simple).

๐Ÿ”“ Logout

Clears the session and returns the user to the login screen.


๐Ÿ’ก Why I Built This

While there are plenty of boilerplates floating around, I wanted a clean, minimal system where I could control the logic fully โ€” especially for integrating into larger apps or connecting with CI/CD flows later on.

Also, itโ€™s a great exercise in:

  • Session handling in Flask
  • Securing credentials with hashing
  • Managing stateful pages without overcomplication

๐Ÿง  Some Observations

  • Storing sensitive info (like Mongo URI) in .env helps a lot when switching environments.
  • bcrypt integration is straightforward โ€” just donโ€™t forget to decode bytes if youโ€™re storing the hash.
  • Flaskโ€™s session object makes login flows ridiculously easy to manage, even for multi-page flows.
  • Keeping routes modular (e.g., separate login, register, forgot, reset) avoids messy conditionals in single routes.

๐Ÿš€ What's Next

Iโ€™m considering:

  • Adding an email flow with token-based reset
  • Dockerizing the entire app
  • Hooking it up with a CI/CD pipeline (maybe via GitHub Actions)
  • Deployment to Railway or Render for testing

๐Ÿงช GitHub Repo

Check out the full code here:

๐Ÿ”— github.com/theritikbarnwal/DevOps/tree/main/login_db

Feedback/suggestions welcome if youโ€™ve worked on similar systems or see ways to improve it.


โœŒ๏ธ Wrapping Up

This login system isnโ€™t meant to be fancy โ€” itโ€™s designed to be readable, modular, and practical. If you're working on something similar or integrating authentication into your own Flask project, feel free to fork or adapt.

Always happy to nerd out over backend flows and CI/CD pipelines โ€” let me know what youโ€™re working on too ๐Ÿ‘จโ€๐Ÿ’ป


Top comments (1)

Collapse
 
vidakhoshpey22 profile image
Vida Khoshpey

So great
I never understood the difference between these types of databases. MongoDB or MySQL Or sqlite anyway it's so great