Commit 466179888f434bd25cc4e6e0e8ab67bab0a29c37

Authored by Ali B
1 parent 3af52cec

Still working on login and tokens

Risiko/app/src/main/java/api/APILoginService.java
... ... @@ -16,6 +16,9 @@ import java.net.HttpURLConnection;
16 16 import java.net.URL;
17 17  
18 18 import activity.MainPage;
  19 +import retrofit2.Call;
  20 +import retrofit2.Callback;
  21 +import retrofit2.Response;
19 22  
20 23  
21 24 /**
... ... @@ -55,66 +58,90 @@ public class APILoginService extends IntentService {
55 58  
56 59 /**
57 60 * @param password Password to account to check
58   - * @param email User email.
  61 + * @param username Username.
59 62 * @throws NullPointerException
60 63 *
61 64 * This method makes connection to API and sends request to check password and email.
62 65 * If correct it will send back a token, and response cod 201.
63 66 * If not correct it will only send back a response code.
64 67 */
65   - private void handleActionLogin(String password, String email) throws NullPointerException {
66   -
67   - HttpURLConnection urlConnection = null;
68   - URL url = null;
69   - BufferedReader reader;
70   - OutputStreamWriter writer = null;
71   -
72   - try {
73   -
74   - // Sending the post request with email and password to the server.
75   - url = new URL(getString(R.string.api_login_link));
76   - urlConnection = (HttpURLConnection) url.openConnection();
77   -
78   - urlConnection.setRequestMethod("POST");
79   - urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
80   - urlConnection.setRequestProperty("charset", "utf-8");
81   - urlConnection.setDoOutput(true);
82   - writer = new OutputStreamWriter(urlConnection.getOutputStream());
83   - writer.write("email="+email+"&password="+password);
84   - writer.flush();
85   - writer.close();
86   -
87   - // The response code from the server.
88   - int responseCode = urlConnection.getResponseCode();
89   -
90   - // If correct email and password. Get the token from the server, encrypt it and store it in shared preferences.
91   - if(responseCode == 201) {
92   - reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
93   - String token = reader.readLine();
94   - reader.close();
95   -
96   - SharedPreferences sharedPref = getSharedPreferences(getString(R.string.packageName), Context.MODE_PRIVATE);
97   - SharedPreferences.Editor edit = sharedPref.edit();
98   - edit.clear();
99   - edit.putString(getString(R.string.preference_name_token), encryptToken(token));
100   - edit.commit();
101   - sendBroadcast(responseCode);
102   - }else{
103   - // In case of other answer than 201. Will be handled in the broadcast receiver.
104   - sendBroadcast(responseCode);
  68 + private void handleActionLogin(String password, String username) throws NullPointerException {
  69 +
  70 +
  71 + Api.get().login(password, username).enqueue(new Callback<String>() {
  72 + @Override
  73 + public void onResponse(Call<String> call, Response<String> response) {
  74 + BufferedReader reader;
  75 + if (response.code() == 202) {
  76 + String token = response.body();
  77 +
  78 + SharedPreferences sharedPref = getSharedPreferences(getString(R.string.packageName), Context.MODE_PRIVATE);
  79 + SharedPreferences.Editor edit = sharedPref.edit();
  80 + edit.clear();
  81 + edit.putString(getString(R.string.preference_name_token), encryptToken(token));
  82 + edit.commit();
  83 + sendBroadcast(response.code());
  84 + } else
  85 + sendBroadcast(response.code());
105 86 }
106 87  
107   - } catch (IOException e) {
108   - e.printStackTrace();
109   - }
  88 + @Override
  89 + public void onFailure(Call<String> call, Throwable t) {
  90 +
  91 + }
  92 + });
  93 +
  94 +// HttpURLConnection urlConnection = null;
  95 +// URL url = null;
  96 +// BufferedReader reader;
  97 +// OutputStreamWriter writer = null;
  98 +//
  99 +// try {
  100 +//
  101 +// // Sending the post request with email and password to the server.
  102 +// url = new URL(getString(R.string.api_login_link));
  103 +// urlConnection = (HttpURLConnection) url.openConnection();
  104 +//
  105 +// urlConnection.setRequestMethod("POST");
  106 +// urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  107 +// urlConnection.setRequestProperty("charset", "utf-8");
  108 +// urlConnection.setDoOutput(true);
  109 +// writer = new OutputStreamWriter(urlConnection.getOutputStream());
  110 +// writer.write("email="+username+"&password="+password);
  111 +// writer.flush();
  112 +// writer.close();
  113 +//
  114 +// // The response code from the server.
  115 +// int responseCode = urlConnection.getResponseCode();
  116 +//
  117 +// // If correct email and password. Get the token from the server, encrypt it and store it in shared preferences.
  118 +// if(responseCode == 201) {
  119 +// reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  120 +// String token = reader.readLine();
  121 +// reader.close();
  122 +//
  123 +// SharedPreferences sharedPref = getSharedPreferences(getString(R.string.packageName), Context.MODE_PRIVATE);
  124 +// SharedPreferences.Editor edit = sharedPref.edit();
  125 +// edit.clear();
  126 +// edit.putString(getString(R.string.preference_name_token), encryptToken(token));
  127 +// edit.commit();
  128 +// sendBroadcast(responseCode);
  129 +// }else{
  130 +// // In case of other answer than 201. Will be handled in the broadcast receiver.
  131 +// sendBroadcast(responseCode);
  132 +// }
  133 +//
  134 +// } catch (IOException e) {
  135 +// e.printStackTrace();
  136 +// }
110 137 }
111 138  
112 139 /**
113 140 * @param responseCode Response code from server
114   - * Code 201: Correct password and Username
  141 + * Code 202: Correct password and Username
115 142 * Code 403: Wrong username or password
116 143 * Code 401: Unautorhized user (user not activated)
117   - * Code 500: Error from php server
  144 + * Code 500: Error from server
118 145 * Code 400: Something went wrong with login
119 146 *
120 147 * Sends broadcast when connection finished.
... ...
Risiko/app/src/main/java/api/Endpoints.java
... ... @@ -33,7 +33,7 @@ public interface Endpoints {
33 33 //ØYVIN FIX
34 34 //String BASEURL ="http://risikoapi.harm.no/";
35 35  
36   - @POST
  36 + @POST("login")
37 37 Call<String> login(@Field("username") String username, @Field("password") String password);
38 38  
39 39 @GET("evaluation")
... ...
Risiko/app/src/main/res/values/strings.xml
... ... @@ -41,7 +41,7 @@
41 41 <string name="added_by">Lagt til av:</string>
42 42 <string name="api_login_link">http://192.168.84.67:3000/login</string>
43 43 <string name="encryption_key">0123456789abcdef0123456789abcdef0123456789abcdef</string>
44   - <string name="preference_name_token">api-token</string>
  44 + <string name="preference_name_token" translatable="false">api-token</string>
45 45 <string name="packageName">api</string>
46 46 <string name="something_went_wrong_with_login">Noe gikk galt</string>
47 47 <string name="server_error">Server feil</string>
... ...
restApi/.idea/workspace.xml
... ... @@ -2,18 +2,11 @@
2 2 <project version="4">
3 3 <component name="ChangeListManager">
4 4 <list default="true" id="0e20d303-9de8-4113-bfd9-019e72b304dc" name="Default" comment="">
5   - <change beforePath="$PROJECT_DIR$/../Risiko/.idea/caches/build_file_checksums.ser" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/.idea/caches/build_file_checksums.ser" afterDir="false" />
6   - <change beforePath="$PROJECT_DIR$/../Risiko/.idea/gradle.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/.idea/gradle.xml" afterDir="false" />
7   - <change beforePath="$PROJECT_DIR$/../Risiko/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/.idea/misc.xml" afterDir="false" />
8   - <change beforePath="$PROJECT_DIR$/../Risiko/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/.idea/modules.xml" afterDir="false" />
9   - <change beforePath="$PROJECT_DIR$/../Risiko/.idea/runConfigurations.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/.idea/runConfigurations.xml" afterDir="false" />
10   - <change beforePath="$PROJECT_DIR$/../Risiko/.idea/vcs.xml" beforeDir="false" />
11   - <change beforePath="$PROJECT_DIR$/../Risiko/app/src/main/java/activity/MainPage.java" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/app/src/main/java/activity/MainPage.java" afterDir="false" />
12   - <change beforePath="$PROJECT_DIR$/../Risiko/app/src/main/res/layout/activity_main_page.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/app/src/main/res/layout/activity_main_page.xml" afterDir="false" />
13   - <change beforePath="$PROJECT_DIR$/../Risiko/app/src/main/res/values/colors.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/app/src/main/res/values/colors.xml" afterDir="false" />
  5 + <change beforePath="$PROJECT_DIR$/../Risiko/app/src/main/java/api/APILoginService.java" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/app/src/main/java/api/APILoginService.java" afterDir="false" />
  6 + <change beforePath="$PROJECT_DIR$/../Risiko/app/src/main/java/api/Endpoints.java" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/app/src/main/java/api/Endpoints.java" afterDir="false" />
14 7 <change beforePath="$PROJECT_DIR$/../Risiko/app/src/main/res/values/strings.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Risiko/app/src/main/res/values/strings.xml" afterDir="false" />
15   - <change beforePath="$PROJECT_DIR$/../Thumbs.db" beforeDir="false" afterPath="$PROJECT_DIR$/../Thumbs.db" afterDir="false" />
16 8 <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
  9 + <change beforePath="$PROJECT_DIR$/api/routes/routes.js" beforeDir="false" afterPath="$PROJECT_DIR$/api/routes/routes.js" afterDir="false" />
17 10 </list>
18 11 <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
19 12 <option name="SHOW_DIALOG" value="false" />
... ... @@ -27,19 +20,20 @@
27 20 <counts>
28 21 <entry key="project.closed" value="2" />
29 22 <entry key="project.open.time.1" value="1" />
  23 + <entry key="project.open.time.10" value="1" />
30 24 <entry key="project.open.time.24" value="1" />
31 25 <entry key="project.open.time.31" value="1" />
32   - <entry key="project.opened" value="3" />
  26 + <entry key="project.opened" value="4" />
33 27 </counts>
34 28 </usages-collector>
35 29 <usages-collector id="statistics.file.extensions.edit">
36 30 <counts>
37   - <entry key="js" value="3212" />
  31 + <entry key="js" value="3248" />
38 32 </counts>
39 33 </usages-collector>
40 34 <usages-collector id="statistics.file.types.edit">
41 35 <counts>
42   - <entry key="JavaScript" value="3212" />
  36 + <entry key="JavaScript" value="3248" />
43 37 </counts>
44 38 </usages-collector>
45 39 <usages-collector id="statistics.file.types.open">
... ... @@ -61,8 +55,8 @@
61 55 <file pinned="false" current-in-tab="true">
62 56 <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
63 57 <provider selected="true" editor-type-id="text-editor">
64   - <state relative-caret-position="1809">
65   - <caret line="367" column="1" lean-forward="true" selection-start-line="367" selection-start-column="1" selection-end-line="367" selection-end-column="1" />
  58 + <state relative-caret-position="415">
  59 + <caret line="357" column="50" selection-start-line="357" selection-start-column="50" selection-end-line="357" selection-end-column="50" />
66 60 </state>
67 61 </provider>
68 62 </entry>
... ... @@ -111,7 +105,7 @@
111 105 </packageJsonPaths>
112 106 </component>
113 107 <component name="PhpWorkspaceProjectConfiguration" backward_compatibility_performed="true" />
114   - <component name="ProjectFrameBounds" extendedState="6">
  108 + <component name="ProjectFrameBounds" extendedState="7">
115 109 <option name="width" value="1920" />
116 110 <option name="height" value="1080" />
117 111 </component>
... ... @@ -120,6 +114,7 @@
120 114 <foldersAlwaysOnTop value="true" />
121 115 </navigator>
122 116 <panes>
  117 + <pane id="Scope" />
123 118 <pane id="ProjectPane">
124 119 <subPane>
125 120 <expand>
... ... @@ -148,7 +143,6 @@
148 143 <select />
149 144 </subPane>
150 145 </pane>
151   - <pane id="Scope" />
152 146 </panes>
153 147 </component>
154 148 <component name="PropertiesComponent">
... ... @@ -222,20 +216,21 @@
222 216 <workItem from="1542189792461" duration="7537000" />
223 217 <workItem from="1542881688157" duration="125000" />
224 218 <workItem from="1542894129000" duration="445000" />
225   - <workItem from="1543239135040" duration="874000" />
  219 + <workItem from="1543239135040" duration="1484000" />
  220 + <workItem from="1543323597689" duration="1776000" />
226 221 </task>
227 222 <servers />
228 223 </component>
229 224 <component name="TimeTrackingManager">
230   - <option name="totallyTimeSpent" value="30763000" />
  225 + <option name="totallyTimeSpent" value="33149000" />
231 226 </component>
232 227 <component name="ToolWindowManager">
233 228 <frame x="-8" y="-8" width="1936" height="1056" extended-state="6" />
234 229 <editor active="true" />
235 230 <layout>
236   - <window_info content_ui="combo" id="Project" order="0" sideWeight="0.69739693" visible="true" weight="0.19968387" />
  231 + <window_info content_ui="combo" id="Project" order="0" sideWeight="0.69631237" visible="true" weight="0.19968387" />
237 232 <window_info id="Structure" order="1" weight="0.25" />
238   - <window_info id="npm" order="2" sideWeight="0.30260304" side_tool="true" visible="true" weight="0.19968387" />
  233 + <window_info id="npm" order="2" sideWeight="0.30368763" side_tool="true" visible="true" weight="0.19968387" />
239 234 <window_info id="Favorites" order="3" side_tool="true" />
240 235 <window_info anchor="bottom" id="Message" order="0" />
241 236 <window_info anchor="bottom" id="Find" order="1" />
... ... @@ -271,8 +266,8 @@
271 266 </entry>
272 267 <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
273 268 <provider selected="true" editor-type-id="text-editor">
274   - <state relative-caret-position="1809">
275   - <caret line="367" column="1" lean-forward="true" selection-start-line="367" selection-start-column="1" selection-end-line="367" selection-end-column="1" />
  269 + <state relative-caret-position="415">
  270 + <caret line="357" column="50" selection-start-line="357" selection-start-column="50" selection-end-line="357" selection-end-column="50" />
276 271 </state>
277 272 </provider>
278 273 </entry>
... ...
restApi/api/routes/routes.js
... ... @@ -356,7 +356,7 @@ function setupTables(db) {
356 356 });
357 357  
358 358 db.execute("CREATE TABLE risk.activity(id uuid, title text, PRIMARY KEY(id));", function (err) {
359   - if (err) console.log("Failed to create table activity.\n" + err)
  359 + if (err) console.log("Failed to create table activity.\n" + err);
360 360 else {
361 361 insertActivity(db, 'Kjore bil');
362 362 insertActivity(db, 'Drikke alkohol');
... ...