Example #1
0
        public async Task <AdminResponse> Authenticate(string userName, string password)
        {
            _logger.LogInfo("Authentication method called");
            try
            {
                AdminUser adminUser = _mapper.Map <AdminUser>(_adminUserRepo.GetAdminUser(userName));
                if (adminUser == null)
                {
                    _logger.LogError("User doesn't exist");
                    throw new Exception(string.Format(_messageHandler.GetMessage(ErrorMessagesEnum.AuthUserDoesNotExists)));
                }
                if (!VerifyPasswordHash(password, adminUser.PasswordHash, adminUser.PasswordSalt))
                {
                    _logger.LogError("Invalid credential");
                    throw new Exception(string.Format(_messageHandler.GetMessage(ErrorMessagesEnum.AuthWrongCredentials)));
                }
                _logger.LogInfo("JWT Token creation initiatted");
                var token = await _jwtFactory.GenerateEncodedToken(adminUser.Id.ToString(), adminUser.UserName, adminUser.Role);

                _logger.LogInfo("Successfully generate JWT Token");
                AdminResponse response = new AdminResponse(true, string.Format(_messageHandler.GetSuccessMessage(SuccessMessagesEnum.SuccessfullyLoggedIn)));
                response.Token = token;
                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(new AdminResponse(false, ex.Message));
            }
        }