Skip to content

Commit

Permalink
Merge branch 'auth' of https://github.com/rockspore/pal-finder into expr
Browse files Browse the repository at this point in the history
  • Loading branch information
rockspore committed Jan 2, 2020
2 parents a69bdaf + be60380 commit 8901784
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 23 deletions.
36 changes: 20 additions & 16 deletions lib/core/networking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import 'package:http/http.dart' as http;
class Networking {
Networking._internal() {
if (Platform.isIOS) {
_host = 'http://localhost:8000/';
_host = 'http://localhost:8000';
} else if (Platform.isAndroid) {
_host = 'http://10.0.2.2:8000/';
_host = 'http://10.0.2.2:8000';
}
}

Expand All @@ -31,22 +31,26 @@ class Networking {
loginUser(String username, String password) async {
String loginUrl;
if (Platform.isIOS) {
loginUrl = '${host}api-token/';
loginUrl = '$host/api-token/';
} else if (Platform.isAndroid) {
loginUrl = '${host}api-token/';
loginUrl = '$host/api-token/';
}
final response = await http.post(
loginUrl,
body: {
'username': username,
'password': password,
},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
_authToken = data['token'];
} else {
throw Exception('Failed login: ${response.statusCode}.');
try {
final response = await http.post(
loginUrl,
body: {
'username': username,
'password': password,
},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
_authToken = data['token'];
} else {
throw('Failed to login: ${response.statusCode}.');
}
} catch(err) {
throw('Failed to connect: $err');
}
}

Expand Down
56 changes: 53 additions & 3 deletions lib/screens/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ class LoginScreen extends StatelessWidget {
return WillPopScope(
onWillPop: () {
if(Navigator.canPop(context)) {
Navigator.of(context).pushNamedAndRemoveUntil(
Navigator.pushNamedAndRemoveUntil(
context,
'/home',
(Route<dynamic> route) => false,
);
} else {
Navigator.of(context).pushReplacementNamed('/home');
Navigator.pushReplacementNamed(context, '/home');
}
},
child: Scaffold(
Expand Down Expand Up @@ -54,7 +55,15 @@ class LoginScreen extends StatelessWidget {
onPressed: () {
SystemChannels.textInput.invokeMethod('TextInput.hide');
// TODO: Implement login and get token method
Networking().loginUser(_usernameController.text, _passwordController.text);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => _LoadingScreen(
_usernameController.text,
_passwordController.text
),
),
);
},
child: Text("LOGIN",
style: TextStyle(color: Colors.white,
Expand All @@ -73,4 +82,45 @@ class LoginScreen extends StatelessWidget {
),
);
}
}

class _LoadingScreen extends StatelessWidget {
_LoadingScreen(this._username, this._password);

final String _username;
final String _password;

@override
Widget build(BuildContext context) {
_loginUser(context);
return Scaffold(
body: Container(
decoration: BoxDecoration(color: Colors.black),
child: Column(
children: <Widget>[
Expanded(child:
Container(decoration: BoxDecoration(color: Colors.black),
alignment: FractionalOffset(0.5, 0.3),
child: Text("Loading...", style: TextStyle(fontSize: 40.0, color: Colors.white),),
),
),
],
),
),
);
}

void _loginUser(BuildContext context) async {
try {
await Networking().loginUser(_username, _password);
Navigator.pushNamedAndRemoveUntil(
context,
'/home',
(Route<dynamic> route) => false,
);
} catch(err) {
print('Error caught: $err');
Navigator.pop(context);
}
}
}
8 changes: 4 additions & 4 deletions lib/screens/splash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter/services.dart';
class SplashScreen extends StatelessWidget {
final int splashDuration = 2;

startTimer(BuildContext context) async {
_startTimer(BuildContext context) async {
return Timer(
Duration(seconds: splashDuration),
() {
Expand All @@ -17,7 +17,7 @@ class SplashScreen extends StatelessWidget {

@override
Widget build(BuildContext context) {
startTimer(context);
_startTimer(context);
return Scaffold(
body: Container(
decoration: BoxDecoration(color: Colors.black),
Expand All @@ -34,8 +34,8 @@ class SplashScreen extends StatelessWidget {
),
),
],
)
)
),
),
);
}
}

0 comments on commit 8901784

Please sign in to comment.