Añadida LOGIN a través de BASE DE DATOS

Se ha creado la base de datos de usuarios y toda la lógica para iniciar sesión mediante ella.
This commit is contained in:
Fernando Méndez 2021-04-07 19:56:06 +02:00 committed by GitHub
parent 0e27827b5d
commit 7e2ef23349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 20 deletions

View File

@ -14,8 +14,8 @@
public function __construct($bd_name){ public function __construct($bd_name){
try{ try{
if (!$this->mysqli) { if (!$this->mysqli) {
$this->mysqli = new mysqli("localhost", "sw", $this->mysqli = new mysqli(self::_SERVERNAME, self::_USERNAME,
"_admin_", $bd_name); self::_PASSWORD, $bd_name);
} }
// echo "Conexión a la BD, satisfactoria."; // echo "Conexión a la BD, satisfactoria.";
} catch (Exception $e){ } catch (Exception $e){

View File

@ -18,50 +18,62 @@
//Methods: //Methods:
//Encrypt password with SHA254 //Encrypt password with SHA254.
private function encryptPass($password){ private function encryptPass($password){
$password = hash('sha256', $password); //$password = hash('sha256', $password);
$password = password_hash($password, PASSWORD_DEFAULT);
return $password; return $password;
} }
//Create a new User: //Returns true if the password and hash match, or false otherwise.
public function verifyPass($password, $passwd){
return password_verify($password, $passwd);
}
//Create a new User.
public function createUser($id, $username, $email, $password, $rol){ public function createUser($id, $username, $email, $password, $rol){
$password = $this->encryptPass($password); $password = $this->encryptPass($password);
$sql = sprintf( "INSERT INTO users( id, username, email, passwd, rol) $sql = sprintf( "INSERT INTO users( id, username, email, passwd, rol)
VALUES ( '%s', '%s', '%s', '%s', '%s')", VALUES ( '%s', '%s', '%s', '%s', '%s')",
$id, $username, $email, $password, $rol ); $id, $username, $email, $password, $rol );
return $sql; return $sql;
} }
//Returns a query to check if the user name exists: //Returns a query to check if the user name exists.
public function selectUser($username){ public function selectUser($username){
$username = $this->mysqli->real_escape_string($username);
$sql = sprintf( "SELECT * FROM users WHERE username = '%s'", $username ); $sql = sprintf( "SELECT * FROM users WHERE username = '%s'", $username );
$resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
//return $sql; return $resul;
return $result = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
} }
//Returns a query to check if the user pass matches: //Returns a query to check if the user pass matches.
public function selectPass($password){ public function selectPass($username, $password){
//$sql = sprintf( "SELECT * FROM users WHERE passwd = '%s'", $password ); $username = $this->mysqli->real_escape_string($username);
$sql = sprintf( "SELECT * FROM users WHERE user = '%s' AND pass = '%s'", $username, $password); $password = $this->mysqli->real_escape_string($password);
$password = $this->encryptPass($password);
//return $sql; $sql = sprintf( "SELECT * FROM users WHERE username = '%s' AND passwd = '%s'", $username, $password);
return $result = mysqli_query($this->mysqli, $sql) or die ('Error into query database'); $resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
//return $this->mysqli->query($sql);
return $resul;
} }
//Returns a query to get the user's data: //Returns a query to get the user's data.
public function userData($id){ public function userData($id){
$sql = sprintf( "SELECT * FROM users WHERE id = '%d'", $id ); $sql = sprintf( "SELECT * FROM users WHERE id = '%d'", $id );
$resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
//return $sql; return $resul;
return $result = mysqli_query($this->mysqli, $sql) or die ('Error into query database');
} }
//Create a new User Data Transfer Object: //Create a new User Data Transfer Object.
public function loadUser($id, $username, $email, $password, $rol){ public function loadUser($id, $username, $email, $password, $rol){
return new UserDTO($id, $username, $email, $password, $rol); return new UserDTO($id, $username, $email, $password, $rol);
} }