add auth routes
This commit is contained in:
parent
df04427ea0
commit
75f244ae9f
4 changed files with 50 additions and 0 deletions
0
auth/__init__.py
Normal file
0
auth/__init__.py
Normal file
14
auth/auth.py
Normal file
14
auth/auth.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
from flask import Blueprint, request, jsonify, session
|
||||
from ..models.User import User
|
||||
|
||||
auth = Blueprint('auth', __name__, url_prefix='/auth')
|
||||
|
||||
@api.route('/signup', methods=['POST'])
|
||||
def auth_signup():
|
||||
response = {"message": "signup post"}
|
||||
return jsonify(response)
|
||||
|
||||
@api.route('/login', methods=['POST'])
|
||||
def auth_login():
|
||||
response = {"message": "login post"}
|
||||
return jsonify(response)
|
|
@ -1,6 +1,7 @@
|
|||
from app import db, ma, bcrypt
|
||||
import datetime
|
||||
import enum
|
||||
import jwt
|
||||
|
||||
class Ranks(enum.Enum): # with minimal Elo rating
|
||||
D7 = "Seven Dan" # Elo 2700+
|
||||
|
@ -61,3 +62,37 @@ class User(db.Model):
|
|||
).decode()
|
||||
self.registered_on = datetime.datetime.now()
|
||||
self.admin = admin
|
||||
|
||||
def encode_auth_token(self, user_id):
|
||||
"""
|
||||
Generates the Auth Token
|
||||
:return: string
|
||||
"""
|
||||
try:
|
||||
payload = {
|
||||
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=5),
|
||||
'iat': datetime.datetime.utcnow(),
|
||||
'sub': user_id
|
||||
}
|
||||
return jwt.encode(
|
||||
payload,
|
||||
app.config.get('SECRET_KEY'),
|
||||
algorithm='HS256'
|
||||
)
|
||||
except Exception as e:
|
||||
return e
|
||||
|
||||
@staticmethod
|
||||
def decode_auth_token(auth_token):
|
||||
"""
|
||||
Decodes the auth token
|
||||
:param auth_token:
|
||||
:return: integer|string
|
||||
"""
|
||||
try:
|
||||
payload = jwt.decode(auth_token, app.config.get('SECRET_KEY'))
|
||||
return payload['sub']
|
||||
except jwt.ExpiredSignatureError:
|
||||
return 'Signature expired. Please log in again.'
|
||||
except jwt.InvalidTokenError:
|
||||
return 'Invalid token. Please log in again.'
|
||||
|
|
|
@ -23,6 +23,7 @@ mccabe==0.6.1
|
|||
numpy==1.17.2
|
||||
psycopg2==2.8.3
|
||||
pycparser==2.19
|
||||
PyJWT==1.7.1
|
||||
pylint==2.4.2
|
||||
python-dateutil==2.8.0
|
||||
python-editor==1.0.4
|
||||
|
|
Loading…
Reference in a new issue