implement auth with username
This commit is contained in:
parent
257fd6a533
commit
5b17dedcad
3 changed files with 48 additions and 36 deletions
3
app.py
3
app.py
|
@ -10,7 +10,8 @@ from flask_cors import CORS
|
|||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
CORS(app, origins="http://localhost:3004")
|
||||
CORS(app, resources={r"/api/*": {"origins": "http://localhost:3000"},
|
||||
r"/auth/*": {"origins": "http://localhost:3000"}})
|
||||
app.config['CORS_HEADERS'] = 'Content-Type'
|
||||
app.config.from_object(DevelopmentConfig)
|
||||
db.init_app(app)
|
||||
|
|
11
auth/auth.py
11
auth/auth.py
|
@ -9,10 +9,14 @@ auth = Blueprint('auth', __name__, url_prefix='/auth')
|
|||
def auth_signup():
|
||||
data = request.get_json()
|
||||
user = User.query.filter_by(email=data.get('email')).first()
|
||||
if not user:
|
||||
user = User.query.filter_by(username=data.get('username')).first()
|
||||
if not user:
|
||||
try:
|
||||
print('getting here 1')
|
||||
print(data)
|
||||
user = User(
|
||||
username = data['username'],
|
||||
email = data['email'],
|
||||
password = data['password'],
|
||||
)
|
||||
|
@ -36,7 +40,12 @@ def auth_signup():
|
|||
'message': 'There was an error. Please try again.'
|
||||
}
|
||||
return jsonify(response), 401
|
||||
else:
|
||||
else: # username is taken
|
||||
response = {
|
||||
'status': 'fail',
|
||||
'message': 'User already exists. Please login.'
|
||||
}
|
||||
else: # email is taken
|
||||
response = {
|
||||
'status': 'fail',
|
||||
'message': 'User already exists. Please login.'
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
from database import db, ma
|
||||
from marshmallow import fields
|
||||
from app import bcrypt
|
||||
from configuration import config
|
||||
import datetime
|
||||
import enum
|
||||
import json
|
||||
import jwt
|
||||
|
||||
class Ranks(enum.Enum): # with minimal Elo rating
|
||||
|
@ -58,7 +60,9 @@ class User(db.Model):
|
|||
elo = db.Column(db.Integer)
|
||||
rank_certainty = db.Column(db.Boolean, nullable=False, default=False)
|
||||
|
||||
def __init__(self, username, email, password, rank='RU', admin=False):
|
||||
def __init__(self, username, email, password, rank=Ranks.K1, admin=False):
|
||||
print(rank)
|
||||
print(Ranks)
|
||||
self.username = username
|
||||
self.email = email
|
||||
self.password = bcrypt.generate_password_hash(
|
||||
|
@ -103,16 +107,14 @@ class User(db.Model):
|
|||
return 'Invalid token. Please log in again.'
|
||||
|
||||
class UserSchema(ma.ModelSchema):
|
||||
class Meta:
|
||||
fields = (
|
||||
'id',
|
||||
'username',
|
||||
'email',
|
||||
'registered_on',
|
||||
'rank',
|
||||
'rank_certainty',
|
||||
'elo'
|
||||
)
|
||||
id = fields.Int()
|
||||
username = fields.Str()
|
||||
email = fields.Str()
|
||||
registered_on = fields.Date()
|
||||
rank = fields.Str()
|
||||
rank_certainty = fields.Bool()
|
||||
elo = fields.Int()
|
||||
|
||||
|
||||
user_schema = UserSchema()
|
||||
users_schema = UserSchema(many=True)
|
Loading…
Reference in a new issue