#16 Added "role" required field for register endpoint
This commit is contained in:
@@ -7,6 +7,7 @@ from werkzeug.security import generate_password_hash, check_password_hash
|
|||||||
class User(UserMixin, db.Model):
|
class User(UserMixin, db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
username = db.Column(db.String(64), index=True, unique=True)
|
username = db.Column(db.String(64), index=True, unique=True)
|
||||||
|
role = db.Column(db.String(32), index=True)
|
||||||
email = db.Column(db.String(120), index=True, unique=True)
|
email = db.Column(db.String(120), index=True, unique=True)
|
||||||
password_hash = db.Column(db.String(128))
|
password_hash = db.Column(db.String(128))
|
||||||
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
|
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
|
||||||
@@ -29,7 +30,7 @@ class User(UserMixin, db.Model):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def from_dict(self, data, new_user=False):
|
def from_dict(self, data, new_user=False):
|
||||||
for field in ["username", "email"]:
|
for field in ["role", "username", "email"]:
|
||||||
if field in data:
|
if field in data:
|
||||||
setattr(self, field, data[field])
|
setattr(self, field, data[field])
|
||||||
if new_user and "password" in data:
|
if new_user and "password" in data:
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ def login_route():
|
|||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|
||||||
if current_user.is_authenticated:
|
if current_user.is_authenticated:
|
||||||
return error_response(400, 'A user is already logged in!')
|
return error_response(400, "A user is already logged in!")
|
||||||
|
|
||||||
if not data.get("user_id") or not data.get("password"):
|
if not data.get("user_id") or not data.get("password"):
|
||||||
return error_response(400, "Must supply user_id and password")
|
return error_response(400, "Must supply user_id and password")
|
||||||
@@ -34,6 +34,7 @@ def login_route():
|
|||||||
resp = jsonify(user.to_dict())
|
resp = jsonify(user.to_dict())
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/logout", methods=["POST"])
|
@bp.route("/logout", methods=["POST"])
|
||||||
def logout_route():
|
def logout_route():
|
||||||
if not current_user.is_authenticated:
|
if not current_user.is_authenticated:
|
||||||
@@ -43,19 +44,20 @@ def logout_route():
|
|||||||
logout_user()
|
logout_user()
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/register", methods=["POST"])
|
@bp.route("/register", methods=["POST"])
|
||||||
def register():
|
def register():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|
||||||
required_fields = ['username', 'email', 'password', 'password2']
|
required_fields = ["role", "username", "email", "password", "password2"]
|
||||||
for f in required_fields:
|
for f in required_fields:
|
||||||
if f not in data:
|
if f not in data:
|
||||||
return error_response(400, f"Must supply {f}")
|
return error_response(400, f"Must supply {f}")
|
||||||
|
|
||||||
if User.query.filter_by(username=data['username']).first():
|
if User.query.filter_by(username=data["username"]).first():
|
||||||
return error_response(409, "User with that username already exists")
|
return error_response(409, "User with that username already exists")
|
||||||
|
|
||||||
if User.query.filter_by(email=data['email']).first():
|
if User.query.filter_by(email=data["email"]).first():
|
||||||
return error_response(409, "User with that email already exists")
|
return error_response(409, "User with that email already exists")
|
||||||
|
|
||||||
u = User()
|
u = User()
|
||||||
|
|||||||
36
backend/migrations/versions/8e48199f1417_.py
Normal file
36
backend/migrations/versions/8e48199f1417_.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 8e48199f1417
|
||||||
|
Revises: 7736bc740f9b
|
||||||
|
Create Date: 2023-03-18 21:18:21.923362
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '8e48199f1417'
|
||||||
|
down_revision = '7736bc740f9b'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('user', schema=None) as batch_op:
|
||||||
|
batch_op.add_column(sa.Column('role', sa.String(length=32), nullable=True))
|
||||||
|
batch_op.create_index(batch_op.f('ix_user_role'), ['role'], unique=False)
|
||||||
|
batch_op.drop_column('about_me')
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('user', schema=None) as batch_op:
|
||||||
|
batch_op.add_column(sa.Column('about_me', sa.VARCHAR(length=140), nullable=True))
|
||||||
|
batch_op.drop_index(batch_op.f('ix_user_role'))
|
||||||
|
batch_op.drop_column('role')
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
Reference in New Issue
Block a user