Implementing Authentication in Android Chat Application YQ
The following code demonstrates the implementation of user authentication within an Android chat application named YQ.
The login activity handles user input and initiates the authentication process:
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button loginButton = (Button) findViewById(R.id.btn_login);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText accountField = (EditText) findViewById(R.id.et_account);
EditText passwordField = (EditText) findViewById(R.id.et_password);
int accountId = Integer.parseInt(accountField.getText().toString());
String userPassword = passwordField.getText().toString();
performAuthentication(accountId, userPassword);
}
});
}
private void performAuthentication(int userId, String userPass) {
User userInfo = new User();
userInfo.setAccount(userId);
userInfo.setPassword(userPass);
userInfo.setOperation("login");
boolean success = new YQConServer().sendLoginInfo(userInfo);
if (success) {
try {
// Request list of online contacts
// ---
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(this, "Authentication failed!", Toast.LENGTH_SHORT).show();
}
}
}
The client-side implementation manages network communication with the server:
public class YQClient {
private Socket connection;
public boolean sendLoginInfo(Object data) {
boolean result = false;
try {
connection = new Socket();
try {
connection.connect(new InetSocketAddress("10.0.2.2", 5469), 2000);
} catch (SocketTimeoutException e) {
return false; // Connection timeout
}
ObjectOutputStream outputStream = new ObjectOutputStream(connection.getOutputStream());
outputStream.writeObject(data);
ObjectInputStream inputStream = new ObjectInputStream(connection.getInputStream());
YQMessage message = (YQMessage) inputStream.readObject();
if (message.getType().equals(YQMessageType.SUCCESS)) {
// Establish persistent connection thread
// ---
result = true;
} else if (message.getType().equals(YQMessageType.FAIL)) {
result = false;
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return result;
}
}
The server-side component processes incoming authentication requests:
public class YQServer {
public YQServer() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(5469);
System.out.println("Server started, listening on port 5469...");
while (true) {
Socket clientConnection = serverSocket.accept();
ObjectInputStream inputStream = new ObjectInputStream(clientConnection.getInputStream());
User userData = (User) inputStream.readObject();
YQMessage responseMessage = new YQMessage();
ObjectOutputStream outputStream = new ObjectOutputStream(clientConnection.getOutputStream());
if (userData.getOperation().equals("login")) {
int accountId = userData.getAccount();
boolean authResult = new UserDao().login(accountId, userData.getPassword());
if (authResult) {
System.out.println("[" + accountId + "] logged in");
responseMessage.setType(YQMessageType.SUCCESS);
outputStream.writeObject(responseMessage);
// Spawn dedicated thread for continuous communication
// ---
} else {
responseMessage.setType(YQMessageType.FAIL);
outputStream.writeObject(responseMessage);
}
} else if (userData.getOperation().equals("register")) {
// Handle registration
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The application requires handling socket timeouts to prevent UI blocking during network failures.