def login_user(data): try: # fetch the user data user = User.query.filter_by(email=data.get('email')).first() if user and user.check_password(data.get('password')): auth_token = User.encode_auth_token(user.id) if auth_token: response_object = { 'status': 'success', 'message': 'Successfully logged in.', 'access_token': auth_token.get('access_token'), 'refresh_token': auth_token.get('refresh_token') } return response_object, 200 else: response_object = { 'status': 'fail', 'message': 'email or password does not match.' } return response_object, 401 except Exception as e: print(e) response_object = {'status': 'fail', 'message': 'Try again'} return response_object, 500
def get_logged_in_user(new_request): # get the auth token auth_token = new_request.headers.get('Authorization') if auth_token: resp = User.decode_auth_token(auth_token) if not isinstance(resp, str): user = User.query.filter_by(id=resp).first() response_object = { 'status': 'success', 'data': { 'user_id': user.id, 'email': user.email, 'admin': user.admin, 'registered_on': str(user.registered_on) } } return response_object, 200 response_object = {'status': 'fail', 'message': resp} return response_object, 401 else: response_object = { 'status': 'fail', 'message': 'Provide a valid auth token.' } return response_object, 401
def save_new_user(data): user = User.query.filter_by(email=data['email']).first() print(data['username']) if not validate_email(data['email'], verify=True, check_mx=True): raise notice(status_code=500, return_code=20006, action_status=False) if not data['password'] or not data['username']: raise notice(status_code=422, return_code=20007, action_status=False) if not user: user = User(public_id=str(uuid.uuid4()), email=data['email'], username=data['username'], password_hash=data['password']) # Hash new user password user.hash_password(data['password']) save_changes(user) email_confirm_token = (user.generate_confirmation_token( data['email'], data['username'])) confirm_url = (url_for('v1_blueprint.confirm', confirm_token=email_confirm_token, _external=True)) + '?email=' + data['email'] # send confirm email to register user. send_email( to=data['email'], subject='active', template='email_tpl/confirm', confirm_url=confirm_url, user=data['username'], ) raise notice(status_code=200, return_code=30001, action_status=True, playbook={ 'username': data['username'], 'create_time': str(user.member_since), 'confirm_url': str(confirm_url), }) else: raise notice(status_code=409, return_code=20004)
def refresh_token(data): print (data) refresh_token = request.json.get('refresh_token') # Get if the refresh token is in blacklist ref = Blacklist.query.filter_by(refresh_token=refresh_token).first() print (ref) try: data = (refresh_jwt.loads(refresh_token)) print (data) #print (s) except Exception as why: # Log the error. print (why) ## Create user not to add db. For generating token. user = User(email=data['email']) token = user.generate_auth_token(False) return {'access_token': token}
def generate_token(user): try: # generate the auth token auth_token = User.encode_auth_token(user.id) response_object = { 'status': 'success', 'message': 'Successfully registered.', 'Authorization': auth_token.decode() } return response_object, 201 except Exception as e: response_object = { 'status': 'fail', 'message': 'Some error occurred. Please try again.' } return response_object, 401
def get(self, confirm_token): # Get Confirm email confirm_email = request.args.get('email') # Check confirm email #if validate_email(confirm_email, check_mx=True, verify=True): # return {"message": "email invalid input."}, 423 # use staticmethod verify confirm toke if User.verify_confirm_token(confirm_token, confirm_email): raise notice(status_code=200, return_code=30002,action_status=True) else: raise notice(status_code=202,return_code=20009, action_status=False)
def save_new_user(data): user = User.query.filter_by(email=data['email']).first() if not user: new_user = User(public_id=str(uuid.uuid4()), email=data['email'], username=data['username'], password=data['password'], registered_on=datetime.datetime.utcnow()) user = save_changes(new_user) response_object = { 'status': 'success', 'message': '%s Successfully registered.' % (new_user.username), } return response_object, 201 else: response_object = { 'status': 'fail', 'message': 'User already exists. Please Log in.', } return response_object, 409