| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <alper@alper-celik.dev> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | using System.Text; |
| | | 6 | | using Api.Auth.Models; |
| | | 7 | | using Api.Auth.Utils; |
| | | 8 | | using Api.Database; |
| | | 9 | | using Api.Database.Utils; |
| | | 10 | | using FluentValidation; |
| | | 11 | | using Geralt; |
| | | 12 | | using HotChocolate.Authorization; |
| | | 13 | | using Microsoft.EntityFrameworkCore; |
| | | 14 | | |
| | | 15 | | namespace Api.Auth.Mutations; |
| | | 16 | | |
| | 1 | 17 | | public record LoginPayload(Queries.User User, string Token); |
| | | 18 | | |
| | | 19 | | public record LoginInput(string Email, string Password, string ClientName = "unknown"); |
| | | 20 | | |
| | | 21 | | [MutationType] |
| | | 22 | | public static partial class LoginMutations |
| | | 23 | | { |
| | | 24 | | [AllowAnonymous] |
| | | 25 | | public static async Task<LoginPayload> LoginMutation( |
| | | 26 | | [Service] PGContext db, |
| | | 27 | | [Service] IEFTransactionDIAccessorService txAccessor, |
| | | 28 | | LoginInput input |
| | | 29 | | ) |
| | | 30 | | { |
| | | 31 | | var tx = await txAccessor.BeginOrGetTransactionAsync(); |
| | | 32 | | UserEF? user = await db |
| | | 33 | | .Users.Where(u => u.Email == input.Email.NormalizeEmail()) |
| | | 34 | | .FirstOrDefaultAsync(); |
| | | 35 | | |
| | | 36 | | var junkHash = new char[Argon2id.HashSize]; |
| | | 37 | | Argon2id.ComputeHash( |
| | | 38 | | junkHash, |
| | | 39 | | Encoding.UTF8.GetBytes("123"), |
| | | 40 | | AuthUtils.ARGON2ID_ITER, |
| | | 41 | | AuthUtils.ARGON2ID_MEM_BYTES |
| | | 42 | | ); |
| | | 43 | | |
| | | 44 | | var passworkdVerified = Argon2id.VerifyHash( |
| | | 45 | | user?.PasswordHash ?? new string(junkHash), |
| | | 46 | | Encoding.UTF8.GetBytes(input.Password.Normalize()) |
| | | 47 | | ); |
| | | 48 | | |
| | | 49 | | if (user is not null && passworkdVerified) |
| | | 50 | | { |
| | | 51 | | var token = await AuthUtils.CreateUserSession(user.Id, input.ClientName, db); |
| | | 52 | | await db.SaveChangesAsync(); |
| | | 53 | | await tx.CommitAsync(); |
| | | 54 | | return new(Queries.UserMapper.ToDto(user), token); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | throw new GraphQLException( |
| | | 58 | | ErrorBuilder |
| | | 59 | | .New() |
| | | 60 | | .SetMessage("Invalid Credentials") |
| | | 61 | | .SetCode(ErrorCodes.INVALID_CREDS) |
| | | 62 | | .Build() |
| | | 63 | | ); |
| | | 64 | | } |
| | | 65 | | } |