Added Registration endpoint and configuration file

email
AndroidDrew 7 years ago
parent 57be81f31a
commit 40951d8354

3
.gitignore vendored

@ -64,3 +64,6 @@ docs/_build/
# Redis
/dump.rdb
# Project
config.py

@ -4,6 +4,7 @@ from apistar import Component, Settings, http, Route, Include
from apistar.backends.sqlalchemy_backend import Session
from apistar_jwt.authentication import get_jwt
from apistar_jwt.token import JWT
from sqlalchemy.exc import IntegrityError, InvalidRequestError
from cookie_api.models import User
@ -46,8 +47,31 @@ def logout():
# TODO Add user registration
def register():
pass
def register(settings: Settings, json_data: http.RequestData, session: Session):
user_id = json_data.get('email')
password = json_data.get('password')
email_check = session.query(User).filter_by(email=user_id).one_or_none()
if email_check is not None:
message = {
'status': 'error',
'message': 'user email address is already in use'
}
return http.Response(message, status=400)
user = User(email=user_id, password=password)
session.add(user)
session.commit()
# TODO Send off an email confirmation
headers = {}
message = {
'status': 'success',
'message': 'Please check your inbox and confirm your email'
}
return http.Response(message, status=200, headers=headers)
# TODO Add user profile endpoint
@ -56,7 +80,7 @@ def user_profile():
# TODO Add email confirmation
def confirm():
def confirm(settings: Settings, json_data: http.RequestData, session: Session):
pass
@ -66,7 +90,8 @@ def reset():
routes = [
Route('/login', 'POST', login)
Route('/login', 'POST', login),
Route('/register', 'POST', register)
]
auth_routes = [Include('/auth', routes)]

@ -5,53 +5,8 @@ except ImportError:
from psycopg2cffi import compat
compat.register()
import os
import sys
from cookie_api import JSONRenderer, Base, application_factory
settings = {
'DATABASE': {
'URL': 'postgresql://apistar:local@localhost/apistar',
'METADATA': Base.metadata
},
'RENDERERS': [JSONRenderer()],
'JWT': {
'SECRET': 'thisisasecret'
},
'LOGGING': {
"LOG_FILE": 'mylogfile.log',
"LEVEL": "DEBUG"
}
}
def prepare_import(path):
"""Given a filename this will try to calculate the python path, add it
to the search path and return the actual module name that is expected.
"""
path = os.path.realpath(path)
if os.path.splitext(path)[1] == '.py':
path = os.path.splitext(path)[0]
if os.path.basename(path) == '__init__':
path = os.path.dirname(path)
module_name = []
# move up until outside package structure (no __init__.py)
while True:
path, name = os.path.split(path)
module_name.append(name)
if not os.path.exists(os.path.join(path, '__init__.py')):
break
if sys.path[0] != path:
sys.path.insert(0, path)
return '.'.join(module_name[::-1])
from cookie_api import application_factory
from config import settings
app = application_factory(settings)

Loading…
Cancel
Save