Commit ccfde966 authored by Sajal Narang's avatar Sajal Narang Committed by GitHub

Merge pull request #130 from pulsejet/badlogin

Use new password login API
parents dbf57e30 9075f6b6
......@@ -4,14 +4,16 @@ package in.ac.iitb.gymkhana.iitbapp;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
......@@ -27,9 +29,6 @@ import retrofit2.Response;
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private final String redirectUri = "https://redirecturi";
private final String guestUri = "https://guesturi";
public String authCode = null;
SessionManager session;
Context mContext = this;
private ProgressDialog progressDialog;
......@@ -46,21 +45,52 @@ public class LoginActivity extends AppCompatActivity {
protected void onStart() {
super.onStart();
WebView webview = (WebView) findViewById(R.id.login_webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setWebViewClient(new WvClient());
webview.loadUrl("file:///android_asset/login.html");
final Button loginButton = findViewById(R.id.login_button);
final EditText usernameEditText = findViewById(R.id.login_username);
final EditText passwordEditText = findViewById(R.id.login_password);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!progressDialog.isShowing()) {
progressDialog.setMessage("Logging In");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
login(usernameEditText.getText().toString(), passwordEditText.getText().toString());
}
});
passwordEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
passwordEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
loginButton.performClick();
}
return false;
}
});
private void login(String authorizationCode, final String redirectURI, String gcmID) {
final TextView guestView = findViewById(R.id.login_guest);
guestView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
private void login(final String username, final String password) {
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.login(authorizationCode, redirectURI, gcmID).enqueue(new Callback<LoginResponse>() {
retrofitInterface.passwordLogin(username, password).enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if (response.isSuccessful()) {
Log.d(TAG, "Login request successful");
session.createLoginSession(redirectURI, response.body().getUser(), response.body().getSessionID());
session.createLoginSession(username, response.body().getUser(), response.body().getSessionID());
Intent i = new Intent(mContext, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
......@@ -68,6 +98,9 @@ public class LoginActivity extends AppCompatActivity {
progressDialog.dismiss();
//Save credentials in AccountManager to keep user logged in
//Go to MainActivity
} else {
Toast.makeText(LoginActivity.this, "Authorization Failed!", Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
//Server error
}
......@@ -95,56 +128,5 @@ public class LoginActivity extends AppCompatActivity {
return true;
}
private class WvClient extends WebViewClient {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
/* Capture redirect */
if (url.startsWith(redirectUri)) {
/* Show progress dialog */
progressDialog.setMessage("Logging In");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
if (!progressDialog.isShowing()) {
progressDialog.show();
}
/* Get auth code from query */
String query = Uri.parse(url).getQuery();
authCode = query.substring(query.lastIndexOf("=") + 1);
login(authCode, redirectUri, authCode);
return true;
}
/* Guest Login */
if (url.startsWith(guestUri)) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
if (!progressDialog.isShowing()) {
progressDialog.setMessage("Loading");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
/* Load URL */
view.loadUrl(url);
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
}
......@@ -25,6 +25,9 @@ public interface RetrofitInterface {
@GET("login")
Call<LoginResponse> login(@Query("code") String AUTH_CODE, @Query("redir") String redirectURI, @Query("fcm_id") String fcmID);
@GET("pass-login")
Call<LoginResponse> passwordLogin(@Query("username") String username, @Query("password") String password);
@POST("events")
Call<EventCreateResponse> createEvent(@Header("Cookie") String sessionId, @Body EventCreateRequest eventCreateRequest);
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:orientation="vertical"
android:weightSum="2">
<WebView
android:id="@+id/login_webview"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"></WebView>
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="199dp"
android:contentDescription="InstiApp"
android:paddingLeft="100dp"
android:paddingRight="100dp"
android:layout_marginBottom="15dp"
android:scaleType="fitEnd"
app:srcCompat="@drawable/lotus" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="InstiApp"
android:textAlignment="center"
android:textSize="28sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:layout_weight="1">
<EditText
android:id="@+id/login_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="LDAP ID" />
<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:hint="Password" />
<Button
android:id="@+id/login_button"
style="@style/Widget.AppCompat.Button.Colored"
android:textColor="@color/primaryTextColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Log In" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="or"
android:textAlignment="center" />
<TextView
android:id="@+id/login_guest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Continue as a Guest"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Subtitle"
android:paddingTop="5dp" />
</LinearLayout>
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment