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

@ -10,33 +10,42 @@ 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: if not user:
try: user = User.query.filter_by(username=data.get('username')).first()
print('getting here 1') if not user:
user = User( try:
email = data['email'], print('getting here 1')
password = data['password'], print(data)
) user = User(
print('getting here 2') username = data['username'],
db.session.add(user) email = data['email'],
print('wtf') password = data['password'],
db.session.commit() )
print('user') print('getting here 2')
auth_token = user.encode_auth_token(user.id) db.session.add(user)
print('getting here 4') print('wtf')
response = { db.session.commit()
'status': 'success', print('user')
'message': 'Succesfully registered.', auth_token = user.encode_auth_token(user.id)
'auth_token': auth_token.decode() print('getting here 4')
} response = {
return jsonify(response), 201 'status': 'success',
except Exception as e: 'message': 'Succesfully registered.',
print(e.__dict__) 'auth_token': auth_token.decode()
}
return jsonify(response), 201
except Exception as e:
print(e.__dict__)
response = {
'status': 'fail',
'message': 'There was an error. Please try again.'
}
return jsonify(response), 401
else: # username is taken
response = { response = {
'status': 'fail', 'status': 'fail',
'message': 'There was an error. Please try again.' 'message': 'User already exists. Please login.'
} }
return jsonify(response), 401 else: # email is taken
else:
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)