Update: Remove logins + message Error changed
This commit is contained in:
parent
0191b6abd1
commit
419e3c1aa9
5 changed files with 42 additions and 13 deletions
|
|
@ -1,11 +1,10 @@
|
|||
from flask import Flask
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from ddtrace import patch_all
|
||||
import sys
|
||||
import os
|
||||
|
||||
db = SQLAlchemy()
|
||||
#patch_all()
|
||||
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__, instance_relative_config=False)
|
||||
|
|
@ -29,5 +28,4 @@ def create_app():
|
|||
with app.app_context():
|
||||
from . import routes
|
||||
db.create_all()
|
||||
|
||||
return app
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ def db_login(ip, email, password):
|
|||
Users.email == email
|
||||
).first()
|
||||
if not user:
|
||||
message = f'{email} does not exist.'
|
||||
message = f'Email or password invalid'
|
||||
log = Logs(
|
||||
date=datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
|
||||
id_user=None,
|
||||
|
|
@ -70,12 +70,12 @@ def db_login(ip, email, password):
|
|||
return {'status': 0, 'message': message, 'data': user.json()}
|
||||
|
||||
|
||||
def db_register(ip, email, login, password, is_admin):
|
||||
def db_register(ip, email, password, is_admin):
|
||||
user = Users.query.filter(
|
||||
Users.email == email
|
||||
).first()
|
||||
if user:
|
||||
message = f'{email} ({login}) already exist.'
|
||||
message = f'{email} already exist.'
|
||||
log = Logs(
|
||||
date=datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
|
||||
id_user=None,
|
||||
|
|
@ -97,7 +97,6 @@ def db_register(ip, email, login, password, is_admin):
|
|||
|
||||
user = Users(
|
||||
email=email,
|
||||
login=login,
|
||||
hash_pass=hash_pass,
|
||||
salt=salt,
|
||||
is_admin=is_admin
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ def send_error(status_code, message):
|
|||
'message': message
|
||||
}
|
||||
res = app.response_class(
|
||||
response=json.dumps(data_json, sort_keys=True),
|
||||
response=json.dumps(data_json),
|
||||
status=status_code,
|
||||
mimetype='application/json'
|
||||
)
|
||||
|
|
@ -23,7 +23,7 @@ def send_message(message, data):
|
|||
'data': data
|
||||
}
|
||||
res = app.response_class(
|
||||
response=json.dumps(data_json, sort_keys=True),
|
||||
response=json.dumps(data_json),
|
||||
status=200,
|
||||
mimetype='application/json'
|
||||
)
|
||||
|
|
|
|||
|
|
@ -29,19 +29,18 @@ def login():
|
|||
def register():
|
||||
post_json = request.json
|
||||
post_email = str(post_json['email'])
|
||||
post_login = str(post_json['login'])
|
||||
post_password = str(post_json['password'])
|
||||
post_is_admin = bool(post_json['is_admin'])
|
||||
|
||||
if post_email and post_login and post_password and post_is_admin:
|
||||
if post_email and post_password and post_is_admin:
|
||||
ip = request.remote_addr
|
||||
res = db_register(ip, post_email, post_login, post_password, post_is_admin)
|
||||
res = db_register(ip, post_email, post_password, post_is_admin)
|
||||
if res['status'] == 1:
|
||||
return send_error(500, res['message'])
|
||||
elif res['status'] == 0:
|
||||
return send_message(res['message'], res['data'])
|
||||
else:
|
||||
return send_error(400, 'POST Request Error : Need email, login, password and is_admin fields.')
|
||||
return send_error(400, 'POST Request Error : Need email, password and is_admin fields.')
|
||||
|
||||
|
||||
# Logout
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
from datetime import datetime, timedelta
|
||||
from flask import current_app as app
|
||||
import jwt
|
||||
|
||||
from . import db
|
||||
|
||||
|
||||
|
|
@ -37,3 +41,32 @@ class Users(db.Model):
|
|||
|
||||
def get_salt(self):
|
||||
return self.salt
|
||||
|
||||
def auth_token(self):
|
||||
try:
|
||||
time = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
|
||||
payload = {
|
||||
'exp': time + timedelta(days=0, seconds=5),
|
||||
'iat': time,
|
||||
'user': self.json()
|
||||
}
|
||||
return jwt.encode(
|
||||
payload,
|
||||
app.config.get('SECRET_KEY'),
|
||||
algorithm='HS256'
|
||||
)
|
||||
except Exception as e:
|
||||
return e
|
||||
|
||||
@staticmethod
|
||||
def decode_auth_token(auth_token):
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
auth_token,
|
||||
app.config.get('SECRET_KEY')
|
||||
)
|
||||
return payload['user']
|
||||
except jwt.ExpiredSignatureError:
|
||||
return 'Signature expired . Please log in again.'
|
||||
except jwt.InvalidTokenError:
|
||||
return 'Invalid token. Please log in again.'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue