Ejemplo n.º 1
0
        public async Task <Payload <Guid> > NewUserAsync(
            NewUserInput input,
            [ScopedService] BookRefDbContext context)
        {
            if (context.Users.Any(e => e.Username.ToLower() == input.Username))
            {
                return(new Payload <Guid>(PayloadHelper.BuildSingleError(new Exception("Username already taken"))));
            }

            var user = new User(input.Username, input.Email);

            user.SetPassword(input.Password);
            await context.Users.AddAsync(user);

            var library = new PersonalLibrary(Guid.NewGuid(), user);
            await context.Libraries.AddAsync(library);

            await context.SaveChangesAsync();

            return(new Payload <Guid>(library.Id));
        }
Ejemplo n.º 2
0
        public async Task <Payload <string> > SingInAsync(
            SingInInput input,
            [ScopedService] BookRefDbContext context)
        {
            var user = context.Users.FirstOrDefault(e => e.Username == input.Username);

            if (user is null)
            {
                Log.Information("User '{Username}' not found in database", input.Username);
                return(new Payload <string>(PayloadHelper.BuildSingleError(new Exception("Bad username or password"))));
            }


            var isValid = BCrypt.Net.BCrypt.Verify(input.Password, user.Password);

            if (!isValid)
            {
                Log.Information("Password from user '{Username}' is not valid", input.Username);
                return(new Payload <string>(PayloadHelper.BuildSingleError(new Exception("Bad username or password"))));
            }


            return(new Payload <string>(BuildToken(user.Username, user.PersonalLibraryId)));
        }