You are on page 1of 7

Utilizar Parse con Android

Realizar un login y autentificacion de usuario


El uso de esta funcionalidad permite a los usuarios
acceder a su informacin de forma segura y Parse.com
se encarga de automatizar gran parte de la
funcionalidad necesaria para la gestin de cuentas de
usuario.
1.- Iniciar con una cuenta de parse
www.parse.com

2.- Descargar la ltima SDK Parse.com para Android
https://www.parse.com/docs/downloads

3.- Crear un proyecto en eclipse android


public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Determine whether the current user is an anonymous user
if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
// If user is anonymous, send the user to LoginSignupActivity.class
Intent intent = new Intent(MainActivity.this,
LoginSignupActivity.class);
startActivity(intent);
finish();
} else {
// If current user is NOT anonymous user
// Get current user data from Parse.com
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
// Send logged in users to Welcome.class
Intent intent = new Intent(MainActivity.this, Welcome.class);
startActivity(intent);
finish();
} else {
// Send user to LoginSignupActivity.class
Intent intent = new Intent(MainActivity.this,
LoginSignupActivity.class);
startActivity(intent);
finish();
} } } }
public class LoginSignupActivity extends Activity {
// Declare Variables
Button loginbutton;
Button signup;
String usernametxt;
String passwordtxt;
EditText password;
EditText username;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from main.xml
setContentView(R.layout.main);
// Locate EditTexts in main.xml
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);

// Locate Buttons in main.xml
loginbutton = (Button) findViewById(R.id.login);
signup = (Button) findViewById(R.id.signup);

// Login Button Click Listener
loginbutton.setOnClickListener(new OnClickListener() {


public void onClick(View arg0) {
// Retrieve the text entered from the EditText
usernametxt = username.getText().toString();
passwordtxt = password.getText().toString();

// Send data to Parse.com for verification
ParseUser.logInInBackground(usernametxt,
passwordtxt,
new LogInCallback() {
public void done(ParseUser user,
ParseException e) {
if (user != null) {
// If user exist and authenticated, send
user to Welcome.class
Intent intent = new Intent(
LoginSignupActivity.this,
Welcome.class);
startActivity(intent);
Toast.makeText(getApplicationContext(),
"Successfully Logged in",
Toast.LENGTH_LONG).show();
finish();
} else {
Toast.makeText(
getApplicationContext(),
"No such user exist, please signup",
Toast.LENGTH_LONG).show();
} }
}); } });

// Sign up Button Click Listener
signup.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// Retrieve the text entered from the EditText
usernametxt = username.getText().toString();
passwordtxt = password.getText().toString();

// Force user to fill up the form
if (usernametxt.equals("") && passwordtxt.equals("")) {
Toast.makeText(getApplicationContext(),
"Please complete the sign up form",
Toast.LENGTH_LONG).show();

} else {
// Save new user data into Parse.com Data Storage
ParseUser user = new ParseUser();
user.setUsername(usernametxt);
user.setPassword(passwordtxt);
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Show a simple Toast message upon
successful registration
Toast.makeText(getApplicationContext(),
"Successfully Signed up, please log in.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Sign up Error", Toast.LENGTH_LONG)
.show();
} }
});
} }
}); } }
public class Welcome extends Activity {

// Declare Variable
Button logout;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from singleitemview.xml
setContentView(R.layout.welcome);

// Retrieve current user from Parse.com
ParseUser currentUser = ParseUser.getCurrentUser();

// Convert currentUser into String
String struser = currentUser.getUsername().toString();

// Locate TextView in welcome.xml
TextView txtuser = (TextView) findViewById(R.id.txtuser);

// Set the currentUser String into TextView
txtuser.setText("You are logged in as " + struser);

// Locate Button in welcome.xml
logout = (Button) findViewById(R.id.logout);

// Logout Button Click Listener
logout.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// Logout current user
ParseUser.logOut();
finish();
} }); } }

You might also like