2018-05-04 13:44:17 +00:00
|
|
|
from app import bcrypt
|
|
|
|
from flask import request, jsonify
|
|
|
|
from .models import Account
|
|
|
|
|
2018-05-03 14:40:30 +00:00
|
|
|
|
|
|
|
def initialize_routes(accounts):
|
|
|
|
@accounts.route("/", methods=['POST'])
|
2018-05-04 13:44:17 +00:00
|
|
|
def create_account():
|
|
|
|
print(request.data)
|
|
|
|
user = request.data.get('user')
|
|
|
|
if not Account.exists_with_any_of(
|
|
|
|
username=user.get('username'), email=user.get('email')):
|
|
|
|
password_hash = bcrypt.generate_password_hash(
|
|
|
|
user.get('password')
|
|
|
|
).decode('utf-8')
|
|
|
|
acct = Account(user.get('username'),
|
|
|
|
password_hash,
|
|
|
|
user.get('email'))
|
|
|
|
acct.save()
|
|
|
|
response = jsonify({
|
|
|
|
'status': 'success',
|
|
|
|
'message': 'Success!'
|
|
|
|
})
|
|
|
|
response.status_code = 200
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
response = jsonify({
|
|
|
|
'status': 'error',
|
|
|
|
'message': 'User already exists!'
|
|
|
|
})
|
|
|
|
response.status_code = 422
|
|
|
|
return response
|
|
|
|
|
|
|
|
@accounts.route("/token", methods=['POST'])
|
|
|
|
def create_token():
|
2018-05-03 15:01:32 +00:00
|
|
|
print(request.data)
|
|
|
|
user = request.data.get('user')
|
2018-05-04 13:44:17 +00:00
|
|
|
if not user:
|
|
|
|
response = jsonify({
|
|
|
|
'status': 'error',
|
|
|
|
'message': 'Invalid request'
|
|
|
|
})
|
|
|
|
response.status_code = 400
|
|
|
|
return response
|
|
|
|
|
|
|
|
if not Account.exists(username=user.get('username')):
|
|
|
|
response = jsonify({
|
|
|
|
'status': 'error',
|
|
|
|
'message': 'Invalid credentials'
|
|
|
|
})
|
|
|
|
response.status_code = 422
|
|
|
|
return response
|
|
|
|
|
|
|
|
account = Account.get(username=user.get('username'))
|
|
|
|
if not bcrypt.check_password_hash(
|
|
|
|
account.password, user.get('password')):
|
|
|
|
response = jsonify({
|
|
|
|
'status': 'error',
|
|
|
|
'message': 'Invalid credentials'
|
|
|
|
})
|
|
|
|
response.status_code = 422
|
|
|
|
return response
|
|
|
|
|
2018-05-03 15:01:32 +00:00
|
|
|
response = jsonify({
|
2018-05-04 13:44:17 +00:00
|
|
|
'status': 'success',
|
|
|
|
'message': 'Successfully logged in',
|
|
|
|
'token': account.create_auth_token()
|
2018-05-03 15:01:32 +00:00
|
|
|
})
|
|
|
|
response.status_code = 200
|
|
|
|
return response
|