public function testCanAddNewUser() { $data = array("username" => "TestUser", "password" => "test123", "email" => "*****@*****.**", "first_name" => "Testy", "last_name" => "McTesterson", "date_of_birth" => "1970-01-01"); $this->dbService->addNewUser($data); $stmt = self::$db->prepare("SELECT * \r\n FROM user_profile \r\n WHERE username = :username"); $stmt->execute(array(':username' => $data['username'])); $this->assertEquals(1, $stmt->rowCount()); $results = $stmt->fetch(PDO::FETCH_ASSOC); $this->assertEquals($data['username'], $results['username']); $this->assertEquals($this->dbService->getPassword($data['username']), $results['password']); $this->assertEquals($data['email'], $results['email']); $this->assertEquals($data['first_name'], $results['first_name']); $this->assertEquals($data['last_name'], $results['last_name']); $this->assertEquals($data['date_of_birth'], $results['date_of_birth']); $stmt = self::$db->prepare("SELECT *\r\n FROM user_registration\r\n WHERE user_id = :id"); $stmt->execute(array(':id' => $results['id'])); $results = $stmt->fetch(PDO::FETCH_ASSOC); $data['key'] = $results['verification']; $this->assertEquals(1, $stmt->rowCount()); return $data; }
/** * Authenticates a user if given the correct username and password. * * @param Request $request The HTTP Request object. * @param Response $response The HTTP Response object. * @param array $args The array containing arguments provided. * * @return string The message from the authentication process. */ public function authenticate(Request $request, Response $response, array $args) { //get post variables from request body $post = $request->getParams(); //validate post variables (exist, and as expected) /** @var Validator $v */ $v = new Validator($post); $v->rule('required', ['username', 'password']); $ret = array(); //if validation fails, exit, else authenticate if ($v->validate()) { if (password_verify($post['password'], $this->dbService->getPassword($post['username']))) { $user = $this->dbService->getUser($post['username']); if ($user) { if ($this->dbService->hasVerified($post['username'])) { $remember = $post['remember']; $this->startSession($user, $remember); $ret['success'] = true; $ret['message'] = "authenticated"; } else { $ret['success'] = false; $ret['message'] = "This account has not yet been verified."; } } else { $ret['success'] = false; $ret['message'] = "Incorrect username and/or password"; } } else { $ret['success'] = false; $ret['message'] = "Incorrect username and/or password"; } } else { $ret['success'] = true; $ret['message'] = "Please enter your username and password."; } return json_encode($ret); }