Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

Commit

Permalink
fix: user registration flow
Browse files Browse the repository at this point in the history
fix: user registration flow
  • Loading branch information
deepu105 committed Apr 30, 2020
1 parent 3089956 commit 172eb99
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 33 deletions.
46 changes: 29 additions & 17 deletions src/main/java/com/adyen/demo/store/service/UserService.java
@@ -1,16 +1,9 @@
package com.adyen.demo.store.service;

import com.adyen.demo.store.config.Constants;
import com.adyen.demo.store.domain.Authority;
import com.adyen.demo.store.domain.User;
import com.adyen.demo.store.repository.AuthorityRepository;
import com.adyen.demo.store.repository.UserRepository;
import com.adyen.demo.store.security.AuthoritiesConstants;
import com.adyen.demo.store.security.SecurityUtils;
import com.adyen.demo.store.service.dto.UserDTO;

import io.github.jhipster.security.RandomUtil;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
Expand All @@ -20,11 +13,17 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.adyen.demo.store.config.Constants;
import com.adyen.demo.store.domain.Authority;
import com.adyen.demo.store.domain.CustomerDetails;
import com.adyen.demo.store.domain.User;
import com.adyen.demo.store.repository.AuthorityRepository;
import com.adyen.demo.store.repository.UserRepository;
import com.adyen.demo.store.security.AuthoritiesConstants;
import com.adyen.demo.store.security.SecurityUtils;
import com.adyen.demo.store.service.dto.UserDTO;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import io.github.jhipster.security.RandomUtil;

