Skip to content

Instantly share code, notes, and snippets.

@ruddell
Created January 14, 2018 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruddell/b8fa6e203e5baab577780da02cb04381 to your computer and use it in GitHub Desktop.
Save ruddell/b8fa6e203e5baab577780da02cb04381 to your computer and use it in GitHub Desktop.
Session Auth SocialController for IgniteJHipster
package com.mycompany.myapp.web.rest;
import com.mycompany.myapp.config.Constants;
import com.mycompany.myapp.service.SocialService;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.support.URIBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.view.RedirectView;
@RestController
@RequestMapping("/social")
public class SocialController {
private final Logger log = LoggerFactory.getLogger(SocialController.class);
private final SocialService socialService;
private final ProviderSignInUtils providerSignInUtils;
public SocialController(SocialService socialService, ProviderSignInUtils providerSignInUtils) {
this.socialService = socialService;
this.providerSignInUtils = providerSignInUtils;
}
@GetMapping("/signup")
public RedirectView signUp(WebRequest webRequest, @CookieValue(name = "NG_TRANSLATE_LANG_KEY", required = false, defaultValue = Constants.DEFAULT_LANGUAGE) String langKey) {
try {
Connection<?> connection = providerSignInUtils.getConnectionFromSession(webRequest);
socialService.createSocialUser(connection, langKey.replace("\"", ""));
return new RedirectView(URIBuilder.fromUri("/#/social-register/" + connection.getKey().getProviderId())
.queryParam("success", "true")
.build().toString(), true);
} catch (Exception e) {
log.error("Exception creating social user: ", e);
return new RedirectView(URIBuilder.fromUri("/#/social-register/no-provider")
.queryParam("success", "false")
.build().toString(), true);
}
}
@PostMapping(value = "/token")
public ResponseEntity loadConnectionFromToken(@RequestParam String token, @RequestParam String secret, @RequestParam String provider) {
try {
socialService.loadConnectionFromToken(token, secret, provider);
return ResponseEntity.ok().build();
} catch (Exception e) {
throw new BadRequestAlertException("Failed to sign in.", "login", "messages.error.authentication");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment