@@ -31,7 +31,7 @@ function authenticate(string $username, string $password, mysqli $mysqli):string
3131 }
3232
3333 // Search the database for the user based on their username.
34- $ result = $ stmt = $ mysqli ->prepare ("SELECT id, name FROM users WHERE username = ?; " );
34+ $ stmt = $ mysqli ->prepare ("SELECT id, name, password FROM users WHERE username = ?; " );
3535 $ stmt ->bind_param ("s " , $ username );
3636 $ stmt ->execute ();
3737 $ result = $ stmt ->get_result ()->fetch_all (MYSQLI_ASSOC ) ?: null ;
@@ -40,15 +40,15 @@ function authenticate(string $username, string $password, mysqli $mysqli):string
4040 return 'Username or password is incorrect. ' ;
4141 }
4242
43- // Validate the password.
44- $ passwordHash = password_hash ($ password , PASSWORD_DEFAULT );
45- if (password_verify ($ password , $ passwordHash ) === false ) {
43+ $ result = reset ($ result );
44+
45+ // Validate the supplied password against the hashed password in the database.
46+ if (password_verify ($ password , $ result ['password ' ]) === false ) {
4647 return 'Username or password is incorrect. ' ;
4748 }
4849
4950 // The password validates correctly, so add their username to
5051 // the $_SESSION variable, which will log the user in.
51- $ result = reset ($ result );
5252 $ _SESSION ['username ' ] = $ username ;
5353 $ _SESSION ['name ' ] = htmlspecialchars ($ result ['name ' ]);
5454 $ _SESSION ['user_id ' ] = $ result ['id ' ];
0 commit comments