def login_user(self): db = AuthDB() #parse body user_params = self.parse_body() #grab email and password user_email = user_params['email'] user_password = user_params['password'].encode('utf-8') hashed = bcrypt.hashpw(user_password, bcrypt.gensalt()) #authenticate email and verify hashed password user_data = db.authenticate_email(user_email) if user_data != None: db_password = user_data['password'].encode('utf-8') print(db_password) if bcrypt.checkpw(db_password, hashed): print("User succussfully authenticated!") self.sessionData['user_id'] = user_data['id'] self.send_response(200) self.send_header('Content-Type', 'application/JSON') self.send_cookie() self.end_headers_with_cors() basic_data = db.get_user(self.sessionData['user_id']) self.wfile.write(json.dumps(basic_data).encode('utf-8')) else: self.send_422() print('That email alerady exists') else: self.send_401()
def register_user(self): db = AuthDB() #created no response user_params = self.parse_body() print(50 * ('*')) print(user_params) print(50 * ('*')) #Hash the password and check to see if it matches user_password = user_params['password'].encode('utf-8') hashed = bcrypt.hashpw(user_password, bcrypt.gensalt()) #store the hashed password in our database self.db_password = user_params['password'] self.db_password = hashed #check if email is unique from our database user_email = user_params['email'] print(50 * ('*')) print(user_email) print(50 * ('*')) valid = db.check_email(user_params, user_email) if valid == None: # self.send_response(422) # self.send_response(409) # self.send_header("Content-type", "text/HTML") # self.end_headers_with_cors() # self.wfile.write(bytes("<html><h4>409 Error: Email already exists</h4></html>", "utf-8")) self.send_422() else: self.send_response(201) self.send_header("Content-Type", "application/json") self.send_cookie() self.end_headers_with_cors() print('email was unique, registration was succuessful')
def handlePokemonRetrieve(self): #list/getAll db = AuthDB() self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers_with_cors() all_pokemon = db.getAllPokemon() self.wfile.write(json.dumps(all_pokemon).encode('utf-8'))
def handlePokemonUpdateAtIndex(self, indexID): # update db = AuthDB() pokemon = db.getPokemonAtIndex(indexID) if pokemon: db.updatePokemonAtIndex(indexID, self.parse_body()) self.send_response(204) self.end_headers_with_cors() else: self.send_404()
def handlePokemonRetrieveAtIndex(self, indexID): # retrieve/GET(index) db = AuthDB() pokemon_by_id = db.getPokemonAtIndex(indexID) if pokemon_by_id != None: self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers_with_cors() self.wfile.write(json.dumps(pokemon_by_id).encode('utf-8')) else: self.send_404()
def handlePokemonCreate(self): # CREATE/POST db = AuthDB() db.createPokemon(self.parse_body()) self.send_response(201) self.send_header("Content-Type", "application/json") self.end_headers_with_cors() # length = int(self.headers.get("Content-Length")) # data = self.rfile.read(length).decode("utf-8") # parsed_data = parse_qs(data) # print(data) # print(parsed_data) # new_pokemon = db.createPokemon(parsed_data) self.wfile.write(json.dumps(new_pokemon).encode('utf-8'))
def handlePokemonDeleteAtIndex(self, indexID): # DELETE db = AuthDB() pDB = AuthDB() deleted_reference = pDB.getPokemonAtIndex(indexID) pokemon_to_delete = db.deletePokemonAtIndex(indexID) if pokemon_to_delete != None: self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers_with_cors() self.wfile.write(json.dumps(deleted_reference).encode('utf-8')) else: self.send_404()