Jonathan Beaudoin
Jonathan Beaudoin

Reputation: 2188

How to validate a email and password using Java Mail API?

I'm using Java Mail API:

PasswordAuthentication valid = new PasswordAuthentication(txtEmail.getText(), 
                                                         txtPassword.getText());

if (valid != null) {
    lblInvalid.setText("Correct information!");
} else {
    lblInvalid.setText("Invalid username or password!");
}

What I want it to do, I want the user to login with their gmail username and password. I want to check if that email username and password is the real gmail login information. How do I check if the email and password enters is the users gmail account.

Upvotes: 1

Views: 5066

Answers (3)

Metalhead
Metalhead

Reputation: 1449

To validate email address you can refer this link

http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/

For validating password: You just need to retrieve the stored password for a user from some database or other security frameworks and validate against the input done by the user.

Upvotes: 1

Giorgio
Giorgio

Reputation: 13539

This is a pretty large topic.

Authentication, Authorization and validation are three different things (but pretty much related).

If you are a beginner and you are just trying some mock authentication with hard-coded credentials you could improve a little on your code with something like this:

public class Authenticator {

public boolean authenticateWithCredentials(String email, String password) {

    boolean areValidCredentials = false;

    //Validate credentials here with database or hardcoded
    if(email.equals("[email protected]") && password.equals("mypassword")) {
        areValidCredentials = true;
    }

    return areValidCredentials;
}

}

if you are going to use just one instance of this class you might use the Singleton pattern:

public class Authenticator {

//Singleton pattern
private static Authenticator instance;

public static Authenticator getInstance() {

    if(instance == null) {
        instance = new Authenticator();
    }

    return instance;
}

private Authenticator() {
    //Block creation of Authenticator instances
}

public boolean authenticateWithCredentials(String email, String password) {

    boolean areValidCredentials = false;

    //Validate credentials here with database or hardcoded
    if(email.equals("[email protected]") && password.equals("mypassword")) {
        areValidCredentials = true;
    }

    return areValidCredentials;
}

}

Upvotes: 0

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

In Java, doing new Anything() will NEVER return null.

Also, this class seems to only be a placeholder data structure, used by other parts of the JDK. It does not intrinsically do validation.

Validating an email address is usually done with regex, and kept simple. You should then send the user a confirmation message to verify their email address if that's important to you.

Passwords can also be validated for correct form using regex.

Update

Looking more closely at the error messages you are trying to emit, it looks like you want to handle authentication yourself. There are tons of ways to do this but a very simple prototype-only solutions is something like:

// create a static mapping of user/passwords:
private static Map<String, String> logins = new HashMap<String, String>();

Then in your handler:

if (txtPassword.getText().equals(logins.get(txtEmail.getText()))) {
    lblInvalid.setText("Correct information!");
} else {
    lblInvalid.setText("Invalid username or password!");
}

For something you're going to use in production I'd highly recommend Spring Security.

Upvotes: 2

Related Questions