generated from nyu-software-engineering/web-app-exercise
-
Notifications
You must be signed in to change notification settings - Fork 3
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 #52 from software-students-spring2024/auth
Implement authentication
- Loading branch information
Showing
5 changed files
with
90 additions
and
21 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 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 |
---|---|---|
@@ -1,31 +1,75 @@ | ||
from flask import render_template, request, redirect, url_for, session | ||
from flask_login import LoginManager | ||
from flask import render_template, request, redirect, url_for, session, flash | ||
from flask_login import LoginManager, UserMixin, login_user | ||
from db import * | ||
|
||
from pymongo import * | ||
from werkzeug.security import generate_password_hash, check_password_hash | ||
|
||
login_manager = LoginManager() | ||
|
||
class User(): | ||
def __init__(self, username, password, is_authenticated, is_active, is_anonymous): | ||
self.is_authenticated = is_authenticated | ||
self.is_active = is_active | ||
self.is_anonymous = is_anonymous | ||
''' | ||
NOTE: | ||
To get the active user from a different file or somewhere else | ||
you need to import flask_login first then you can retrieve the active user with current_user | ||
Example: | ||
from flask_login import current_user | ||
if current_user.is_authenticated: | ||
# Do something with current_user | ||
Also check allDecks function in app.py for example usage | ||
''' | ||
# Automatically implements is_authenticated, is_anonymous, is_active and get_id() | ||
# through UserMixin | ||
class User(UserMixin): | ||
def __init__(self, user_id, password): | ||
self.id = user_id | ||
self.password = password | ||
|
||
def verify_password(self, pwd): | ||
return check_password_hash(self.password, pwd) | ||
|
||
|
||
@login_manager.user_loader | ||
def load_user(user_id): | ||
return User.get(user_id) | ||
data = db['users'].find_one({"user_id": user_id}) | ||
if (data): | ||
return User(data['user_id'], data['password']) | ||
else: | ||
return None | ||
|
||
|
||
def authLogin(): | ||
def auth_login(): | ||
collections = db.list_collection_names() | ||
if request.method == 'POST': | ||
## TODO: Login | ||
return "Someone pressed the login button huh, I better log you in." | ||
user_id = request.form["username"] | ||
password = request.form["password"] | ||
user = load_user(user_id) | ||
if user and user.verify_password(password): | ||
# TODO: Redirect to the appropriate page for the logged in user | ||
login_user(user) | ||
return redirect('/') | ||
else: | ||
return render_template('login.html', invalid_login=True) | ||
else: | ||
return render_template('login.html') | ||
return render_template('login.html', invalid_login=False) | ||
|
||
|
||
def authSignup(): | ||
def auth_signup(): | ||
if request.method == 'POST': | ||
username = request.form["username"] | ||
user_id = request.form["username"] | ||
password = request.form["password"] | ||
confirm_password = request.form["confirm-password"] | ||
if load_user(user_id): | ||
return render_template('signup.html', username_taken=True, passwords_dont_match=False) | ||
elif password != confirm_password: | ||
return render_template('signup.html', username_taken=False, passwords_dont_match=True) | ||
else: | ||
user = User(user_id, generate_password_hash(password)) | ||
db['users'].insert_one({"user_id": user.id, "password": user.password}) | ||
login_user(user) | ||
# TODO: Redirect to the appropriate page for the logged in user | ||
return redirect('/') | ||
|
||
else: | ||
return render_template('signup.html') | ||
return render_template('signup.html', username_taken=False, passwords_dont_match=False) | ||
|
||
|
||
|
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 |
---|---|---|
|
@@ -116,3 +116,7 @@ h3 { | |
justify-content: center; | ||
align-items: center; | ||
} | ||
|
||
.signup-error, .login-error { | ||
font-size: 25px; | ||
} |
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 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