def user(): #Cookie Authentication if request.method == 'GET': user_id = u_ctrl.verify_session(request) #if cookie exists if user_id != None: #return user data from user id on cookie, and refresh cookie response = jsonify(u_ctrl.get_user_as_dictionary(user_id)) response = u_ctrl.create_session(response, user_id) return response, 200 #otherwise return a 204 return "No login", 204 #Regular logins if request.method == 'POST': body = request.get_json() #if user does not exist in database if not u_ctrl.username_exists(body['username']): return 'User does not exist', 404 #if user has correct password if u_ctrl.verify_user(body['username'], body['password']): #make response user_id = u_ctrl.get_user_id(body['username']) response = jsonify(u_ctrl.get_user_as_dictionary(user_id)) #add session-cookie to response response = u_ctrl.create_session(response, user_id) #return user object with a 200 return response, 200 #return 401 if auth failed else: return 'Invalid password', 401 #Destroying sessions on logaou if request.method == 'DELETE': response = jsonify(response="Session Destroyed") response = u_ctrl.destroy_session(response) return response, 204
def newUser(): body = request.get_json() #if user is not already in db if not u_ctrl.username_exists(body['username']): #add user to db u_ctrl.make_new_user(body) #make response user_id = u_ctrl.get_user_id(body['username']) response = jsonify(u_ctrl.get_user_as_dictionary(user_id)) #add session-cookie to response response = u_ctrl.create_session(response, user_id) #return user object with a 201 # when creating a new user save some mock recommendations r_ctrl.populate_new_user_recommendations(user_id) # work_queue.enqueue("find_prob",[user_id]) return response, 201 #else return a 302 for Found else: return 'Username already exists', 302