2

I have this React App which has back end as Spring Boot java application. Its login flow requires to redirect from App to third party servlet, and then this servlet calls our backend API with AUTH code, which after validation in backend must be redirected to our react App, along with Auth code. Now problem is, while I am able to redirect response to my react app, how can I access http, ResponseEntity in react App?

@GetMapping
public ResponseEntity<String> generateAuthToken(
        @RequestParam("code") String authCode,
        @RequestParam("state") String state,
        HttpServletResponse httpServletResponse
) {
    log.debug("Found token {}, and state {}", authCode, state);
    String authToken = getAuthToken(authCode);
    httpServletResponse.setHeader("Location", "http://localhost:3000/MainMenu");
    httpServletResponse.setStatus(302);
    return new ResponseEntity<String>(authToken , HttpStatus.MOVED_PERMANENTLY);

}

How can I access this authToken in sent in ReponseEntity in our ReactApp's MainMenu component. User clicks login button on Front end App (in react) -> App redirect user to a third party servlet to gather credentials -> Servlet authenticates user, generate AUTHZ code and call a rest end point on back end -> Back end point(Spring Boot application) process AUTHZ code and generates String access token and redirect response to Front end react App with this access token. I am able to redirect to Front end app using HttpServletResponse redirect, how to send access token to react app.

  • How does the code that you have tried so far look like? What is the actual problem that you are trying to solve? – AxelJunes Sep 03 '19 at 11:31
  • If I'm understanding this correctly, you would need to add that authToken to an endpoint that returns a ModelAndView object, and then you can attach any type of data you'd like to it which would be accessable in the view layer. – clint_milner Sep 03 '19 at 11:33
  • I am integrating Generic SSO flow in my App, this integration requires redirection from App to third party Authentication servlet, which on success, send response to Rest endpoint on backend. Backend has to process this response and create some token, which must be used in App as user validation token. So backend has to, after generating this token, redirect to our App along with this token. – Abhijeet srivastava Sep 03 '19 at 11:34

1 Answers1

0

You can use "axios" or "fetch" in our react app/ui to fetch data from server. In below example fetch is used to call rest end point service.

const getToken= async () => {
    try {
      let response = await fetch('http://<host>:<port>/<endpoint url>?code=${codevalue}&state=${statevalue}',
      {method:"GET"});
      let data =  await response.json();
      console.log(data);
        
    } catch (error) {
      console.error(error);
    }
  };