Skip to content

Instantly share code, notes, and snippets.

@frankcash
Created August 26, 2016 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankcash/9da93ff0b16277c794c65fedb519128e to your computer and use it in GitHub Desktop.
Save frankcash/9da93ff0b16277c794c65fedb519128e to your computer and use it in GitHub Desktop.
package Bean;
import com.google.gson.annotations.Expose;
/**
* Created by foobar on 8/25/16.
*/
public class Local {
@Expose
public String id;
@Expose
public String name;
@Expose
public String pwd;
@Expose
public String email;
@Expose
public int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.example.foobar.heloeworldauth;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import API.UserAuthentication;
import Bean.Local;
import Bean.User;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* A login screen that offers login via email/password.
*/
public class LoginActivity extends AppCompatActivity implements Callback<User> {
private static final String TAG = "LoginActivity";
private static final int REQUEST_SIGNUP = 0;
@BindView(R.id.input_email) EditText _emailText;
@BindView(R.id.input_password) EditText _passwordText;
@BindView(R.id.btn_login) Button _loginButton;
@BindView(R.id.link_signup) TextView _signupLink;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
_loginButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
login();
}
});
_signupLink.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(getApplicationContext(), SignupActivity.class);
startActivityForResult(intent, REQUEST_SIGNUP);
}
});
}
public void login(){
Log.d(TAG, "Login");
if(!validate()){
onLoginFailed();
return;
}
_loginButton.setEnabled(false);
final ProgressDialog progressDialog =
new ProgressDialog(LoginActivity.this, R.style.AppTheme_Dark_Dialog);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Authenticating...");
progressDialog.show();
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
Local local = new Local();
local.setEmail(email);
local.setPwd(password);
User user = new User();
user.setLocal(local);
// TODO: Implement auth call here
// TODO: Testing auth call here
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd't'HH:mm:ssZ").create();
Retrofit retrofit = new Retrofit.Builder().baseUrl(UserAuthentication.ENDPOINT)
.addConverterFactory(GsonConverterFactory.create(gson)).build();
UserAuthentication userAuthentication = retrofit.create(UserAuthentication.class);
Call call = userAuthentication.createUser(user);
call.enqueue(this);
// new android.os.Handler().postDelayed(
// new Runnable(){
// public void run(){
// //TODO: on complete call either onLoginSuccess or onLoginFailed
// //TODO: just defaulting to onLoginSuccess right now.
// onLoginSuccess();
//
// progressDialog.dismiss();
// }
// }, 3000);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_SIGNUP){
if(resultCode == RESULT_OK){
// TODO: Implement successful signup logic here
// TODO: by default we just finish the activity and log them in automagically
this.finish();
}
}
}
@Override
public void onBackPressed(){
// Disable going back to MainActivity
moveTaskToBack(true);
}
public void onLoginSuccess(){
_loginButton.setEnabled(true);
finish();
}
public void onLoginFailed(){
Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
_loginButton.setEnabled(true);
}
public boolean validate(){
boolean valid = true;
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
if(email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()){
_emailText.setError("Enter a valid email address");
valid = false;
}else{
_emailText.setError(null);
}
if(password.isEmpty() || password.length()<4){
_passwordText.setError("Password is too short");
valid = false;
}else{
_passwordText.setError(null);
}
return valid;
}
// TODO: Testing auth here
@Override
public void onResponse(Call<User> call, Response<User> response) {
int STATUS = response.code();
if(STATUS == 200){
User user = response.body();
Local userInfo = user.getLocal();
Toast.makeText(this, "Got the user: " + userInfo.name , Toast.LENGTH_LONG).show();
onLoginSuccess();
}else{
Toast.makeText(this, "Did not work" + String.valueOf(STATUS), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Toast.makeText(this, "Nope", Toast.LENGTH_LONG).show();
}
}
package Bean;
/**
* Created by foobar on 8/25/16.
*/
public class User {
public String _id;
public Local local;
public Local getLocal() {
return local;
}
public void setLocal(Local local) {
this.local = local;
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
}
package API;
import Bean.User;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
/**
* Created by foobar on 8/25/16.
*/
public interface UserAuthentication {
String ENDPOINT = "";
@POST("/signup")
Call<User> createUser(@Body User user);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment