Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirmation link Email in JSF

How do you make a link which you can email to users to confirm their email address is clicked in JSF? i.e. once they click on the link their account will be activated.

like image 803
DD. Avatar asked Jul 20 '10 23:07

DD.


2 Answers

Assuming you're already on JSF 2.0, you could grab @ManagedProperty and @PostConstruct.

@ManagedBean
@RequestScoped
public class Activation {

    @ManagedProperty(value="#{param.key}")
    private String key;
    private boolean valid;

    @PostConstruct
    public void init() {
        valid = check(key); // And auto-login if valid?
    }

    // ...
}

and then in JSF which is accessed by http://example.com/activate.jsf?key=somelonggeneratedkey

<h:panelGroup layout="block" rendered="#{activation.valid}">
   <p>Your account is successfully activated!</p>
   <p><h:link outcome="home">Go to home page</h:link></p>
</h:panelGroup>
<h:panelGroup layout="block" rendered="#{!activation.valid}">
   <p>Activation failed! Please enter your email address to try once again.</p> 
   <h:form>
       ...
   </h:form>
</h:panelGroup>
like image 103
BalusC Avatar answered Nov 07 '22 02:11

BalusC


You can implement it by creating a page (.jsp for ex) that has:

<f:view beforePhaseListener="#{userActivationController.performActivation}">

(this is for facelets; for jsp the attribute is just beforePhase). And then, in the managed bean's method use FacesContext.getCurrentContext().getExternalContext().getParameterMap() to obtain the request patameters and get the activation code, which is passed like:

http://yoursite.com/activate.jsp?code=54gfd54tgdgfd

like image 24
Bozho Avatar answered Nov 07 '22 04:11

Bozho