Skip to content

Commit

Permalink
* NetworkUtils#fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
vemarav committed Apr 11, 2018
1 parent 0428e3e commit fb79c2e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 15 deletions.
19 changes: 15 additions & 4 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))


apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

Expand All @@ -22,19 +27,25 @@ android {
}

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.authflow"
applicationId "com.aravind.authflow"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}

buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
Expand Down
4 changes: 4 additions & 0 deletions android/key.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
storePassword=aravind
keyPassword=aravind
keyAlias=key
storeFile=/home/aravind/key.jks
5 changes: 3 additions & 2 deletions lib/app/auth_flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ class AuthFlow extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Authentication Flow',
title: 'Authentication Flow',
theme: new ThemeData(
primaryColor: Colors.green.shade500,
textSelectionColor: Colors.green.shade500,
buttonColor: Colors.green.shade500,
accentColor: Colors.green.shade500
accentColor: Colors.green.shade500,
bottomAppBarColor: Colors.white
),
home: new LoginPage(),
routes: {
Expand Down
33 changes: 30 additions & 3 deletions lib/app/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class _HomePageState extends State<HomePage> {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

SharedPreferences _sharedPreferences;
var _authToken, _id, _name;
var _authToken, _id, _name, _homeResponse;

@override
void initState() {
Expand All @@ -32,17 +32,44 @@ class _HomePageState extends State<HomePage> {
String authToken = AuthUtils.getToken(_sharedPreferences);
var id = _sharedPreferences.getInt(AuthUtils.userIdKey);
var name = _sharedPreferences.getString(AuthUtils.nameKey);

print(authToken);

_fetchHome(authToken);

setState(() {
_authToken = authToken;
_id = id;
_name = name;
});
print(_authToken);

if(_authToken == null) {
_logout();
}
}

_fetchHome(String authToken) async {
var responseJson = await NetworkUtils.fetch(authToken, '/api/v1/home');

if(responseJson == null) {

NetworkUtils.showSnackBar(_scaffoldKey, 'Something went wrong!');

} else if(responseJson == 'NetworkError') {

NetworkUtils.showSnackBar(_scaffoldKey, null);

} else if(responseJson['errors'] != null) {

_logout();

}

setState(() {
_homeResponse = responseJson.toString();
});
}

_logout() {
NetworkUtils.logoutUser(_scaffoldKey.currentContext, _sharedPreferences);
}
Expand All @@ -62,7 +89,7 @@ class _HomePageState extends State<HomePage> {
new Container(
padding: const EdgeInsets.all(8.0),
child: new Text(
"USER_ID: $_id \nUSER_NAME: $_name",
"USER_ID: $_id \nUSER_NAME: $_name \nHOME_RESPONSE: $_homeResponse",
style: new TextStyle(
fontSize: 24.0,
color: Colors.grey.shade700
Expand Down
10 changes: 10 additions & 0 deletions lib/app/pages/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,31 @@ class LoginPageState extends State<LoginPage> {
var responseJson = await NetworkUtils.authenticateUser(
_emailController.text, _passwordController.text
);

print(responseJson);

if(responseJson == null) {

NetworkUtils.showSnackBar(_scaffoldKey, 'Something went wrong!');

} else if(responseJson == 'NetworkError') {

NetworkUtils.showSnackBar(_scaffoldKey, null);

} else if(responseJson['errors'] != null) {

NetworkUtils.showSnackBar(_scaffoldKey, 'Invalid Email/Password');

} else {

AuthUtils.insertDetails(_sharedPreferences, responseJson);
/**
* Removes stack and start with the new page.
* In this case on press back on HomePage app will exit.
* **/
Navigator.of(_scaffoldKey.currentContext)
.pushReplacementNamed(HomePage.routeName);

}
_hideLoading();
} else {
Expand Down
37 changes: 31 additions & 6 deletions lib/app/utils/network_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
Expand All @@ -14,11 +13,13 @@ class NetworkUtils {
var uri = host + AuthUtils.endPoint;

try {
final response = await http.post(uri, body: {
'email': email,
'password': password,
'authenticate': 'user'
});
final response = await http.post(
uri,
body: {
'email': email,
'password': password
}
);

final responseJson = json.decode(response.body);
return responseJson;
Expand Down Expand Up @@ -47,4 +48,28 @@ class NetworkUtils {
)
);
}

static fetch(var authToken, var endPoint) async {
var uri = host + endPoint;

try {
final response = await http.get(
uri,
headers: {
'Authorization': authToken
},
);

final responseJson = json.decode(response.body);
return responseJson;

} catch (exception) {
print(exception);
if(exception.toString().contains('SocketException')) {
return 'NetworkError';
} else {
return null;
}
}
}
}

0 comments on commit fb79c2e

Please sign in to comment.