Last active
January 12, 2017 20:49
-
-
Save Frankity/e8ea52ccdf4a23194d46233682e1a54f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Blueprint, request, jsonify, make_response | |
from flask_cors import CORS, cross_origin | |
from flask_login import session | |
import hashlib, os, binascii | |
from api import db | |
from api.users.models import Users | |
mod_users = Blueprint('users', __name__, url_prefix='/rest') | |
@mod_users.route('/register', methods=['POST']) | |
def registerUser(): | |
if request.method == 'POST': | |
email = request.values.get('email') | |
nick = request.values.get('nick') | |
password = request.values.get('password') | |
if Users.query.filter_by(email=email).first() is not None: | |
return jsonify({'false' : 'El correo ya esta registrado', 'result': "final"}) | |
elif Users.query.filter_by(nick=nick).first() is not None: | |
return jsonify({'false' : 'El usuario ya esta registrado', 'result': "final"}) | |
else: | |
a_pwd = hashlib.pbkdf2_hmac('sha256', password, b'salt', 100000) | |
b_pwd = binascii.hexlify(a_pwd) | |
token_mail = binascii.hexlify(os.urandom(16)) | |
user = Users(email=email, nick=nick, password=b_pwd) | |
db.session.add(user) | |
db.session.commit() | |
if user: | |
print "user registered" | |
return "true" | |
else: | |
return "false" | |
@mod_users.route('/login', methods=['POST', 'GET']) | |
def loginUser(): | |
if request.method == 'POST': | |
nick = request.values.get('nick') | |
password = request.values.get('password') | |
try: | |
if Users.query.filter_by(nick=nick).first() is None: | |
return jsonify({'Message':'The entered username does not exist.'}) | |
else: | |
try: | |
a_pwd = hashlib.pbkdf2_hmac('sha256', password, b'salt', 100000) | |
b_pwd = binascii.hexlify(a_pwd) | |
query = Users.query.filter_by(nick=nick).first() | |
c_pwd = query.password | |
if b_pwd == c_pwd: | |
session['id'] = query.id | |
return jsonify({'true':'true', | |
'id': query.id, | |
'user':{ | |
'nick': query.nick, | |
'email': query.email, | |
'created_at':query.regdate}}) | |
else: | |
return jsonify({"tag": "login", | |
"success": "false", | |
"error_msg": "Error in login, please check your credentials and try again!" | |
}) | |
except Exception as e: | |
return jsonify({'Message': 'query error ' + str(e)}) | |
print str(e) | |
except Exception as e: | |
return jsonify({'Message': 'error ' + str(e)}) | |
print str(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment