Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

voorlopige versie login & register #167

Merged
merged 1 commit into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion WatchFriends/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
#}
6 changes: 3 additions & 3 deletions WatchFriends/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nmct.jaspernielsmichielhein.watchfriends">

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
package nmct.jaspernielsmichielhein.watchfriends.api;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONObject;

import java.util.ArrayList;

import nmct.jaspernielsmichielhein.watchfriends.model.Episode;
import nmct.jaspernielsmichielhein.watchfriends.model.Season;
import nmct.jaspernielsmichielhein.watchfriends.model.Series;
import nmct.jaspernielsmichielhein.watchfriends.model.SeriesList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import rx.Observable;

public interface WatchFriendsService {

// HOME LISTS
//REGISTER
@FormUrlEncoded
@POST("auth/register")
Call<JsonObject> register(@Field("email") String email, @Field("lastname") String lastname, @Field("firstname") String firstname, @Field("password") String password);

//LOGIN
@FormUrlEncoded
@POST("auth/login")
Call<JsonObject> login(@Field("email") String email, @Field("password") String password);

//HOME LISTS
@GET("list")
Observable<ArrayList<SeriesList>> getLists();

Expand All @@ -27,4 +47,5 @@ public interface WatchFriendsService {
//EPISODE
@GET("tv/{series}/season/{season}/episode/{episode}")
Observable<Episode> getEpisode(@Path("series") int seriesId, @Path("season") int season, @Path("episode") int episode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,32 @@ public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
return null;
if (!authTokenType.equals("access_token")) {
throw new IllegalArgumentException("Only access_token is available");
}

AccountManager accountManager = AccountManager.get(mContext);
String token = accountManager.peekAuthToken(account, authTokenType);
if (token == null) {
return createAuthenticatorActivityBundle(response);
}

return createAccessTokenBundle(account, token);
}

private Bundle createAccessTokenBundle(Account account, String token) {
Bundle reply = new Bundle();
reply.putString(AccountManager.KEY_AUTHTOKEN, token);
reply.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
reply.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
return reply;
}

@Override
public String getAuthTokenLabel(String authTokenType) {
if (authTokenType.equals("access_token")) {
return "Access token";
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public T call(Throwable throwable) {
}
}).subscribe(action);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
* Created by hein_ on 14-Nov-16.
*/

public class ApiMovieDbHelper extends ApiHelper {

private static Retrofit RETROFIT_INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,34 @@
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.audiofx.AcousticEchoCanceler;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.facebook.login.LoginManager;

import java.util.concurrent.TimeUnit;

public class AuthHelper {

private static Account mAccount;
private static AccountManager mAccountManager;
private static AccountAuthenticatorResponse mAccountAuthenticatorResponse;

public static String getUsername(Context context) {
public static String getEmail(Context context) {
mAccountManager = AccountManager.get(context);

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
return null;
}
Account[] accounts = mAccountManager.getAccountsByType(nmct.jaspernielsmichielhein.watchfriends.helper.Contract.ACCOUNT_TYPE);
Account[] accounts = mAccountManager.getAccountsByType(Contract.ACCOUNT_TYPE);

if (accounts.length > 0) {
return accounts[0].name;
Expand All @@ -32,6 +40,40 @@ public static String getUsername(Context context) {
}
}

public static String getAuthToken(Context context) {
mAccountManager = AccountManager.get(context);

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
return null;
}
Account[] accounts = mAccountManager.getAccountsByType(Contract.ACCOUNT_TYPE);

if (accounts.length > 0) {
mAccount = accounts[0];
try {
return new TokenTask().execute().get();
} catch (Exception e) {
Log.d("Error: ", e.getMessage());
return "";
}

} else {
return null;
}
}

private static class TokenTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
try {
return mAccountManager.blockingGetAuthToken(mAccount, "access_token", false);
} catch (Exception e) {
Log.d("Error: ", e.getMessage());
return "";
}
}
}

public static Boolean isUserLoggedIn(Context context) {
mAccountManager = AccountManager.get(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ public class Contract {
public static final String MOVIEDB_BASE_URL = "https://api.themoviedb.org/3/";
public static final String MOVIEDB_IMAGE_BASE_URL = "https://image.tmdb.org/t/p/w342/";


//AUTH
public static final String ACCOUNT_TYPE = "nmct.jaspernielsmichielhein.watchfriends.account";

//TODO: url aanpassen!
//WATCHFRIENDS
public static final String WATCHFRIENDS_BASE_URL = "https://www.watchfriends.me/api/";
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public interface onProfileSelectedListener {
}

public interface onAccountRegisteredListener {
void onAccountRegistered(String mUsername);
void onAccountRegistered(String email, String token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.util.Patterns;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
Expand Down Expand Up @@ -71,4 +72,12 @@ public void onClick(DialogInterface dialogInterface, int i) {
.show();
}

public static boolean isEmailValid(String email) {
return Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

public static boolean isPasswordValid(String password) {
return password.length() >= 8;
}

}
Loading