implement auth with username

This commit is contained in:
Sorrel Bri 2019-10-06 13:44:38 -07:00
parent 257fd6a533
commit 5b17dedcad
3 changed files with 48 additions and 36 deletions

3
app.py
View file

@ -10,7 +10,8 @@ from flask_cors import CORS
def create_app(): def create_app():
app = Flask(__name__) 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['CORS_HEADERS'] = 'Content-Type'
app.config.from_object(DevelopmentConfig) app.config.from_object(DevelopmentConfig)
db.init_app(app) db.init_app(app)

View file

@ -9,10 +9,14 @@ auth = Blueprint('auth', __name__, url_prefix='/auth')
def auth_signup(): def auth_signup():
data = request.get_json() data = request.get_json()
user = User.query.filter_by(email=data.get('email')).first() 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: if not user:
try: try:
print('getting here 1') print('getting here 1')
print(data)
user = User( user = User(
username = data['username'],
email = data['email'], email = data['email'],
password = data['password'], password = data['password'],
) )
@ -36,7 +40,12 @@ def auth_signup():
'message': 'There was an error. Please try again.' 'message': 'There was an error. Please try again.'
} }
return jsonify(response), 401 return jsonify(response), 401
else: else: # username is taken
response = {
'status': 'fail',
'message': 'User already exists. Please login.'
}
else: # email is taken
response = { response = {
'status': 'fail', 'status': 'fail',
'message': 'User already exists. Please login.' 'message': 'User already exists. Please login.'

View file

@ -1,8 +1,10 @@
from database import db, ma from database import db, ma
from marshmallow import fields
from app import bcrypt from app import bcrypt
from configuration import config from configuration import config
import datetime import datetime
import enum import enum
import json
import jwt import jwt
class Ranks(enum.Enum): # with minimal Elo rating class Ranks(enum.Enum): # with minimal Elo rating
@ -58,7 +60,9 @@ class User(db.Model):
elo = db.Column(db.Integer) elo = db.Column(db.Integer)
rank_certainty = db.Column(db.Boolean, nullable=False, default=False) 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.username = username
self.email = email self.email = email
self.password = bcrypt.generate_password_hash( self.password = bcrypt.generate_password_hash(
@ -103,16 +107,14 @@ class User(db.Model):
return 'Invalid token. Please log in again.' return 'Invalid token. Please log in again.'
class UserSchema(ma.ModelSchema): class UserSchema(ma.ModelSchema):
class Meta: id = fields.Int()
fields = ( username = fields.Str()
'id', email = fields.Str()
'username', registered_on = fields.Date()
'email', rank = fields.Str()
'registered_on', rank_certainty = fields.Bool()
'rank', elo = fields.Int()
'rank_certainty',
'elo'
)
user_schema = UserSchema() user_schema = UserSchema()
users_schema = UserSchema(many=True) users_schema = UserSchema(many=True)