I am building a web app and I need to pass the user's id to my front-end(angular) so I can do some ajax calls.
My question is, how can I give(store) the user's id to my front end? For example, a user can create a group by specifying a group name, but I need to be able to pass the users Id aswell in my ajax request(My backend endpoint needs both an Id and a name).
Angular:
$scope.createGroup = function() {
$http.post(BASE_URL + "/group", $scope.groupRequest).
success(function (data, status, headers, config) {
console.log("Success");
}).error(function(data, status, headers, config) {
$log.info("Error: status =" + status + ", body =" + JSON.stringify(data));
});
}
Front-end controller
@RequestMapping(value = "/group", method = RequestMethod.POST)
public ResponseEntity createGroup(@RequestBody Map body) {
try {
return restTemplate.postForEntity(URI.create(BASE_URL + "/group"), body, Map.class);
} catch (HttpClientErrorException e) {
LOG.warn("Error when trying to fetch groups", e);
return ResponseEntity.badRequest().body(e.getResponseBodyAsString());
}
}
Java Back end controller
@RequestMapping(value = "/group", method = RequestMethod.POST)
public ResponseEntity createGroup(@RequestBody @Valid GroupRequest groupRequest, BindingResult validation) {
if (validation.hasErrors()) {
throw new ValidationException(validation.getFieldErrors());
}
Long groupId = groupService.createGroup(groupRequest);
URI groupLocationURI = URI.create("/group/" + groupId);
return ResponseEntity.created(groupLocationURI).build();
}
Thanks