/**
* Service class for managing users.
Expand All @@ -41,12 +40,15 @@ public class UserService {

private final AuthorityRepository authorityRepository;

private final CustomerDetailsService customerDetailsService;

private final CacheManager cacheManager;

public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, AuthorityRepository authorityRepository, CacheManager cacheManager) {
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, AuthorityRepository authorityRepository, final CustomerDetailsService customerDetailsService, CacheManager cacheManager) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
this.authorityRepository = authorityRepository;
this.customerDetailsService = customerDetailsService;
this.cacheManager = cacheManager;
}

Expand Down Expand Up @@ -119,15 +121,24 @@ public User registerUser(UserDTO userDTO, String password) {
Set<Authority> authorities = new HashSet<>();
authorityRepository.findById(AuthoritiesConstants.USER).ifPresent(authorities::add);
newUser.setAuthorities(authorities);
CustomerDetails customer = new CustomerDetails();
customer.setGender(userDTO.getGender());
customer.setPhone(userDTO.getPhone());
customer.setAddressLine1(userDTO.getAddressLine1());
customer.setAddressLine2(userDTO.getAddressLine2());
customer.setCity(userDTO.getCity());
customer.setCountry(userDTO.getCountry());
customer.setUser(newUser);
userRepository.save(newUser);
customerDetailsService.save(customer);
this.clearUserCaches(newUser);
log.debug("Created Information for User: {}", newUser);
return newUser;
}

private boolean removeNonActivatedUser(User existingUser) {
if (existingUser.getActivated()) {
return false;
return false;
}
userRepository.delete(existingUser);
userRepository.flush();
Expand Down Expand Up @@ -290,6 +301,7 @@ public void removeNotActivatedUsers() {

/**
* Gets a list of all the authorities.
*
* @return a list of all the authorities.
*/
public List<String> getAuthorities() {
Expand Down
87 changes: 79 additions & 8 deletions src/main/java/com/adyen/demo/store/service/dto/UserDTO.java
@@ -1,14 +1,13 @@
package com.adyen.demo.store.service.dto;

import com.adyen.demo.store.config.Constants;

import com.adyen.demo.store.domain.Authority;
import com.adyen.demo.store.domain.User;

import javax.validation.constraints.*;
import java.time.Instant;
import java.util.Set;
import java.util.stream.Collectors;
import javax.validation.constraints.*;
import com.adyen.demo.store.config.Constants;
import com.adyen.demo.store.domain.Authority;
import com.adyen.demo.store.domain.User;
import com.adyen.demo.store.domain.enumeration.Gender;

/**
* A DTO representing a user, with his authorities.
Expand Down Expand Up @@ -50,6 +49,18 @@ public class UserDTO {

private Set<String> authorities;

private Gender gender;

private String phone;

private String addressLine1;

private String addressLine2;

private String city;

private String country;

public UserDTO() {
// Empty constructor needed for Jackson.
}
Expand Down Expand Up @@ -176,6 +187,60 @@ public void setAuthorities(Set<String> authorities) {
this.authorities = authorities;
}

public Gender getGender() {
return gender;
}

public UserDTO setGender(final Gender gender) {
this.gender = gender;
return this;
}

public String getPhone() {
return phone;
}

public UserDTO setPhone(final String phone) {
this.phone = phone;
return this;
}

public String getAddressLine1() {
return addressLine1;
}

public UserDTO setAddressLine1(final String addressLine1) {
this.addressLine1 = addressLine1;
return this;
}

public String getAddressLine2() {
return addressLine2;
}

public UserDTO setAddressLine2(final String addressLine2) {
this.addressLine2 = addressLine2;
return this;
}

public String getCity() {
return city;
}

public UserDTO setCity(final String city) {
this.city = city;
return this;
}

public String getCountry() {
return country;
}

public UserDTO setCountry(final String country) {
this.country = country;
return this;
}

@Override
public String toString() {
return "UserDTO{" +
Expand All @@ -186,11 +251,17 @@ public String toString() {
", imageUrl='" + imageUrl + '\'' +
", activated=" + activated +
", langKey='" + langKey + '\'' +
", createdBy=" + createdBy +
", createdBy='" + createdBy + '\'' +
", createdDate=" + createdDate +
", lastModifiedBy='" + lastModifiedBy + '\'' +
", lastModifiedDate=" + lastModifiedDate +
", authorities=" + authorities +
"}";
", gender=" + gender +
", phone='" + phone + '\'' +
", addressLine1='" + addressLine1 + '\'' +
", addressLine2='" + addressLine2 + '\'' +
", city='" + city + '\'' +
", country='" + country + '\'' +
'}';
}
}
Expand Up @@ -48,7 +48,7 @@ export const CustomerDetailsUpdate = (props: ICustomerDetailsUpdateProps) => {
...customerDetailsEntity,
...values
};
entity.user = users[values.user];
entity.user = values.user;

if (isNew) {
props.createEntity(entity);
Expand Down Expand Up @@ -158,7 +158,7 @@ export const CustomerDetailsUpdate = (props: ICustomerDetailsUpdateProps) => {
type="select"
className="form-control"
name="user.id"
value={isNew ? users[0] && users[0].id : customerDetailsEntity.user.id}
value={isNew ? users[0] && users[0].id : customerDetailsEntity.user?.id}
required
>
{users
Expand Down
Expand Up @@ -45,9 +45,14 @@ export default (state: RegisterState = initialState, action): RegisterState => {
};

// Actions
export const handleRegister = (login, email, password, langKey = 'en') => ({
export const handleRegister = (values, langKey = 'en') => ({
type: ACTION_TYPES.CREATE_ACCOUNT,
payload: axios.post('api/register', { login, email, password, langKey }),
payload: axios.post('api/register', {
...values,
login: values.username,
password: values.firstPassword,
langKey
}),
meta: {
successMessage: '<strong>Registration saved!</strong> Please check your email for confirmation.'
}
Expand Down
65 changes: 62 additions & 3 deletions src/main/webapp/app/modules/account/register/register.tsx
Expand Up @@ -5,18 +5,22 @@ import { AvForm, AvField } from 'availity-reactstrap-validation';
import { Row, Col, Alert, Button } from 'reactstrap';

import PasswordStrengthBar from 'app/shared/layout/password/password-strength-bar';
import { IRootState } from 'app/shared/reducers';
import { handleRegister, reset } from './register.reducer';

export type IRegisterProps = DispatchProps;

export const RegisterPage = (props: IRegisterProps) => {
const [password, setPassword] = useState('');

useEffect(() => () => props.reset(), []);
useEffect(
() => () => {
props.reset();
},
[]
);

const handleValidSubmit = (event, values) => {
props.handleRegister(values.username, values.email, values.firstPassword);
props.handleRegister(values);
event.preventDefault();
};

Expand Down Expand Up @@ -79,6 +83,61 @@ export const RegisterPage = (props: IRegisterProps) => {
match: { value: 'firstPassword', errorMessage: 'The password and its confirmation do not match!' }
}}
/>
<AvField
name="firstName"
label="First Name"
type="text"
validate={{
required: { value: true, errorMessage: 'This field is required.' }
}}
/>
<AvField
name="lastName"
label="Last Name"
type="text"
validate={{
required: { value: true, errorMessage: 'This field is required.' }
}}
/>
<AvField name="gender" label="Gender" type="select">
<option value=""></option>
<option value="MALE">MALE</option>
<option value="FEMALE">FEMALE</option>
<option value="OTHER">OTHER</option>
</AvField>
<AvField
name="phone"
label="Phone"
type="text"
validate={{
required: { value: true, errorMessage: 'This field is required.' }
}}
/>
<AvField
name="addressLine1"
label="Address Line 1"
type="text"
validate={{
required: { value: true, errorMessage: 'This field is required.' }
}}
/>
<AvField type="text" label="Address Line 2" name="addressLine2" />
<AvField
name="city"
label="City"
type="text"
validate={{
required: { value: true, errorMessage: 'This field is required.' }
}}
/>
<AvField
name="country"
label="Country"
type="text"
validate={{
required: { value: true, errorMessage: 'This field is required.' }
}}
/>
<Button id="register-submit" color="primary" type="submit">
Register
</Button>
Expand Down
Expand Up @@ -88,7 +88,7 @@ describe('Creating account tests', () => {
meta
}
];
await store.dispatch(handleRegister('', '', '')).then(() => expect(store.getActions()).toEqual(expectedActions));
await store.dispatch(handleRegister({})).then(() => expect(store.getActions()).toEqual(expectedActions));
});
it('dispatches ACTION_TYPES.RESET actions', async () => {
const expectedActions = [
Expand Down

0 comments on commit 172eb99

Please sign in to comment.