Commit e9c1caba authored by Sajal Narang's avatar Sajal Narang

Implement cookie-based authorization, fix #68

parent ec2307dd
...@@ -232,7 +232,7 @@ public class LoginActivity extends AppCompatActivity { ...@@ -232,7 +232,7 @@ public class LoginActivity extends AppCompatActivity {
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) { public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
Log.d(TAG, "Login request successful"); Log.d(TAG, "Login request successful");
session.createLoginSession(redirectURI, response.body().getUser()); session.createLoginSession(redirectURI, response.body().getUser(), response.body().getSessionID());
Intent i = new Intent(mContext, MainActivity.class); Intent i = new Intent(mContext, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
......
...@@ -48,6 +48,8 @@ import retrofit2.Call; ...@@ -48,6 +48,8 @@ import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import static in.ac.iitb.gymkhana.iitbapp.SessionManager.SESSION_ID;
public class MainActivity extends AppCompatActivity public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener { implements NavigationView.OnNavigationItemSelectedListener {
...@@ -249,6 +251,9 @@ public class MainActivity extends AppCompatActivity ...@@ -249,6 +251,9 @@ public class MainActivity extends AppCompatActivity
} }
private void updateFragment(Fragment fragment) { private void updateFragment(Fragment fragment) {
Bundle bundle = new Bundle();
bundle.putString(SESSION_ID, session.pref.getString(SESSION_ID, "Error"));
fragment.setArguments(bundle);
FragmentManager manager = getSupportFragmentManager(); FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction(); FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.framelayout_for_fragment, fragment, fragment.getTag()); transaction.replace(R.id.framelayout_for_fragment, fragment, fragment.getTag());
......
...@@ -13,6 +13,7 @@ public class SessionManager { ...@@ -13,6 +13,7 @@ public class SessionManager {
private static final String IS_LOGGED_IN = "IsLoggedIn"; private static final String IS_LOGGED_IN = "IsLoggedIn";
private static final String GCM_ID = "GcmId"; private static final String GCM_ID = "GcmId";
public static final String CURRENT_USER = "current_user"; public static final String CURRENT_USER = "current_user";
public static final String SESSION_ID = "session_id";
SharedPreferences pref; SharedPreferences pref;
Editor editor; Editor editor;
Context context; Context context;
...@@ -36,11 +37,12 @@ public class SessionManager { ...@@ -36,11 +37,12 @@ public class SessionManager {
} }
} }
public void createLoginSession(String gcmId, User currentUser) { public void createLoginSession(String gcmId, User currentUser, String sessionID) {
Log.d("SessionManager", "GcmId being stored"); Log.d("SessionManager", "GcmId being stored");
editor.putBoolean(IS_LOGGED_IN, true); editor.putBoolean(IS_LOGGED_IN, true);
editor.putString(GCM_ID, gcmId); editor.putString(GCM_ID, gcmId);
editor.putString(CURRENT_USER, currentUser.toString()); editor.putString(CURRENT_USER, currentUser.toString());
editor.putString(SESSION_ID, sessionID);
editor.commit(); editor.commit();
} }
......
...@@ -12,6 +12,7 @@ import in.ac.iitb.gymkhana.iitbapp.data.User; ...@@ -12,6 +12,7 @@ import in.ac.iitb.gymkhana.iitbapp.data.User;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.Body; import retrofit2.http.Body;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST; import retrofit2.http.POST;
import retrofit2.http.Path; import retrofit2.http.Path;
import retrofit2.http.Query; import retrofit2.http.Query;
...@@ -21,17 +22,17 @@ public interface RetrofitInterface { ...@@ -21,17 +22,17 @@ public interface RetrofitInterface {
Call<LoginResponse> login(@Query("code") String AUTH_CODE, @Query("redir") String redirectURI, @Query("fcm_id") String fcmID); Call<LoginResponse> login(@Query("code") String AUTH_CODE, @Query("redir") String redirectURI, @Query("fcm_id") String fcmID);
@POST("events") @POST("events")
Call<EventCreateResponse> createEvent(@Body EventCreateRequest eventCreateRequest); Call<EventCreateResponse> createEvent(@Header("Cookie") String sessionId, @Body EventCreateRequest eventCreateRequest);
@GET("users/{uuid}/followed_bodies_events") @GET("events")
Call<NewsFeedResponse> getNewsFeed(@Path("uuid") String uuid); Call<NewsFeedResponse> getNewsFeed(@Header("Cookie") String sessionId);
@GET("users/{uuid}") @GET("users/{uuid}")
Call<User> getUser(@Path("uuid") String uuid); Call<User> getUser(@Header("Cookie") String sessionId, @Path("uuid") String uuid);
@POST("getNotifications/") @POST("getNotifications/")
Call<NotificationsResponse> getNotifications(@Body NotificationsRequest notificationsRequest); Call<NotificationsResponse> getNotifications(@Body NotificationsRequest notificationsRequest);
@POST("upload") @POST("upload")
Call<ImageUploadResponse> uploadImage(@Body ImageUploadRequest imageUploadRequest); Call<ImageUploadResponse> uploadImage(@Header("Cookie") String sessionId, @Body ImageUploadRequest imageUploadRequest);
} }
...@@ -47,6 +47,7 @@ import retrofit2.Response; ...@@ -47,6 +47,7 @@ import retrofit2.Response;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
import static android.content.ContentValues.TAG; import static android.content.ContentValues.TAG;
import static in.ac.iitb.gymkhana.iitbapp.SessionManager.SESSION_ID;
public class AddEventFragment extends Fragment { public class AddEventFragment extends Fragment {
...@@ -234,7 +235,7 @@ public class AddEventFragment extends Fragment { ...@@ -234,7 +235,7 @@ public class AddEventFragment extends Fragment {
progressDialog.setMessage("Uploading Image"); progressDialog.setMessage("Uploading Image");
ImageUploadRequest imageUploadRequest = new ImageUploadRequest(base64Image); ImageUploadRequest imageUploadRequest = new ImageUploadRequest(base64Image);
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class); RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.uploadImage(imageUploadRequest).enqueue(new Callback<ImageUploadResponse>() { retrofitInterface.uploadImage("sessionid=" + getArguments().getString(SESSION_ID), imageUploadRequest).enqueue(new Callback<ImageUploadResponse>() {
@Override @Override
public void onResponse(Call<ImageUploadResponse> call, Response<ImageUploadResponse> response) { public void onResponse(Call<ImageUploadResponse> call, Response<ImageUploadResponse> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
...@@ -255,7 +256,7 @@ public class AddEventFragment extends Fragment { ...@@ -255,7 +256,7 @@ public class AddEventFragment extends Fragment {
progressDialog.setMessage("Creating Event"); progressDialog.setMessage("Creating Event");
EventCreateRequest eventCreateRequest = new EventCreateRequest(eventName.getText().toString(), details.getText().toString(), eventImageURL, timestamp_start.toString(), timestamp_end.toString(), false, Arrays.asList(new String[]{venue.getText().toString()}), Arrays.asList(new String[]{"bde82d5e-f379-4b8a-ae38-a9f03e4f1c4a"})); EventCreateRequest eventCreateRequest = new EventCreateRequest(eventName.getText().toString(), details.getText().toString(), eventImageURL, timestamp_start.toString(), timestamp_end.toString(), false, Arrays.asList(new String[]{venue.getText().toString()}), Arrays.asList(new String[]{"bde82d5e-f379-4b8a-ae38-a9f03e4f1c4a"}));
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class); RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.createEvent(eventCreateRequest).enqueue(new Callback<EventCreateResponse>() { retrofitInterface.createEvent("sessionid=" + getArguments().getString(SESSION_ID), eventCreateRequest).enqueue(new Callback<EventCreateResponse>() {
@Override @Override
public void onResponse(Call<EventCreateResponse> call, Response<EventCreateResponse> response) { public void onResponse(Call<EventCreateResponse> call, Response<EventCreateResponse> response) {
Toast.makeText(getContext(), "Event Created", Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), "Event Created", Toast.LENGTH_SHORT).show();
......
...@@ -58,6 +58,7 @@ public class CalendarFragment extends Fragment { ...@@ -58,6 +58,7 @@ public class CalendarFragment extends Fragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
AddEventFragment addEventFragment = new AddEventFragment(); AddEventFragment addEventFragment = new AddEventFragment();
addEventFragment.setArguments(getArguments());
FragmentTransaction ft = getChildFragmentManager().beginTransaction(); FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.calendar_layout, addEventFragment); ft.replace(R.id.calendar_layout, addEventFragment);
ft.addToBackStack("addEvent"); ft.addToBackStack("addEvent");
......
...@@ -34,6 +34,8 @@ import retrofit2.Call; ...@@ -34,6 +34,8 @@ import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import static in.ac.iitb.gymkhana.iitbapp.SessionManager.SESSION_ID;
/** /**
* A simple {@link Fragment} subclass. * A simple {@link Fragment} subclass.
*/ */
...@@ -94,10 +96,8 @@ public class FeedFragment extends Fragment { ...@@ -94,10 +96,8 @@ public class FeedFragment extends Fragment {
} }
private void updateFeed() { private void updateFeed() {
//TODO: Fetch userID from SharedPreferences
String userID = "51e04db1-040f-406c-8b6f-0c47a1bdc5a4";
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class); RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.getNewsFeed(userID).enqueue(new Callback<NewsFeedResponse>() { retrofitInterface.getNewsFeed("sessionid=" + getArguments().getString(SESSION_ID)).enqueue(new Callback<NewsFeedResponse>() {
@Override @Override
public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) { public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
......
...@@ -20,6 +20,8 @@ import retrofit2.Call; ...@@ -20,6 +20,8 @@ import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import static in.ac.iitb.gymkhana.iitbapp.SessionManager.SESSION_ID;
/** /**
* A simple {@link Fragment} subclass. * A simple {@link Fragment} subclass.
*/ */
...@@ -45,7 +47,7 @@ public class ProfileFragment extends Fragment { ...@@ -45,7 +47,7 @@ public class ProfileFragment extends Fragment {
String userID = bundle.getString(Constants.USER_ID); String userID = bundle.getString(Constants.USER_ID);
RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class); RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
retrofitInterface.getUser(userID).enqueue(new Callback<User>() { retrofitInterface.getUser("sessionid=" + getArguments().getString(SESSION_ID), userID).enqueue(new Callback<User>() {
@Override @Override
public void onResponse(Call<User> call, Response<User> response) { public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
......
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