-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from martinlaizg/auth
Añadido JWT a peticiones y manejo de error También se han añadido cambios menores: Quitada conversión a base64 antes de hash Arreglado problema de dependencia de Repo-Service Arreglado dato km de distancia a sitio jugando Cambiada vista de preferencias Arreglada carga de login
- Loading branch information
Showing
45 changed files
with
565 additions
and
476 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 63 additions & 2 deletions
65
app/src/main/java/com/martinlaizg/geofind/data/access/api/RetrofitInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,90 @@ | ||
package com.martinlaizg.geofind.data.access.api; | ||
|
||
import android.app.Application; | ||
import android.content.SharedPreferences; | ||
import android.util.Log; | ||
|
||
import androidx.preference.PreferenceManager; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.martinlaizg.geofind.config.Preferences; | ||
import com.martinlaizg.geofind.data.access.api.service.exceptions.APIException; | ||
import com.martinlaizg.geofind.data.repository.RepositoryFactory; | ||
import com.martinlaizg.geofind.data.repository.UserRepository; | ||
import com.martinlaizg.geofind.utils.DateUtils; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
public class RetrofitInstance { | ||
|
||
private static final String BASE_URL = "https://geofind1.herokuapp.com/api/"; | ||
|
||
private static final String TAG = RetrofitInstance.class.getSimpleName(); | ||
|
||
private static Retrofit retrofitInstance; | ||
|
||
public static RestClient getRestClient() { | ||
private static String token; | ||
private static SharedPreferences sp; | ||
private static UserRepository userRepo; | ||
|
||
public static RestClient getRestClient(Application application) { | ||
if(userRepo == null) { | ||
userRepo = RepositoryFactory.getUserRepository(application); | ||
} | ||
if(sp == null) { | ||
sp = PreferenceManager.getDefaultSharedPreferences(application.getApplicationContext()); | ||
} | ||
Retrofit rf = getRetrofitInstance(); | ||
return rf.create(RestClient.class); | ||
} | ||
|
||
public static Retrofit getRetrofitInstance() { | ||
if(retrofitInstance == null) { | ||
|
||
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(chain -> { | ||
Request originalRequest = chain.request(); | ||
Request newRequest = originalRequest.newBuilder() | ||
.addHeader("Authorization", getBearerToken()) | ||
.method(originalRequest.method(), originalRequest.body()).build(); | ||
|
||
Response response = chain.proceed(newRequest); | ||
if(response.code() == 401) { // Code 401 (Unathorized) | ||
try { | ||
userRepo.reLogin(); | ||
} catch(APIException e) { | ||
Log.e(TAG, "Fail relogin", e); | ||
} | ||
newRequest = originalRequest.newBuilder() | ||
.addHeader("Authorization", getBearerToken()) | ||
.method(originalRequest.method(), originalRequest.body()).build(); | ||
response = chain.proceed(newRequest); | ||
} else { | ||
String newToken = response.header("Authorization"); | ||
if(newToken != null && !newToken.isEmpty()) { | ||
Preferences.setToken(sp, newToken); | ||
} | ||
} | ||
return response; | ||
}).build(); | ||
|
||
Gson gson = new GsonBuilder().setDateFormat(DateUtils.DATE_FORMAT).create(); | ||
retrofitInstance = new Retrofit.Builder().baseUrl(BASE_URL) | ||
retrofitInstance = new Retrofit.Builder().baseUrl(BASE_URL).client(okHttpClient) | ||
.addConverterFactory(GsonConverterFactory.create(gson)).build(); | ||
} | ||
|
||
return retrofitInstance; | ||
} | ||
|
||
private static String getBearerToken() { | ||
return "Bearer " + token; | ||
} | ||
|
||
public static void setToken(String token) { | ||
RetrofitInstance.token = token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/martinlaizg/geofind/data/access/api/service/ServiceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.martinlaizg.geofind.data.access.api.service; | ||
|
||
import android.app.Application; | ||
|
||
public class ServiceFactory { | ||
|
||
private static UserService userService; | ||
private static TourService tourService; | ||
private static PlayService playService; | ||
|
||
public static UserService getUserService(Application application) { | ||
if(userService == null) { | ||
userService = new UserService(); | ||
userService.instantiate(application); | ||
} | ||
return userService; | ||
} | ||
|
||
public static TourService getTourService(Application application) { | ||
if(tourService == null) { | ||
tourService = new TourService(); | ||
tourService.instantiate(application); | ||
} | ||
return tourService; | ||
} | ||
|
||
public static PlayService getPlayService(Application application) { | ||
if(playService == null) { | ||
playService = new PlayService(); | ||
playService.instantiate(application); | ||
} | ||
return playService; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.