Skip to content

Commit

Permalink
Add requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
seionmoya committed Sep 8, 2024
1 parent d7e02fd commit 43d4817
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Fuyu.Backend.Core/DTO/Accounts/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ public class Account

[DataMember]
public Dictionary<string, int?> Games;

[DataMember]
public bool IsBanned;
}
}
10 changes: 10 additions & 0 deletions Fuyu.Backend.Core/DTO/Accounts/ELoginStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Fuyu.Backend.Core.DTO.Accounts
{
public enum ELoginStatus
{
AccountBanned,
AccountNotFound,
SessionAlreadyExists,
Success
}
}
4 changes: 4 additions & 0 deletions Fuyu.Backend.Core/DTO/Response/AccountLoginResponse.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Runtime.Serialization;
using Fuyu.Backend.Core.DTO.Accounts;

namespace Fuyu.Backend.Core.DTO.Responses
{
[DataContract]
public class AccountLoginResponse
{
[DataMember]
public ELoginStatus Status;

[DataMember]
public string SessionId;
}
Expand Down
44 changes: 37 additions & 7 deletions Fuyu.Backend.Core/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Fuyu.Common.Hashing;
using Fuyu.Common.Serialization;
using Fuyu.Backend.Core.DTO.Accounts;
using Fuyu.Backend.Core.DTO.Responses;

namespace Fuyu.Backend.Core.Services
{
Expand Down Expand Up @@ -41,15 +42,19 @@ public static int AccountExists(string username)
}
}

public static string LoginAccount(string username, string password)
public static AccountLoginResponse LoginAccount(string username, string password)
{
// find account
var accountId = AccountExists(username);

if (accountId == -1)
{
// account doesn't exist
return string.Empty;
return new AccountLoginResponse()
{
Status = ELoginStatus.AccountNotFound,
SessionId = string.Empty
};
}

// validate password
Expand All @@ -58,7 +63,22 @@ public static string LoginAccount(string username, string password)
if (account.Password != password)
{
// password is wrong
return string.Empty;
return new AccountLoginResponse()
{
Status = ELoginStatus.AccountNotFound,
SessionId = string.Empty
};
}

// validate status
if (account.IsBanned)
{
// account is banned
return new AccountLoginResponse()
{
Status = ELoginStatus.AccountBanned,
SessionId = string.Empty
};
}

// find active account session
Expand All @@ -68,8 +88,11 @@ public static string LoginAccount(string username, string password)
{
if (kvp.Value == accountId)
{
// session already exists
return kvp.Key;
return new AccountLoginResponse()
{
Status = ELoginStatus.SessionAlreadyExists,
SessionId = kvp.Key
};
}
}

Expand All @@ -78,8 +101,14 @@ public static string LoginAccount(string username, string password)
// decided to generate a new MongoId for each login.
// -- seionmoya, 2024/09/02
var sessionId = new MongoId(accountId).ToString();

CoreOrm.SetOrAddSession(sessionId, accountId);
return sessionId.ToString();

return new AccountLoginResponse()
{
Status = ELoginStatus.Success,
SessionId = sessionId.ToString()
};
}

private static int GetNewAccountId()
Expand Down Expand Up @@ -150,7 +179,8 @@ public static ERegisterStatus RegisterAccount(string username, string password)
{
{ "eft", null },
{ "arena", null }
}
},
IsBanned = false
};

CoreOrm.SetOrAddAccount(account);
Expand Down

0 comments on commit 43d4817

Please sign in to comment.