Flask+MongoDB
Introduction
The Flask + MongoDB Authentication System is a streamlined authentication solution crafted using Flask and MongoDB. This template is designed to facilitate user registration, login, and session management, making it an ideal starting point for developers looking to implement authentication in their applications.
Features
- User Registration: Enables new users to register with unique credentials.
- User Login: Authenticates users via username and password.
- Dashboard: Provides a personalized dashboard accessible post-login.
- Session Management: Utilizes Flask's session handling for managing user states.
- User Logout: Allows users to log out, effectively clearing their session.
Technologies Used
- Flask: A lightweight WSGI web application framework for Python.
- Flask-PyMongo: A Flask extension that simplifies MongoDB integration.
- MongoDB: A flexible NoSQL database known for its scalability.
Installation Steps
To set up the Flask + MongoDB Authentication System with Universal-Box, follow these steps:
- Create a virtual environment and activate it:
- Install the required dependencies:
- Ensure MongoDB is running:
Confirm that your MongoDB server is operational locally or accessible remotely
- Set up the environment variables:
Create a
.env
file in the root directory and add the following: - Run the Flask application:
Start the Flask server with:
- Access the application:
Open your browser and navigate to
http://127.0.0.1:5000/
to access the login page.
Routes and Functionalities
User Registration
/register
GET, POST:- GET: Renders the registration form.
- POST: Processes registration submissions, checks for existing usernames, and creates new user entries.
User Login
/login
GET, POST:- GET: Displays the login form.
- POST: Validates user credentials and manages session creation.
Dashboard Access
/dashboard
GET:- Displays the user dashboard if logged in; otherwise redirects to the login page.
User Logout
/logout
GET:- Clears the user session and redirects to the homepage.
Homepage
/
GET:- Renders the main homepage of the application.
Flash Messages
The application utilizes flash messages to inform users about various events:
- Registration Success: "Registration successful. Please log in."
- Registration Error: "Username already exists. Please choose a different one."
- Login Error: "Invalid username or password. Please try again."
Screenshots
Database Structure
The application employs MongoDB for user data storage, specifically within a users
collection that includes:
Field | Type | Description |
---|---|---|
_id | ObjectId | Unique identifier for each user |
username | String | Unique username for each user |
password | String | Hashed password for security |
This template was developed using Universal-Box.