Skip to content

Commit

Permalink
intial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JasmitaVirani committed Oct 6, 2023
1 parent 948bb5a commit 5e07aa0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ ASSETS_ROOT=/static/assets
# SOCIAL AUTH Github
# GITHUB_ID=YOUR_GITHUB_ID
# GITHUB_SECRET=YOUR_GITHUB_SECRET
SECRET_KEY='63fe3b4881327df6c8d66a114a8cd65c9fb97d37b2f6e04e'
6 changes: 4 additions & 2 deletions apps/authentication/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
from wtforms.validators import Email, DataRequired

import secrets
# login and registration


def generate_new_session_id():
return secrets.token_hex(16)
class LoginForm(FlaskForm):
username = StringField('Username',
id='username_login',
Expand All @@ -29,3 +30,4 @@ class CreateAccountForm(FlaskForm):
password = PasswordField('Password',
id='pwd_create',
validators=[DataRequired()])

20 changes: 15 additions & 5 deletions apps/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from flask_dance.consumer.storage.sqla import OAuthConsumerMixin

from apps import db, login_manager

import secrets
from apps.authentication.util import hash_pass

def generate_new_session_id():
return secrets.token_hex(16)
class Users(db.Model, UserMixin):

__tablename__ = 'users'
Expand All @@ -20,10 +21,10 @@ class Users(db.Model, UserMixin):
username = db.Column(db.String(64), unique=True)
email = db.Column(db.String(64), unique=True)
password = db.Column(db.LargeBinary)

session_id = db.Column(db.String(80), unique=True,index=True)
oauth_github = db.Column(db.String(100), nullable=True)

def __init__(self, **kwargs):
def __init__(self, session_id,**kwargs):
for property, value in kwargs.items():
# depending on whether value is an iterable or not, we must
# unpack it's value (when **kwargs is request.form, some values
Expand All @@ -36,7 +37,16 @@ def __init__(self, **kwargs):
value = hash_pass(value) # we need bytes here (not plain str)

setattr(self, property, value)

self.session_id=session_id
def update_session_id(self, session_id):

existing_user = Users.query.filter_by(session_id=session_id).first()
if existing_user:
new_session_id = generate_new_session_id()
self.update_session_id(new_session_id)
else:
self.session_id = session_id
db.session.commit()
def __repr__(self):
return str(self.username)

Expand Down
14 changes: 9 additions & 5 deletions apps/authentication/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from apps.authentication.models import Users

from apps.authentication.util import verify_pass

import secrets
def generate_new_session_id():
return secrets.token_hex(16)
@blueprint.route('/')
def route_default():
return redirect(url_for('authentication_blueprint.login'))
Expand Down Expand Up @@ -90,24 +92,26 @@ def register():

# Check email exists
user = Users.query.filter_by(email=email).first()

if user:
return render_template('accounts/register.html',
msg='Email already registered',
success=False,
form=create_account_form)

session_id = generate_new_session_id()
# else we can create the user
user = Users(**request.form)
user = Users(session_id=session_id,**request.form)
db.session.add(user)
db.session.commit()

# session['sid'] = user.session_id
# Delete user from session
logout_user()

return render_template('accounts/register.html',
msg='User created successfully.',
success=True,
form=create_account_form)
form=create_account_form,
session_id=session_id)

else:
return render_template('accounts/register.html', form=create_account_form)
Expand Down
27 changes: 27 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

from apps.config import config_dict
from apps import create_app, db
from dotenv import load_dotenv

from flask_sqlalchemy import SQLAlchemy
from flask_session import Session
from datetime import datetime,timedelta
# WARNING: Don't run with debug turned on in production!
DEBUG = (os.getenv('DEBUG', 'False') == 'True')

Expand All @@ -36,6 +40,29 @@
app.logger.info('Page Compression = ' + 'FALSE' if DEBUG else 'TRUE' )
app.logger.info('DBMS = ' + app_config.SQLALCHEMY_DATABASE_URI)
app.logger.info('ASSETS_ROOT = ' + app_config.ASSETS_ROOT )
# Load environment variables from .env file
load_dotenv()

# Access the secret key from the environment
app.secret_key = os.getenv('SECRET_KEY')
app.config['SQLALCHEMY_DATABASE_URI'] = app_config.SQLALCHEMY_DATABASE_URI
db = SQLAlchemy(app)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SESSION_TYPE'] = 'sqlalchemy'
app.config['SESSION_TYPE'] = 'filesystem'
current_dir = os.getcwd()+"/session/storage/"
app.config['SESSION_FILE_DIR'] = current_dir

# Create the session storage directory if it doesn't exist
os.makedirs(app.config['SESSION_FILE_DIR'], exist_ok=True)
app.config['SESSION_SQLALCHEMY'] = db
app.config['SESSION_SQLALCHEMY_TABLE'] = 'sessions'
app.config['SESSION_USE_SIGNER'] = True
app.config['SESSION_PERMANENT'] = False
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=1)
Session(app)
#########################################################################


if __name__ == "__main__":
app.run()
Binary file added session/storage/2029240f6d1128be89ddc32729463129
Binary file not shown.
Binary file added session/storage/843939e98ce52172bce37d6303e49fc0
Binary file not shown.

0 comments on commit 5e07aa0

Please sign in to comment.