├── README.md ├── android ├── .gitignore ├── .idea │ ├── compiler.xml │ ├── copyright │ │ └── profiles_settings.xml │ ├── encodings.xml │ ├── gradle.xml │ ├── misc.xml │ ├── modules.xml │ ├── runConfigurations.xml │ └── vcs.xml ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── test │ │ │ └── tuto_passport │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── test │ │ │ │ └── tuto_passport │ │ │ │ ├── FacebookManager.java │ │ │ │ ├── LoginActivity.java │ │ │ │ ├── MyApp.java │ │ │ │ ├── PostActivity.java │ │ │ │ ├── RegisterActivity.java │ │ │ │ ├── TokenManager.java │ │ │ │ ├── Utils.java │ │ │ │ ├── entities │ │ │ │ ├── AccessToken.java │ │ │ │ ├── ApiError.java │ │ │ │ ├── Post.java │ │ │ │ └── PostResponse.java │ │ │ │ └── network │ │ │ │ ├── ApiService.java │ │ │ │ ├── CustomAuthenticator.java │ │ │ │ └── RetrofitBuilder.java │ │ └── res │ │ │ ├── drawable │ │ │ ├── email_icon.xml │ │ │ ├── facebook_icon.xml │ │ │ ├── facebook_icon_dark.xml │ │ │ ├── facebook_selector.xml │ │ │ ├── lock_icon.xml │ │ │ └── person_icon.xml │ │ │ ├── layout │ │ │ ├── activity_login.xml │ │ │ ├── activity_post.xml │ │ │ └── activity_register.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── test │ │ └── tuto_passport │ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle └── laravel ├── .env.example ├── .gitattributes ├── .gitignore ├── app ├── Console │ └── Kernel.php ├── Exceptions │ └── Handler.php ├── Http │ ├── Controllers │ │ ├── Api │ │ │ ├── Auth │ │ │ │ ├── IssueTokenTrait.php │ │ │ │ ├── LoginController.php │ │ │ │ ├── RegisterController.php │ │ │ │ └── SocialAuthController.php │ │ │ └── PostController.php │ │ ├── Auth │ │ │ ├── ForgotPasswordController.php │ │ │ ├── LoginController.php │ │ │ ├── RegisterController.php │ │ │ └── ResetPasswordController.php │ │ └── Controller.php │ ├── Grant │ │ └── SocialGrant.php │ ├── Kernel.php │ └── Middleware │ │ ├── EncryptCookies.php │ │ ├── RedirectIfAuthenticated.php │ │ ├── TrimStrings.php │ │ └── VerifyCsrfToken.php ├── Post.php ├── Providers │ ├── AppServiceProvider.php │ ├── AuthServiceProvider.php │ ├── BroadcastServiceProvider.php │ ├── EventServiceProvider.php │ ├── RouteServiceProvider.php │ └── SocialAuthServiceProvider.php ├── SocialAccount.php └── User.php ├── artisan ├── bootstrap ├── app.php ├── autoload.php └── cache │ └── .gitignore ├── composer.json ├── composer.lock ├── config ├── app.php ├── auth.php ├── broadcasting.php ├── cache.php ├── database.php ├── filesystems.php ├── mail.php ├── queue.php ├── services.php ├── session.php └── view.php ├── database ├── .gitignore ├── factories │ └── ModelFactory.php ├── migrations │ ├── 2014_10_12_000000_create_users_table.php │ ├── 2014_10_12_100000_create_password_resets_table.php │ ├── 2017_06_17_162120_create_posts_table.php │ └── 2017_06_30_120120_create_social_accounts_table.php └── seeds │ └── DatabaseSeeder.php ├── package.json ├── phpunit.xml ├── public ├── .htaccess ├── css │ └── app.css ├── favicon.ico ├── index.php ├── js │ └── app.js ├── robots.txt └── web.config ├── readme.md ├── resources ├── assets │ ├── js │ │ ├── app.js │ │ ├── bootstrap.js │ │ └── components │ │ │ └── Example.vue │ └── sass │ │ ├── _variables.scss │ │ └── app.scss ├── lang │ └── en │ │ ├── auth.php │ │ ├── pagination.php │ │ ├── passwords.php │ │ └── validation.php └── views │ └── welcome.blade.php ├── routes ├── api.php ├── channels.php ├── console.php └── web.php ├── server.php ├── storage ├── app │ ├── .gitignore │ └── public │ │ └── .gitignore ├── framework │ ├── .gitignore │ ├── cache │ │ └── .gitignore │ ├── sessions │ │ └── .gitignore │ ├── testing │ │ └── .gitignore │ └── views │ │ └── .gitignore └── logs │ └── .gitignore ├── tests ├── CreatesApplication.php ├── Feature │ └── ExampleTest.php ├── TestCase.php └── Unit │ └── ExampleTest.php └── webpack.mix.js /README.md: -------------------------------------------------------------------------------- 1 | # Laravel + Passport For An Android App 2 | 3 | [Link for the tuto](https://www.youtube.com/playlist?list=PLEubh3Rmu4tn8xtkVcWnWOjQcGG4aeRK-) -------------------------------------------------------------------------------- /android/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /android/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /android/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /android/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /android/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /android/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /android/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /android/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /android/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /android/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /android/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | defaultConfig { 7 | applicationId "test.tuto_passport" 8 | minSdkVersion 16 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | compile 'com.android.support:appcompat-v7:25.3.1' 35 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 36 | compile 'com.squareup.retrofit2:retrofit:2.3.0' 37 | compile 'com.squareup.retrofit2:converter-moshi:2.3.0' 38 | compile 'com.android.support:design:25.4.0' 39 | compile 'com.jakewharton:butterknife:8.6.0' 40 | compile 'com.basgeekball:awesome-validation:2.0' 41 | compile 'com.facebook.stetho:stetho:1.5.0' 42 | compile 'com.facebook.stetho:stetho-okhttp3:1.5.0' 43 | compile 'com.facebook.android:facebook-android-sdk:[4,5)' 44 | testCompile 'junit:junit:4.12' 45 | testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1' 46 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' 47 | debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1' 48 | releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1' 49 | } 50 | -------------------------------------------------------------------------------- /android/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in E:\AndroidSDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /android/app/src/androidTest/java/test/tuto_passport/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("test.tuto_passport", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/FacebookManager.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | 7 | import com.facebook.AccessToken; 8 | import com.facebook.CallbackManager; 9 | import com.facebook.FacebookCallback; 10 | import com.facebook.FacebookException; 11 | import com.facebook.GraphRequest; 12 | import com.facebook.GraphResponse; 13 | import com.facebook.login.LoginManager; 14 | import com.facebook.login.LoginResult; 15 | 16 | import org.json.JSONException; 17 | import org.json.JSONObject; 18 | 19 | import java.util.Arrays; 20 | 21 | import retrofit2.Call; 22 | import retrofit2.Callback; 23 | import retrofit2.Response; 24 | import test.tuto_passport.network.ApiService; 25 | 26 | public class FacebookManager { 27 | 28 | private static final String TAG = "FacebookManager"; 29 | private static final String PROVIDER = "facebook"; 30 | 31 | public interface FacebookLoginListener{ 32 | void onSuccess(); 33 | void onError(String message); 34 | } 35 | 36 | private ApiService apiService; 37 | private TokenManager tokenManager; 38 | private CallbackManager callbackManager; 39 | private FacebookLoginListener listener; 40 | private Call call; 41 | 42 | private FacebookCallback facebookCallback = new FacebookCallback() { 43 | @Override 44 | public void onSuccess(LoginResult loginResult) { 45 | fetchUser(loginResult.getAccessToken()); 46 | } 47 | 48 | @Override 49 | public void onCancel() { 50 | 51 | } 52 | 53 | @Override 54 | public void onError(FacebookException error) { 55 | listener.onError(error.getMessage()); 56 | } 57 | }; 58 | 59 | public FacebookManager(ApiService apiService, TokenManager tokenManager){ 60 | 61 | this.apiService = apiService; 62 | this.tokenManager = tokenManager; 63 | this.callbackManager = CallbackManager.Factory.create(); 64 | LoginManager.getInstance().registerCallback(callbackManager, facebookCallback); 65 | 66 | } 67 | 68 | public void login(Activity activity, FacebookLoginListener listener){ 69 | this.listener = listener; 70 | 71 | if(AccessToken.getCurrentAccessToken() != null){ 72 | //Get the user 73 | fetchUser(AccessToken.getCurrentAccessToken()); 74 | }else{ 75 | LoginManager.getInstance().logInWithReadPermissions(activity, Arrays.asList("public_profile", "email")); 76 | } 77 | 78 | } 79 | 80 | private void fetchUser(AccessToken accessToken){ 81 | GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() { 82 | @Override 83 | public void onCompleted(JSONObject object, GraphResponse response) { 84 | 85 | try { 86 | String id = object.getString("id"); 87 | String name = object.getString("first_name"); 88 | String email = object.getString("email"); 89 | getTokenFromBackend(name, email, PROVIDER, id); 90 | } catch (JSONException e) { 91 | e.printStackTrace(); 92 | listener.onError(e.getMessage()); 93 | } 94 | 95 | } 96 | }); 97 | 98 | Bundle parameters = new Bundle(); 99 | parameters.putString("fields", "id, first_name, email"); 100 | request.setParameters(parameters); 101 | request.executeAsync(); 102 | } 103 | 104 | private void getTokenFromBackend(String name, String email, String provider, String providerUserId){ 105 | 106 | call = apiService.socialAuth(name, email, provider, providerUserId); 107 | call.enqueue(new Callback() { 108 | @Override 109 | public void onResponse(Call call, Response response) { 110 | if(response.isSuccessful()){ 111 | tokenManager.saveToken(response.body()); 112 | listener.onSuccess(); 113 | }else{ 114 | listener.onError("An error occured"); 115 | } 116 | } 117 | 118 | @Override 119 | public void onFailure(Call call, Throwable t) { 120 | listener.onError(t.getMessage()); 121 | } 122 | }); 123 | 124 | } 125 | 126 | public void onActivityResult(int requestCode, int resultCode, Intent data){ 127 | callbackManager.onActivityResult(requestCode, resultCode, data); 128 | } 129 | 130 | public void onDestroy(){ 131 | if(call != null){ 132 | call.cancel(); 133 | } 134 | call = null; 135 | if(callbackManager != null){ 136 | LoginManager.getInstance().unregisterCallback(callbackManager); 137 | } 138 | } 139 | 140 | public void clearSession(){ 141 | LoginManager.getInstance().logOut(); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/LoginActivity.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport; 2 | 3 | import android.content.Intent; 4 | import android.support.design.widget.TextInputLayout; 5 | import android.support.transition.TransitionManager; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.os.Bundle; 8 | import android.util.Log; 9 | import android.util.Patterns; 10 | import android.view.View; 11 | import android.widget.LinearLayout; 12 | import android.widget.ProgressBar; 13 | import android.widget.RelativeLayout; 14 | import android.widget.Toast; 15 | 16 | import com.basgeekball.awesomevalidation.AwesomeValidation; 17 | import com.basgeekball.awesomevalidation.ValidationStyle; 18 | import com.basgeekball.awesomevalidation.utility.RegexTemplate; 19 | 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | import butterknife.BindView; 24 | import butterknife.ButterKnife; 25 | import butterknife.OnClick; 26 | import okhttp3.ResponseBody; 27 | import retrofit2.Call; 28 | import retrofit2.Callback; 29 | import retrofit2.Response; 30 | import retrofit2.http.POST; 31 | import test.tuto_passport.entities.AccessToken; 32 | import test.tuto_passport.entities.ApiError; 33 | import test.tuto_passport.network.ApiService; 34 | import test.tuto_passport.network.RetrofitBuilder; 35 | 36 | public class LoginActivity extends AppCompatActivity { 37 | 38 | private static final String TAG = "LoginActivity"; 39 | 40 | @BindView(R.id.til_email) 41 | TextInputLayout tilEmail; 42 | @BindView(R.id.til_password) 43 | TextInputLayout tilPassword; 44 | @BindView(R.id.container) 45 | RelativeLayout container; 46 | @BindView(R.id.form_container) 47 | LinearLayout formContainer; 48 | @BindView(R.id.loader) 49 | ProgressBar loader; 50 | 51 | ApiService service; 52 | TokenManager tokenManager; 53 | AwesomeValidation validator; 54 | Call call; 55 | FacebookManager facebookManager; 56 | 57 | @Override 58 | protected void onCreate(Bundle savedInstanceState) { 59 | super.onCreate(savedInstanceState); 60 | setContentView(R.layout.activity_login); 61 | 62 | ButterKnife.bind(this); 63 | 64 | service = RetrofitBuilder.createService(ApiService.class); 65 | tokenManager = TokenManager.getInstance(getSharedPreferences("prefs", MODE_PRIVATE)); 66 | validator = new AwesomeValidation(ValidationStyle.TEXT_INPUT_LAYOUT); 67 | facebookManager = new FacebookManager(service, tokenManager); 68 | setupRules(); 69 | 70 | if(tokenManager.getToken().getAccessToken() != null){ 71 | startActivity(new Intent(LoginActivity.this, PostActivity.class)); 72 | finish(); 73 | } 74 | } 75 | 76 | private void showLoading(){ 77 | TransitionManager.beginDelayedTransition(container); 78 | formContainer.setVisibility(View.GONE); 79 | loader.setVisibility(View.VISIBLE); 80 | } 81 | 82 | private void showForm(){ 83 | TransitionManager.beginDelayedTransition(container); 84 | formContainer.setVisibility(View.VISIBLE); 85 | loader.setVisibility(View.GONE); 86 | } 87 | 88 | @OnClick(R.id.btn_facebook) 89 | void loginFacebook(){ 90 | showLoading(); 91 | facebookManager.login(this, new FacebookManager.FacebookLoginListener() { 92 | @Override 93 | public void onSuccess() { 94 | facebookManager.clearSession(); 95 | startActivity(new Intent(LoginActivity.this, PostActivity.class)); 96 | finish(); 97 | } 98 | 99 | @Override 100 | public void onError(String message) { 101 | showForm(); 102 | Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show(); 103 | } 104 | }); 105 | 106 | } 107 | 108 | @OnClick(R.id.btn_login) 109 | void login() { 110 | 111 | String email = tilEmail.getEditText().getText().toString(); 112 | String password = tilPassword.getEditText().getText().toString(); 113 | 114 | tilEmail.setError(null); 115 | tilPassword.setError(null); 116 | 117 | validator.clear(); 118 | 119 | if (validator.validate()) { 120 | showLoading(); 121 | call = service.login(email, password); 122 | call.enqueue(new Callback() { 123 | @Override 124 | public void onResponse(Call call, Response response) { 125 | 126 | Log.w(TAG, "onResponse: " + response); 127 | 128 | if (response.isSuccessful()) { 129 | tokenManager.saveToken(response.body()); 130 | startActivity(new Intent(LoginActivity.this, PostActivity.class)); 131 | finish(); 132 | } else { 133 | if (response.code() == 422) { 134 | handleErrors(response.errorBody()); 135 | } 136 | if (response.code() == 401) { 137 | ApiError apiError = Utils.converErrors(response.errorBody()); 138 | Toast.makeText(LoginActivity.this, apiError.getMessage(), Toast.LENGTH_LONG).show(); 139 | } 140 | showForm(); 141 | } 142 | 143 | } 144 | 145 | @Override 146 | public void onFailure(Call call, Throwable t) { 147 | Log.w(TAG, "onFailure: " + t.getMessage()); 148 | showForm(); 149 | } 150 | }); 151 | 152 | } 153 | 154 | } 155 | 156 | @OnClick(R.id.go_to_register) 157 | void goToRegister(){ 158 | startActivity(new Intent(LoginActivity.this, RegisterActivity.class)); 159 | } 160 | 161 | private void handleErrors(ResponseBody response) { 162 | 163 | ApiError apiError = Utils.converErrors(response); 164 | 165 | for (Map.Entry> error : apiError.getErrors().entrySet()) { 166 | if (error.getKey().equals("username")) { 167 | tilEmail.setError(error.getValue().get(0)); 168 | } 169 | if (error.getKey().equals("password")) { 170 | tilPassword.setError(error.getValue().get(0)); 171 | } 172 | } 173 | 174 | } 175 | 176 | public void setupRules() { 177 | 178 | validator.addValidation(this, R.id.til_email, Patterns.EMAIL_ADDRESS, R.string.err_email); 179 | validator.addValidation(this, R.id.til_password, RegexTemplate.NOT_EMPTY, R.string.err_password); 180 | } 181 | 182 | @Override 183 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 184 | super.onActivityResult(requestCode, resultCode, data); 185 | facebookManager.onActivityResult(requestCode, resultCode, data); 186 | } 187 | 188 | @Override 189 | protected void onDestroy() { 190 | super.onDestroy(); 191 | if (call != null) { 192 | call.cancel(); 193 | call = null; 194 | } 195 | facebookManager.onDestroy(); 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/MyApp.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport; 2 | 3 | import android.app.Application; 4 | 5 | import com.facebook.stetho.Stetho; 6 | import com.squareup.leakcanary.LeakCanary; 7 | 8 | public class MyApp extends Application { 9 | 10 | @Override 11 | public void onCreate() { 12 | super.onCreate(); 13 | Stetho.initializeWithDefaults(this); 14 | 15 | // if (LeakCanary.isInAnalyzerProcess(this)) { 16 | // // This process is dedicated to LeakCanary for heap analysis. 17 | // // You should not init your app in this process. 18 | // return; 19 | // } 20 | // LeakCanary.install(this); 21 | // Normal app init code... 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/PostActivity.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.widget.TextView; 8 | 9 | import butterknife.BindView; 10 | import butterknife.ButterKnife; 11 | import butterknife.OnClick; 12 | import retrofit2.Call; 13 | import retrofit2.Callback; 14 | import retrofit2.Response; 15 | import test.tuto_passport.entities.PostResponse; 16 | import test.tuto_passport.network.ApiService; 17 | import test.tuto_passport.network.RetrofitBuilder; 18 | 19 | public class PostActivity extends AppCompatActivity { 20 | 21 | private static final String TAG = "PostActivity"; 22 | 23 | @BindView(R.id.post_title) 24 | TextView title; 25 | 26 | ApiService service; 27 | TokenManager tokenManager; 28 | Call call; 29 | 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.activity_post); 35 | 36 | ButterKnife.bind(this); 37 | tokenManager = TokenManager.getInstance(getSharedPreferences("prefs", MODE_PRIVATE)); 38 | 39 | if(tokenManager.getToken() == null){ 40 | startActivity(new Intent(PostActivity.this, LoginActivity.class)); 41 | finish(); 42 | } 43 | 44 | service = RetrofitBuilder.createServiceWithAuth(ApiService.class, tokenManager); 45 | } 46 | 47 | @OnClick(R.id.btn_posts) 48 | void getPosts(){ 49 | 50 | call = service.posts(); 51 | call.enqueue(new Callback() { 52 | @Override 53 | public void onResponse(Call call, Response response) { 54 | Log.w(TAG, "onResponse: " + response ); 55 | 56 | if(response.isSuccessful()){ 57 | title.setText(response.body().getData().get(0).getTitle()); 58 | }else { 59 | tokenManager.deleteToken(); 60 | startActivity(new Intent(PostActivity.this, LoginActivity.class)); 61 | finish(); 62 | 63 | } 64 | } 65 | 66 | @Override 67 | public void onFailure(Call call, Throwable t) { 68 | Log.w(TAG, "onFailure: " + t.getMessage() ); 69 | } 70 | }); 71 | 72 | } 73 | 74 | @Override 75 | protected void onDestroy() { 76 | super.onDestroy(); 77 | if(call != null){ 78 | call.cancel(); 79 | call = null; 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/RegisterActivity.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport; 2 | 3 | import android.content.Intent; 4 | import android.support.design.widget.TextInputLayout; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | import android.util.Log; 8 | import android.util.Patterns; 9 | 10 | import com.basgeekball.awesomevalidation.AwesomeValidation; 11 | import com.basgeekball.awesomevalidation.ValidationStyle; 12 | import com.basgeekball.awesomevalidation.utility.RegexTemplate; 13 | 14 | import java.io.IOException; 15 | import java.lang.annotation.Annotation; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | import butterknife.BindView; 20 | import butterknife.ButterKnife; 21 | import butterknife.OnClick; 22 | import okhttp3.ResponseBody; 23 | import retrofit2.Call; 24 | import retrofit2.Callback; 25 | import retrofit2.Converter; 26 | import retrofit2.Response; 27 | import test.tuto_passport.entities.AccessToken; 28 | import test.tuto_passport.entities.ApiError; 29 | import test.tuto_passport.network.ApiService; 30 | import test.tuto_passport.network.RetrofitBuilder; 31 | 32 | public class RegisterActivity extends AppCompatActivity { 33 | 34 | private static final String TAG = "RegisterActivity"; 35 | 36 | @BindView(R.id.til_name) 37 | TextInputLayout tilName; 38 | @BindView(R.id.til_email) 39 | TextInputLayout tilEmail; 40 | @BindView(R.id.til_password) 41 | TextInputLayout tilPassword; 42 | 43 | ApiService service; 44 | Call call; 45 | AwesomeValidation validator; 46 | TokenManager tokenManager; 47 | 48 | @Override 49 | protected void onCreate(Bundle savedInstanceState) { 50 | super.onCreate(savedInstanceState); 51 | setContentView(R.layout.activity_register); 52 | 53 | ButterKnife.bind(this); 54 | 55 | service = RetrofitBuilder.createService(ApiService.class); 56 | validator = new AwesomeValidation(ValidationStyle.TEXT_INPUT_LAYOUT); 57 | tokenManager = TokenManager.getInstance(getSharedPreferences("prefs", MODE_PRIVATE)); 58 | setupRules(); 59 | 60 | if(tokenManager.getToken().getAccessToken() != null){ 61 | startActivity(new Intent(RegisterActivity.this, PostActivity.class)); 62 | finish(); 63 | } 64 | } 65 | 66 | @OnClick(R.id.btn_register) 67 | void register(){ 68 | 69 | String name = tilName.getEditText().getText().toString(); 70 | String email = tilEmail.getEditText().getText().toString(); 71 | String password = tilPassword.getEditText().getText().toString(); 72 | 73 | tilName.setError(null); 74 | tilEmail.setError(null); 75 | tilPassword.setError(null); 76 | 77 | validator.clear(); 78 | 79 | if(validator.validate()) { 80 | call = service.register(name, email, password); 81 | call.enqueue(new Callback() { 82 | @Override 83 | public void onResponse(Call call, Response response) { 84 | 85 | Log.w(TAG, "onResponse: " + response); 86 | 87 | if (response.isSuccessful()) { 88 | Log.w(TAG, "onResponse: " + response.body() ); 89 | tokenManager.saveToken(response.body()); 90 | startActivity(new Intent(RegisterActivity.this, PostActivity.class)); 91 | finish(); 92 | } else { 93 | handleErrors(response.errorBody()); 94 | } 95 | 96 | } 97 | 98 | @Override 99 | public void onFailure(Call call, Throwable t) { 100 | Log.w(TAG, "onFailure: " + t.getMessage()); 101 | } 102 | }); 103 | } 104 | } 105 | 106 | @OnClick(R.id.go_to_login) 107 | void goToRegister(){ 108 | startActivity(new Intent(RegisterActivity.this, LoginActivity.class)); 109 | } 110 | 111 | 112 | private void handleErrors(ResponseBody response){ 113 | 114 | ApiError apiError = Utils.converErrors(response); 115 | 116 | for(Map.Entry> error : apiError.getErrors().entrySet()){ 117 | if(error.getKey().equals("name")){ 118 | tilName.setError(error.getValue().get(0)); 119 | } 120 | if(error.getKey().equals("email")){ 121 | tilEmail.setError(error.getValue().get(0)); 122 | } 123 | if(error.getKey().equals("password")){ 124 | tilPassword.setError(error.getValue().get(0)); 125 | } 126 | } 127 | 128 | } 129 | 130 | public void setupRules(){ 131 | 132 | validator.addValidation(this, R.id.til_name, RegexTemplate.NOT_EMPTY, R.string.err_name); 133 | validator.addValidation(this, R.id.til_email, Patterns.EMAIL_ADDRESS, R.string.err_email); 134 | validator.addValidation(this, R.id.til_password, "[a-zA-Z0-9]{6,}", R.string.err_password); 135 | } 136 | 137 | @Override 138 | protected void onDestroy() { 139 | super.onDestroy(); 140 | if(call != null) { 141 | call.cancel(); 142 | call = null; 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/TokenManager.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport; 2 | 3 | import android.content.SharedPreferences; 4 | 5 | import test.tuto_passport.entities.AccessToken; 6 | 7 | public class TokenManager { 8 | 9 | private SharedPreferences prefs; 10 | private SharedPreferences.Editor editor; 11 | 12 | private static TokenManager INSTANCE = null; 13 | 14 | private TokenManager(SharedPreferences prefs){ 15 | this.prefs = prefs; 16 | this.editor = prefs.edit(); 17 | } 18 | 19 | static synchronized TokenManager getInstance(SharedPreferences prefs){ 20 | if(INSTANCE == null){ 21 | INSTANCE = new TokenManager(prefs); 22 | } 23 | return INSTANCE; 24 | } 25 | 26 | public void saveToken(AccessToken token){ 27 | editor.putString("ACCESS_TOKEN", token.getAccessToken()).commit(); 28 | editor.putString("REFRESH_TOKEN", token.getRefreshToken()).commit(); 29 | } 30 | 31 | public void deleteToken(){ 32 | editor.remove("ACCESS_TOKEN").commit(); 33 | editor.remove("REFRESH_TOKEN").commit(); 34 | } 35 | 36 | public AccessToken getToken(){ 37 | AccessToken token = new AccessToken(); 38 | token.setAccessToken(prefs.getString("ACCESS_TOKEN", null)); 39 | token.setRefreshToken(prefs.getString("REFRESH_TOKEN", null)); 40 | return token; 41 | } 42 | 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/Utils.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport; 2 | 3 | import java.io.IOException; 4 | import java.lang.annotation.Annotation; 5 | 6 | import okhttp3.ResponseBody; 7 | import retrofit2.Converter; 8 | import test.tuto_passport.entities.ApiError; 9 | import test.tuto_passport.network.RetrofitBuilder; 10 | 11 | public class Utils { 12 | 13 | public static ApiError converErrors(ResponseBody response){ 14 | Converter converter = RetrofitBuilder.getRetrofit().responseBodyConverter(ApiError.class, new Annotation[0]); 15 | 16 | ApiError apiError = null; 17 | 18 | try { 19 | apiError = converter.convert(response); 20 | } catch (IOException e) { 21 | e.printStackTrace(); 22 | } 23 | return apiError; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/entities/AccessToken.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport.entities; 2 | 3 | import com.squareup.moshi.Json; 4 | 5 | public class AccessToken { 6 | 7 | @Json(name = "token_type") 8 | String tokenType; 9 | @Json(name = "expires_in") 10 | int expiresIn; 11 | @Json(name = "access_token") 12 | String accessToken; 13 | @Json(name = "refresh_token") 14 | String refreshToken; 15 | 16 | public String getTokenType() { 17 | return tokenType; 18 | } 19 | 20 | public int getExpiresIn() { 21 | return expiresIn; 22 | } 23 | 24 | public String getAccessToken() { 25 | return accessToken; 26 | } 27 | 28 | public String getRefreshToken() { 29 | return refreshToken; 30 | } 31 | 32 | public void setTokenType(String tokenType) { 33 | this.tokenType = tokenType; 34 | } 35 | 36 | public void setExpiresIn(int expiresIn) { 37 | this.expiresIn = expiresIn; 38 | } 39 | 40 | public void setAccessToken(String accessToken) { 41 | this.accessToken = accessToken; 42 | } 43 | 44 | public void setRefreshToken(String refreshToken) { 45 | this.refreshToken = refreshToken; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/entities/ApiError.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport.entities; 2 | 3 | import com.squareup.moshi.Json; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class ApiError { 9 | 10 | String message; 11 | Map> errors; 12 | 13 | public String getMessage() { 14 | return message; 15 | } 16 | 17 | public Map> getErrors() { 18 | return errors; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/entities/Post.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport.entities; 2 | 3 | public class Post { 4 | 5 | int id; 6 | String title; 7 | String body; 8 | 9 | public int getId() { 10 | return id; 11 | } 12 | 13 | public void setId(int id) { 14 | this.id = id; 15 | } 16 | 17 | public String getTitle() { 18 | return title; 19 | } 20 | 21 | public void setTitle(String title) { 22 | this.title = title; 23 | } 24 | 25 | public String getBody() { 26 | return body; 27 | } 28 | 29 | public void setBody(String body) { 30 | this.body = body; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/entities/PostResponse.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport.entities; 2 | 3 | import java.util.List; 4 | 5 | public class PostResponse { 6 | 7 | List data; 8 | 9 | public List getData() { 10 | return data; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/network/ApiService.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport.network; 2 | 3 | import retrofit2.Call; 4 | import retrofit2.http.Field; 5 | import retrofit2.http.FormUrlEncoded; 6 | import retrofit2.http.GET; 7 | import retrofit2.http.POST; 8 | import test.tuto_passport.entities.AccessToken; 9 | import test.tuto_passport.entities.PostResponse; 10 | 11 | public interface ApiService { 12 | 13 | @POST("register") 14 | @FormUrlEncoded 15 | Call register(@Field("name") String name, @Field("email") String email, @Field("password") String password); 16 | 17 | @POST("login") 18 | @FormUrlEncoded 19 | Call login(@Field("username") String username, @Field("password") String password); 20 | 21 | @POST("social_auth") 22 | @FormUrlEncoded 23 | Call socialAuth(@Field("name") String name, 24 | @Field("email") String email, 25 | @Field("provider") String provider, 26 | @Field("provider_user_id") String providerUserId); 27 | 28 | @POST("refresh") 29 | @FormUrlEncoded 30 | Call refresh(@Field("refresh_token") String refreshToken); 31 | 32 | @GET("posts") 33 | Call posts(); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/network/CustomAuthenticator.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport.network; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.annotation.Nullable; 6 | 7 | import okhttp3.Authenticator; 8 | import okhttp3.Request; 9 | import okhttp3.Response; 10 | import okhttp3.Route; 11 | import retrofit2.Call; 12 | import test.tuto_passport.TokenManager; 13 | import test.tuto_passport.entities.AccessToken; 14 | 15 | public class CustomAuthenticator implements Authenticator { 16 | 17 | private TokenManager tokenManager; 18 | private static CustomAuthenticator INSTANCE; 19 | 20 | private CustomAuthenticator(TokenManager tokenManager){ 21 | this.tokenManager = tokenManager; 22 | } 23 | 24 | static synchronized CustomAuthenticator getInstance(TokenManager tokenManager){ 25 | if(INSTANCE == null){ 26 | INSTANCE = new CustomAuthenticator(tokenManager); 27 | } 28 | 29 | return INSTANCE; 30 | } 31 | 32 | 33 | @Nullable 34 | @Override 35 | public Request authenticate(Route route, Response response) throws IOException { 36 | 37 | if(responseCount(response) >= 3){ 38 | return null; 39 | } 40 | 41 | AccessToken token = tokenManager.getToken(); 42 | 43 | ApiService service = RetrofitBuilder.createService(ApiService.class); 44 | Call call = service.refresh(token.getRefreshToken() + "a"); 45 | retrofit2.Response res = call.execute(); 46 | 47 | if(res.isSuccessful()){ 48 | AccessToken newToken = res.body(); 49 | tokenManager.saveToken(newToken); 50 | 51 | return response.request().newBuilder().header("Authorization", "Bearer " + res.body().getAccessToken()).build(); 52 | }else{ 53 | return null; 54 | } 55 | } 56 | 57 | private int responseCount(Response response) { 58 | int result = 1; 59 | while ((response = response.priorResponse()) != null) { 60 | result++; 61 | } 62 | return result; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /android/app/src/main/java/test/tuto_passport/network/RetrofitBuilder.java: -------------------------------------------------------------------------------- 1 | package test.tuto_passport.network; 2 | 3 | import com.facebook.stetho.okhttp3.StethoInterceptor; 4 | 5 | import java.io.IOException; 6 | 7 | import okhttp3.Interceptor; 8 | import okhttp3.OkHttpClient; 9 | import okhttp3.Request; 10 | import okhttp3.Response; 11 | import retrofit2.Retrofit; 12 | import retrofit2.converter.moshi.MoshiConverterFactory; 13 | import test.tuto_passport.BuildConfig; 14 | import test.tuto_passport.TokenManager; 15 | 16 | public class RetrofitBuilder { 17 | 18 | private static final String BASE_URL = "http://192.168.0.10/tutos/tuto_passport/public/api/"; 19 | 20 | private final static OkHttpClient client = buildClient(); 21 | private final static Retrofit retrofit = buildRetrofit(client); 22 | 23 | private static OkHttpClient buildClient(){ 24 | OkHttpClient.Builder builder = new OkHttpClient.Builder() 25 | .addInterceptor(new Interceptor() { 26 | @Override 27 | public Response intercept(Chain chain) throws IOException { 28 | Request request = chain.request(); 29 | 30 | Request.Builder builder = request.newBuilder() 31 | .addHeader("Accept", "application/json") 32 | .addHeader("Connection", "close"); 33 | 34 | request = builder.build(); 35 | 36 | return chain.proceed(request); 37 | 38 | } 39 | }); 40 | 41 | if(BuildConfig.DEBUG){ 42 | builder.addNetworkInterceptor(new StethoInterceptor()); 43 | } 44 | 45 | return builder.build(); 46 | 47 | } 48 | 49 | private static Retrofit buildRetrofit(OkHttpClient client){ 50 | return new Retrofit.Builder() 51 | .baseUrl(BASE_URL) 52 | .client(client) 53 | .addConverterFactory(MoshiConverterFactory.create()) 54 | .build(); 55 | } 56 | 57 | public static T createService(Class service){ 58 | return retrofit.create(service); 59 | } 60 | 61 | public static T createServiceWithAuth(Class service, final TokenManager tokenManager){ 62 | 63 | OkHttpClient newClient = client.newBuilder().addInterceptor(new Interceptor() { 64 | @Override 65 | public Response intercept(Chain chain) throws IOException { 66 | 67 | Request request = chain.request(); 68 | 69 | Request.Builder builder = request.newBuilder(); 70 | 71 | if(tokenManager.getToken().getAccessToken() != null){ 72 | builder.addHeader("Authorization", "Bearer " + tokenManager.getToken().getAccessToken()); 73 | } 74 | request = builder.build(); 75 | return chain.proceed(request); 76 | } 77 | }).authenticator(CustomAuthenticator.getInstance(tokenManager)).build(); 78 | 79 | Retrofit newRetrofit = retrofit.newBuilder().client(newClient).build(); 80 | return newRetrofit.create(service); 81 | 82 | } 83 | 84 | public static Retrofit getRetrofit() { 85 | return retrofit; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /android/app/src/main/res/drawable/email_icon.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /android/app/src/main/res/drawable/facebook_icon.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /android/app/src/main/res/drawable/facebook_icon_dark.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /android/app/src/main/res/drawable/facebook_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /android/app/src/main/res/drawable/lock_icon.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /android/app/src/main/res/drawable/person_icon.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /android/app/src/main/res/layout/activity_login.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 17 | 18 | 24 | 25 | 31 | 32 | 38 | 39 | 40 | 45 | 46 | 54 | 55 | 56 | 57 | 63 | 64 | 73 | 74 | 75 | 76 | 77 | 86 | 87 |