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():
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)

View file

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

View file

@ -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)