-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from deepraj21/templates
Adding Flask templates with user authentication
- Loading branch information
Showing
32 changed files
with
3,297 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MONGO_URI=mongodb://localhost:27017/mydatabase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Flask + MongoDB Authentication System | ||
|
||
This is a simple authentication system built using Flask and MongoDB. It includes user registration, login, and session management functionalities. The application is designed to be straightforward and easy to set up, making it suitable for small projects or as a foundation for more complex systems. | ||
|
||
## Features | ||
|
||
- **User Registration**: Allows new users to register with a unique username and password. | ||
- **User Login**: Authenticates users with their username and password. | ||
- **Dashboard**: Displays a personalized dashboard after successful login. | ||
- **Session Management**: Utilizes Flask sessions to manage user login states. | ||
- **User Logout**: Allows users to log out and clear their session. | ||
|
||
## Technologies Used | ||
|
||
- **Flask**: A lightweight WSGI web application framework in Python. | ||
- **Flask-PyMongo**: A Flask extension that simplifies using MongoDB in Flask applications. | ||
- **MongoDB**: A NoSQL database known for its flexibility and scalability. | ||
|
||
## Installation | ||
|
||
1. **Create a virtual environment and activate it**: | ||
|
||
```bash | ||
python3 -m venv venv | ||
./venv/Scripts/activate # On Windows use `venv\Scripts\activate` | ||
``` | ||
|
||
2. **Install the required dependencies**: | ||
|
||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
3. **Ensure MongoDB is running**: | ||
- Make sure your MongoDB server is running locally or accessible remotely. | ||
|
||
4. **Run the application**: | ||
|
||
```bash | ||
python app.py | ||
``` | ||
|
||
5. **Access the application**: | ||
|
||
Visit `http://127.0.0.1:5000/` in your web browser. | ||
|
||
## Routes and Functionalities | ||
|
||
- **`/register` [GET, POST]**: | ||
- **GET**: Renders the registration page where new users can sign up. | ||
- **POST**: Handles the form submission for user registration. If the username is already taken, it flashes an error message. Otherwise, it creates a new user and redirects to the login page with a success message. | ||
|
||
- **`/login` [GET, POST]**: | ||
- **GET**: Renders the login page where users can log in with their credentials. | ||
- **POST**: Handles the login form submission. If the credentials are correct, it logs the user in by storing their user ID in the session and redirects to the dashboard. If the credentials are incorrect, it flashes an error message. | ||
|
||
- **`/dashboard` [GET]**: | ||
- Displays the dashboard page if the user is logged in. If not, it redirects to the login page. | ||
|
||
- **`/logout` [GET]**: | ||
- Logs the user out by clearing their session and redirects them to the home page. | ||
|
||
- **`/` [GET]**: | ||
- Renders the homepage of the application. | ||
|
||
## Flash Messages | ||
|
||
The application uses flash messages to communicate the following events to the user: | ||
|
||
- **Registration**: | ||
- **Success**: "Registration successful. Please log in." | ||
- **Error**: "Username already exists. Please choose a different one." | ||
|
||
- **Login**: | ||
- **Error**: "Invalid username or password. Please try again." | ||
|
||
These messages are displayed on the frontend in the registration and login pages. | ||
|
||
## Database | ||
|
||
The application uses MongoDB for storing user information. The `users` collection in the MongoDB database contains the following fields for each user: | ||
|
||
- **_id**: ObjectId, the unique identifier for each document (user). | ||
- **username**: String, unique, cannot be null. | ||
- **password**: String, hashed, cannot be null. | ||
|
||
--- | ||
|
||
Made using [Universal-Box](https://github.com/Abhishek-Mallick/universal-box) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from flask import render_template, request, redirect, url_for, session, flash, Flask | ||
from flask_pymongo import PyMongo | ||
from bson.objectid import ObjectId | ||
from werkzeug.security import generate_password_hash, check_password_hash | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
app = Flask(__name__) | ||
app.secret_key = 'MYSECRETKEY' | ||
app.config['MONGO_URI'] = os.getenv('MONGO_URI') | ||
mongo = PyMongo(app) | ||
|
||
# User collection in MongoDB | ||
users_collection = mongo.db.users | ||
|
||
@app.route('/register', methods=['GET', 'POST']) | ||
def register(): | ||
if request.method == 'POST': | ||
username = request.form['username'] | ||
password = request.form['password'] | ||
|
||
# Check if the username already exists | ||
user_exists = users_collection.find_one({'username': username}) | ||
if user_exists: | ||
flash('Username already exists. Please choose a different one.', 'error') | ||
return redirect(url_for('register')) | ||
|
||
# Hash the password before storing it | ||
hashed_password = generate_password_hash(password) | ||
|
||
# Insert the new user into the MongoDB collection | ||
users_collection.insert_one({'username': username, 'password': hashed_password}) | ||
flash('Registration successful. Please log in.', 'success') | ||
return redirect(url_for('login')) | ||
|
||
return render_template('register.html') | ||
|
||
@app.route('/login', methods=['GET', 'POST']) | ||
def login(): | ||
if request.method == 'POST': | ||
username = request.form['username'] | ||
password = request.form['password'] | ||
|
||
# Find the user by username | ||
user = users_collection.find_one({'username': username}) | ||
|
||
if user and check_password_hash(user['password'], password): | ||
session['user_id'] = str(user['_id']) | ||
return redirect(url_for('dashboard')) | ||
else: | ||
flash('Invalid username or password. Please try again.', 'error') | ||
return redirect(url_for('login')) | ||
|
||
return render_template('login.html') | ||
|
||
@app.route('/', methods=['GET', 'POST']) | ||
def index(): | ||
return render_template('index.html') | ||
|
||
@app.route('/dashboard', methods=['GET', 'POST']) | ||
def dashboard(): | ||
if 'user_id' in session: | ||
user = users_collection.find_one({'_id': ObjectId(session['user_id'])}) | ||
if user: | ||
username = user['username'] | ||
return render_template('dashboard.html', username=username) | ||
return redirect(url_for('login')) | ||
|
||
@app.route('/logout') | ||
def logout(): | ||
session.pop('user_id', None) | ||
return redirect(url_for('index')) | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |
Oops, something went wrong.