[Spring Boot] Twitch oauth2 is not redirecting after successful authorization

Hi together,

in angular the user can chose the oauth2 provider

  oauthLogin(provider: string) {
    const oauthUrl = `http://localhost:8080/oauth2/authorization/${provider}`;
    window.location.href = oauthUrl;
  }

After clicking the button the redirect to http://localhost:8080/oauth2/code/twitch happens.
I receive a Authorization Code with this token i can get an access Token and with this token I can get the user informations.

@RestController
@RequiredArgsConstructor
@RequestMapping("/oauth2/code")
public class OAuth2Controller {

    @Value("${spring.security.oauth2.client.registration.twitch.client-id}")
    private String clientId;
    @Value("${spring.security.oauth2.client.registration.twitch.client-secret}")
    private String clientSecret;
    private HttpHeaders createHeaders(String accessToken) {

        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + accessToken);
        headers.set("Client-Id", clientId);
        return headers;
    }

    public ResponseEntity<String> getUserInfo(String accessToken) {
        System.out.println("accessToken: " + accessToken);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = createHeaders(accessToken);
        System.out.println("headers: " + headers);
        HttpEntity<String> entity = new HttpEntity<>(headers);

        String userInfoUrl = "https://api.twitch.tv/helix/users";
        ResponseEntity<String> response = restTemplate.exchange(userInfoUrl, HttpMethod.GET, entity, String.class);

        return response;
    }

    public String getAccessToken(String authorizationCode) {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> requestBody = new HashMap<>();
        requestBody.put("client_id", clientId);
        requestBody.put("client_secret", clientSecret);
        requestBody.put("code", authorizationCode);
        requestBody.put("grant_type", "authorization_code");
        requestBody.put("redirect_uri", "http://localhost:8080/oauth2/code/twitch");

        String tokenUrl = "https://id.twitch.tv/oauth2/token";
        Map<String, Object> response = restTemplate.postForObject(tokenUrl, requestBody, Map.class);

        return response != null ? (String) response.get("access_token") : null;
    }
    @GetMapping("/twitch")
    public ResponseEntity<?> handleTwitchCallback(@RequestParam("code") String authorizationCode) {
        System.out.println("Authorization Code: " + authorizationCode);
        String accessToken = getAccessToken(authorizationCode);
        System.out.println("Access Token: " + accessToken);
        ResponseEntity<String> userInfoResponse = getUserInfo(accessToken);
        System.out.println("User Info: " + userInfoResponse.getBody());
        return ResponseEntity.ok().body("");
    }

}

My apllication properties

spring.security.oauth2.client.registration.twitch.client-id=censored
spring.security.oauth2.client.registration.twitch.client-secret=censored
spring.security.oauth2.client.registration.twitch.redirect-uri=http://localhost:8080/oauth2/code/twitch
spring.security.oauth2.client.registration.twitch.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.twitch.scope=user:read:email

spring.security.oauth2.client.provider.twitch.authorization-uri=https://id.twitch.tv/oauth2/authorize
spring.security.oauth2.client.provider.twitch.token-uri=https://id.twitch.tv/oauth2/token
spring.security.oauth2.client.provider.twitch.user-info-uri=https://api.twitch.tv/helix/users
spring.security.oauth2.client.provider.twitch.user-name-attribute=login

All this is working. But the redirect is not working

@Configuration

public class OAuth2SecurityConfig {
    @Bean
    public SecurityFilterChain oauth2SecurityFilterChain(HttpSecurity http) throws Exception {
        http
                .oauth2Login()
                .loginPage("/oauth2/authorization/google")
                .defaultSuccessUrl("http://localhost:4200/oauthsuccess")
                .failureUrl("http://localhost:4200/oauthfailure")
                .and()
                .oauth2Login()
                .loginPage("/oauth2/authorization/twitch")
                .defaultSuccessUrl("http://localhost:4200/oauthsuccess")
                .failureUrl("http://localhost:4200/oauthfailure");
        return http.build();
    }
}

For google the redirect is working. But google was generally easier to implement because I didn’t need to request all this token and directly received the user data. I think this is because Google is one of the four default providers in spring boot.

Does anyone knows why defaultSuccessUrl(“http://localhost:4200/oauth-success”) is not working?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.