Skip to content

Commit

Permalink
added ability to receive token when you signup
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerstock committed Jun 25, 2019
1 parent b22d04e commit 9c98881
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

Expand All @@ -38,23 +36,47 @@ public ResponseEntity<?> addNewUser(HttpServletRequest request, @Valid
@RequestBody
User newuser) throws URISyntaxException
{

logger.trace(request.getRequestURI() + " accessed");

ArrayList<UserRoles> newRoles = new ArrayList<>();
newRoles.add(new UserRoles(newuser, roleService.findByName("user")));
newuser.setUserRoles(newRoles);

newuser = userService.save(newuser);
String temp = newuser.getPasswordRaw();
newuser = userService.save(newuser); //passwordRaw is nullified


String loginUrl
= "https://sgs-lambda-bookr.herokuapp.com//oauth/token";

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

headers.add("Authorization", "Basic bGFtYmRhLWNsaWVudDpsYW1iZGEtc2VjcmV0");
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("grant_type", "password");
map.add("username", newuser.getUsername());
map.add("password", temp);
HttpEntity<MultiValueMap<String, String>> request2 = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity(
loginUrl, request2 , String.class);




// set the location header for the newly created resource - to another controller!
/* // set the location header for the newly created resource - to another controller!
HttpHeaders responseHeaders = new HttpHeaders();
URI newRestaurantURI = ServletUriComponentsBuilder
URI createUserURI = ServletUriComponentsBuilder
.fromUriString(request.getServerName() + ":" + request.getLocalPort() + "/users/user/{userId}")
.buildAndExpand(newuser.getUserid()).toUri();
responseHeaders.setLocation(newRestaurantURI);
responseHeaders.setLocation(createUserURI);*/


return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
return response;
//return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/lambdaschool/starthere/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class User extends Auditable
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;

@Column
private String passwordRaw;

@OneToMany(mappedBy = "user",
cascade = CascadeType.ALL)
@JsonIgnoreProperties("user")
Expand All @@ -46,6 +49,17 @@ public User(String username, String password, List<UserRoles> userRoles)
}
this.userRoles = userRoles;
}
public User(String username, String password, List<UserRoles> userRoles, Boolean encrypt)
{
setUsername(username);
if(encrypt) setPassword(password);
else setPasswordNoEncrypt(password);
for (UserRoles ur : userRoles)
{
ur.setUser(this);
}
this.userRoles = userRoles;
}

public long getUserid()
{
Expand Down Expand Up @@ -74,10 +88,15 @@ public String getPassword()

public void setPassword(String password)
{
this.passwordRaw = password;
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
this.password = passwordEncoder.encode(password);
}

public String getPasswordRaw() {
return passwordRaw;
}

public void setPasswordNoEncrypt(String password)
{
this.password = password;
Expand Down

0 comments on commit 9c98881

Please sign in to comment.