1

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jonathan Beaudoin
  • 2,158
  • 4
  • 27
  • 63

3 Answers3

2

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.

Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
  • how does regex check if the username and password is right? I need to to validate so the account and password are able to send emails. – Jonathan Beaudoin Oct 12 '12 at 23:59
  • Yeah I realized that after my initial answer and updated it as shown. This of course is not for a real program for customers to use. I'd highly recommend Spring Security. – Abdullah Jibaly Oct 13 '12 at 00:06
1

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.

Metalhead
  • 1,429
  • 3
  • 15
  • 34
0

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("my_email@emailprovider.com") && 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("my_email@emailprovider.com") && password.equals("mypassword")) {
        areValidCredentials = true;
    }

    return areValidCredentials;
}

}

Giorgio
  • 13,129
  • 12
  • 48
  • 75
  • On Design Patterns: When to use the Singleton?: http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton – zengr Oct 13 '12 at 00:08
  • 1
    I agree there are really few reasons to use Singleton and 99% this isn't a good reason to use it. So if you are just learning Object Oriented programming do as you've never heard of the Singleton! :) – Giorgio Oct 13 '12 at 00:14