Ejemplo n.º 1
0
        public async Task <Response <bool> > Register(User user)
        {
            Response <bool> response = new Response <bool>();

            // Check if user with this email already exists
            var existingUser = await _userRepository.GetUserByEmail(user.Email);

            if (existingUser != null)
            {
                return(response.Failed("User already exists.", false));
            }

            // Populate other properties
            user.SignUpDate = DateTime.Now;
            user.Password   = PasswordCrypter.CryptPassword(user.Password);

            try
            {
                await _userRepository.SaveUser(user);

                return(response.Success("User successfully created.", true));
            }
            catch (Exception)
            {
                return(response.Failed("Could not create user.", false));
            }
        }
Ejemplo n.º 2
0
        public async Task <Response <string> > SignIn(User user)
        {
            Response <string> response = new Response <string>();

            // Get the user from the database, check if he actually exists
            var matchingUser = await _userRepository.GetUserByEmail(user.Email);

            if (matchingUser == null)
            {
                return(response.Failed("User does not exist.", "Failed request"));
            }

            // User with this email exists, now check his password
            if (!PasswordCrypter.ValidatePassword(user.Password, matchingUser.Password))
            {
                return(response.Failed("Invalid password.", "Failed request"));
            }

            // Generate token
            // We pass in the matchingUser, since it is a record fron the DB containing all of the fields that will go into claims
            return(response.Success("Successfully signed in.", CreateToken(matchingUser)));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Konstruktor klasy WHManagerDbContext
 /// </summary>
 /// <param name="options">Opcje z Startup.cs</param>
 /// <param name="passwordCrypter">Serwis odpowiedzialny za szyfrowanie haseł</param>
 public WHManagerDbContext(DbContextOptions <WHManagerDbContext> options, PasswordCrypter passwordCrypter)
     : base(options)
 {
     _passwordCrypter = passwordCrypter;
 }
Ejemplo n.º 4
0
 public static ActualResult Register(Client client)
 {
     client.Password = PasswordCrypter.CryptPassword(client.Password);
     return(ClientRepository.InsertClient(client));
 }