title: How use SocialAuth with JSF to redirect?
body: <p>I'm trying to use SocialAuth, the idea is very simple, click in <code>log in</code> with facebook then redirect the user to my website signed in.
  The <code>log in</code> part I get it, which is below :</p>
  
  <p>1) /index.xhtml</p>
  
  <pre><code>&lt;h:form id="login-facebook"&gt;
      &lt;h:commandButton id="login" action="#{socialFacebook.login}" value="Login"/&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>2) <code>socialFacebook</code> bean</p>
  
  <pre><code>package controller;
  
  
  @ManagedBean(name="socialFacebook")
  @RequestScoped
  public class SocialFacebook implements Serializable{
      private static final long serialVersionUID = -4787254243136316495L;
  
      private String code;
  
      @PostConstruct
      public void init(){
          try {
              HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
              SocialAuthManager manager = (SocialAuthManager)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("authManager");
              Map&lt;String, String&gt; paramsMap = SocialAuthUtil.getRequestParametersMap(request);
              AuthProvider provider = manager.connect(paramsMap);
  
              // get profile
              Profile p = provider.getUserProfile();
              System.out.println(p.getFullName());
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      public void login(){
          try {
              HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
  
              //Create an instance of SocialAuthConfig object
              SocialAuthConfig config  = SocialAuthConfig.getDefault();
  
              //load configuration. By default load the configuration from oauth_consumer.properties. 
              //You can also pass input stream, properties object or properties file name.
              config.load(); 
  
              //Create an instance of SocialAuthManager and set config
              SocialAuthManager manager = new SocialAuthManager();
              manager.setSocialAuthConfig(config);
  
              //URL of YOUR application which will be called after authentication
              //String successUrl = "http://localhost:8080/cc/pages/system/login_facebook.xhtml" + ";jsessionid=" + req.getSession().getId();
              String successUrl = "http://localhost:8080/cc/pages/system/index.xhtml" + ";jsessionid=" + request.getSession().getId();
  
              // get Provider URL to which you should redirect for authentication.
              // id can have values "facebook", "twitter", "yahoo" etc. or the OpenID URL
              String url = manager.getAuthenticationUrl("facebook", successUrl);
  
              // Store in session
              FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("authManager", manager);                
              //redirect to the successful login page
              FacesContext.getCurrentInstance().responseComplete();
              FacesContext.getCurrentInstance().getExternalContext().redirect(url);
  
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  
      public String getCode() {
          return code;
      }
  
      public void setCode(String code) {
          this.code = code;
      }
  
      }
  </code></pre>
  
  <p>3) Facebook returned the following URL:</p>
  
  <pre><code>http://localhost:8080/cc/pages/system/home_facebook.xhtml;jsessionid=e143aa975fa3f313c677fbcb03e3?code=AQAmJXdQX0B__zJHXnRyPfgaG1CfNUEEEEEEEEEEEEEEEZJLEpsT5s1spd3KtWGWI2HYaIOZKLkrn8axKs4iKwJVQJwJQB_WSs2iWkp2DDDDDDDDDDDDtdRPLPG7psp6r2PYmn7CTm2QNNha7f1QlgmoZtBsIEF0SSSSSSSSSSSSSSSSSSSSSSS8RutAU8dqI2KDE57f#_=_
  </code></pre>
  
  <p>4) It pass by my init method as BalusC suggest but always prints <code>nope :(</code> :</p>
  
  <pre><code>@ManagedBean(name="redirectFacebook")
  @RequestScoped
  public class RedirectFacebook implements Serializable{
      private static final long serialVersionUID = -566276017320074630L;
  
      private String code;
      private Profile profile;
  
  
      @PostConstruct
      public void init(){
          try {
              HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
              HttpSession session = (HttpSession) request.getAttribute("jsessionid");
  
              if (request.getAttribute("code") != null)
                  System.out.println("code");
              else
                  System.out.println("nope :(");
  
              if (session != null){
                  SocialAuthManager manager = (SocialAuthManager)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("authManager");
                  Map&lt;String, String&gt; paramsMap = SocialAuthUtil.getRequestParametersMap(request);
                  AuthProvider provider = manager.connect(paramsMap);
  
                  // get profile
                  profile = provider.getUserProfile();
                  System.out.println(profile.getFullName());
              }
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>5) And it prints <code>nope :(</code> too in my home_facebook page:</p>
  
  <pre><code>&lt;h:form id="redirect-facebook-form"&gt;
      &lt;f:metadata&gt;
          &lt;f:viewParam name="code" value="#{redirectFacebook.code}" /&gt;
      &lt;/f:metadata&gt;
      &lt;h:panelGroup rendered="#{not empty redirectFacebook.profile}"&gt; 
          Hello, you're successfully associated as #{socialFacebook.profile.firstName} on Facebook
      &lt;/h:panelGroup&gt;
      &lt;h:panelGroup rendered="#{empty redirectFacebook.profile}"&gt; 
          Nope :(
      &lt;/h:panelGroup&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>But, I'm a bit confuse how to get the result in my bean and do some verifications as if the user is registered or not for instance. I know, looking some code in Google, that I have to do this, but how can I redirect to my bean and do this and redirect the user to the proper page ?</p>
  
  <pre><code>    SocialAuthManager manager = (SocialAuthManager)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("authManager");
      Map&lt;String, String&gt; paramsMap = SocialAuthUtil.getRequestParametersMap(request);
      AuthProvider provider = manager.connect(paramsMap);
  
      // get profile
      Profile p;
      p = provider.getUserProfile();
  </code></pre>
  
  <p>This is really taking some nights to figure it out. 
  Any idea is very appreaciated, thanks.</p>