Refactor repository methods to async and update credential logic

This commit is contained in:
Aaron Po
2026-01-22 11:14:23 -05:00
parent fd544dbd34
commit 82db763951
11 changed files with 84 additions and 67 deletions

View File

@@ -164,7 +164,13 @@ CREATE TABLE UserCredential -- delete credentials when user account is deleted
Hash NVARCHAR(MAX) NOT NULL,
-- uses argon2
Timer ROWVERSION,
IsRevoked BIT NOT NULL
CONSTRAINT DF_UserCredential_IsRevoked DEFAULT 0,
RevokedAt DATETIME NULL,
Timer ROWVERSION,
CONSTRAINT PK_UserCredential
PRIMARY KEY (UserCredentialID),
@@ -173,9 +179,6 @@ CREATE TABLE UserCredential -- delete credentials when user account is deleted
FOREIGN KEY (UserAccountID)
REFERENCES UserAccount(UserAccountID)
ON DELETE CASCADE,
CONSTRAINT AK_UserCredential_UserAccountID
UNIQUE (UserAccountID)
);
CREATE NONCLUSTERED INDEX IX_UserCredential_UserAccount

View File

@@ -1,6 +1,6 @@
CREATE OR ALTER PROCEDURE dbo.USP_AddUserCredential(
@UserAccountId uniqueidentifier,
@Hash nvarchar(max)
CREATE OR ALTER PROCEDURE dbo.USP_AddUpdateUserCredential(
@UserAccountId UNIQUEIDENTIFIER,
@Hash NVARCHAR(MAX)
)
AS
BEGIN
@@ -16,14 +16,14 @@ BEGIN
)
THROW 50001, 'UserAccountID does not exist.', 1;
IF EXISTS (
SELECT 1
FROM dbo.UserCredential
WHERE UserAccountID = @UserAccountId
)
THROW 50002, 'UserCredential for this UserAccountID already exists.', 1;
-- invalidate old credentials
UPDATE dbo.UserCredential
SET IsRevoked = 1,
RevokedAt = GETDATE()
WHERE UserAccountId = @UserAccountId
AND IsRevoked = 0;
INSERT INTO dbo.UserCredential
(UserAccountId, Hash)
VALUES

View File

@@ -0,0 +1,18 @@
CREATE OR ALTER PROCEDURE dbo.USP_GetUserCredentialByUserAccountId(
@UserAccountId UNIQUEIDENTIFIER
)
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;
SELECT
UserCredentialId,
UserAccountId,
Hash,
IsRevoked,
CreatedAt,
RevokedAt
FROM dbo.UserCredential
WHERE UserAccountId = @UserAccountId AND IsRevoked = 0;
END;