Skip to content

Commit

Permalink
[Login] When logging in to the account, update the last IP and last t…
Browse files Browse the repository at this point in the history
…ime in the database (AAEmu#986)
  • Loading branch information
yanlong-li committed Jul 2, 2024
1 parent 126a3fb commit 0c9ff7e
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion AAEmu.Login/Core/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ public static void Login(LoginConnection connection, string username)

connection.SendPacket(new ACJoinResponsePacket(0, 6));
connection.SendPacket(new ACAuthResponsePacket(connection.AccountId, 6));

reader.Close();

#region update account
command.Parameters.Clear();
command.CommandText = "UPDATE `users` SET last_ip = @last_ip, last_login = @last_login, updated_at = @updated_at WHERE id = @id";
command.Parameters.AddWithValue("@id", connection.AccountId);
command.Parameters.AddWithValue("@last_ip", connection.LastIp.ToString());
command.Parameters.AddWithValue("@last_login", ((DateTimeOffset)connection.LastLogin).ToUnixTimeSeconds());
command.Parameters.AddWithValue("@updated_at", ((DateTimeOffset)connection.LastLogin).ToUnixTimeSeconds());
command.Prepare();

if (command.ExecuteNonQuery() != 1)
{
Logger.Warn("Database update failed, error occurred while updating account login IP and time");
}
# endregion
}
}
}
Expand Down Expand Up @@ -106,6 +123,23 @@ public static void Login(LoginConnection connection, string username, IEnumerabl
Logger.Info("{0} connected.", connection.AccountName);
connection.SendPacket(new ACJoinResponsePacket(0, 6));
connection.SendPacket(new ACAuthResponsePacket(connection.AccountId, 6));

reader.Close();

#region update account
command.Parameters.Clear();
command.CommandText = "UPDATE `users` SET last_ip = @last_ip, last_login = @last_login, updated_at = @updated_at WHERE id = @id";
command.Parameters.AddWithValue("@id", connection.AccountId);
command.Parameters.AddWithValue("@last_ip", connection.LastIp.ToString());
command.Parameters.AddWithValue("@last_login", ((DateTimeOffset)connection.LastLogin).ToUnixTimeSeconds());
command.Parameters.AddWithValue("@updated_at", ((DateTimeOffset)connection.LastLogin).ToUnixTimeSeconds());
command.Prepare();

if (command.ExecuteNonQuery() != 1)
{
Logger.Warn("Database update failed, error occurred while updating account login IP and time");
}
# endregion
}
}
}
Expand All @@ -118,9 +152,13 @@ public static void CreateAndLoginInvalid(LoginConnection connection, string user
using (var command = connect.CreateCommand())
{
command.CommandText =
"INSERT into users (username, password, email, last_ip) VALUES (@username, @password, \"\", \"\")";
"INSERT into users (username, password, email, last_ip, created_at, updated_at) VALUES (@username, @password, \"\", @last_ip, @last_login, @created_at, @updated_at)";
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", pass);
command.Parameters.AddWithValue("@last_ip", connection.LastIp.ToString());
command.Parameters.AddWithValue("@last_login", ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds());
command.Parameters.AddWithValue("@created_at", ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds());
command.Parameters.AddWithValue("@updated_at", ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds());
command.Prepare();

if (command.ExecuteNonQuery() != 1)
Expand Down

0 comments on commit 0c9ff7e

Please sign in to comment.