Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AuthenticationActivity extends BaseActivity implements View.OnClickListener {
- EditText username, password;
- Button signInButton, registerButton;
- public static void startActivity(Activity startingActivity) {
- startingActivity.startActivity(new Intent(startingActivity, AuthenticationActivity.class));
- startingActivity.finish();
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Hasura.initialise(this);
- setContentView(R.layout.activity_authentication);
- username = (EditText) findViewById(R.id.username);
- password = (EditText) findViewById(R.id.password);
- signInButton = (Button) findViewById(R.id.signInButton);
- registerButton = (Button) findViewById(R.id.registerButton);
- signInButton.setOnClickListener(this);
- registerButton.setOnClickListener(this);
- if (Hasura.getUserSessionId() != null) {
- ToDoActivity.startActivity(this);
- }
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.signInButton:
- handleLogin();
- break;
- case R.id.registerButton:
- handleRegistration();
- break;
- }
- }
- private void handleLogin() {
- showProgressIndicator();
- Hasura.auth.login(new AuthRequest(username.getText().toString(), password.getText().toString())).enqueue(new Callback<AuthResponse>() {
- @Override
- public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) {
- hideProgressIndicator();
- if (response.isSuccessful()) {
- Hasura.setSession(response.body());
- ToDoActivity.startActivity(AuthenticationActivity.this);
- } else {
- try {
- MessageResponse messageResponse = new Gson().fromJson(response.errorBody().string(), MessageResponse.class);
- Toast.makeText(AuthenticationActivity.this, messageResponse.getMessage(), Toast.LENGTH_LONG).show();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onFailure(Call<AuthResponse> call, Throwable t) {
- hideProgressIndicator();
- Toast.makeText(AuthenticationActivity.this, "Something went wrong, please ensure that you have a working internet connection", Toast.LENGTH_LONG).show();
- }
- });
- }
- private void handleRegistration() {
- showProgressIndicator();
- Hasura.auth.register(new AuthRequest(username.getText().toString(), password.getText().toString())).enqueue(new Callback<AuthResponse>() {
- @Override
- public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) {
- hideProgressIndicator();
- if (response.isSuccessful()) {
- Hasura.setSession(response.body());
- ToDoActivity.startActivity(AuthenticationActivity.this);
- } else {
- try {
- MessageResponse messageResponse = new Gson().fromJson(response.errorBody().string(), MessageResponse.class);
- Toast.makeText(AuthenticationActivity.this, messageResponse.getMessage(), Toast.LENGTH_LONG).show();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onFailure(Call<AuthResponse> call, Throwable t) {
- hideProgressIndicator();
- Toast.makeText(AuthenticationActivity.this, "Something went wrong, please ensure that you have a working internet connection", Toast.LENGTH_LONG).show();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement