mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-22 21:45:48 +00:00
User sessions are now passed to the backend
This commit is contained in:
parent
f4d0527a3d
commit
7a9723bb34
|
@ -3,19 +3,23 @@ import 'package:http/http.dart' as http;
|
|||
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
|
||||
|
||||
class AuthService{
|
||||
Future<List<String>> getUserDevices(sessionToken) async {
|
||||
Future<List<String>> getUserDevices(String sessionToken) async {
|
||||
final response = await http.get(
|
||||
Uri.parse(Endpoints.getUserDevices),
|
||||
headers: <String, String>{
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
'token': sessionToken,
|
||||
'Authorization': sessionToken,
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200){
|
||||
List<dynamic> list = jsonDecode(response.body)['devices'];
|
||||
List<String> res = [];
|
||||
for (final l in list) {
|
||||
res.add(l.toString());
|
||||
}
|
||||
return res;
|
||||
} else{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,15 +5,20 @@ import 'package:leg_barkr_app/model/latitude_longitude.dart';
|
|||
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
|
||||
|
||||
class MapService{
|
||||
Future<LatitudeLongitude> getPetLastLocation(deviceId) async {
|
||||
Future<LatitudeLongitude> getPetLastLocation(String deviceId, String sessionToken) async {
|
||||
final response = await http.get(
|
||||
Uri.parse(Endpoints.getLastLocation),
|
||||
headers: <String, String>{
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
'deviceid': deviceId,
|
||||
'Authorization': sessionToken,
|
||||
'Device-ID': deviceId,
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200){
|
||||
return LatitudeLongitude.fromJson(jsonDecode(response.body));
|
||||
} else {
|
||||
return throw Exception('Pet not found');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Position> getMyLocation() async{
|
||||
|
|
|
@ -4,14 +4,19 @@ import 'package:leg_barkr_app/model/metrics_response.dart';
|
|||
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
|
||||
|
||||
class MetricsService {
|
||||
Future<MetricsResponse> getMetricsSummary(deviceId) async {
|
||||
Future<MetricsResponse> getMetricsSummary(String deviceId, String sessionToken) async {
|
||||
final response = await http.get(
|
||||
Uri.parse(Endpoints.getMetricsSummary),
|
||||
headers: <String, String>{
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
'deviceid': deviceId,
|
||||
'Authorization': sessionToken,
|
||||
'Device-ID': deviceId,
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
return MetricsResponse.fromJson(jsonDecode(response.body));
|
||||
} else {
|
||||
return MetricsResponse(0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,30 +3,40 @@ import 'package:http/http.dart' as http;
|
|||
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
|
||||
|
||||
class StepsService {
|
||||
Future<int> getStepsToday(deviceId) async {
|
||||
Future<int> getStepsToday(String deviceId, String sessionToken) async {
|
||||
final response = await http.get(
|
||||
Uri.parse(Endpoints.getStepsToday),
|
||||
headers: <String, String>{
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
'deviceid': deviceId,
|
||||
'Authorization': sessionToken,
|
||||
'Device-ID': deviceId,
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
return jsonDecode(response.body)['cumulative_steps_today'];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<int>> getStepsLastFiveDays(deviceId) async {
|
||||
Future<List<int>> getStepsLastFiveDays(String deviceId, String sessionToken) async {
|
||||
final response = await http.get(
|
||||
Uri.parse(Endpoints.getStepsLastFiveDays),
|
||||
headers: <String, String>{
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
'deviceid': deviceId,
|
||||
'Authorization': sessionToken,
|
||||
'Device-ID': deviceId,
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
List<dynamic> list = jsonDecode(response.body)['daily_steps'];
|
||||
List<int> steps = [];
|
||||
for (final l in list){
|
||||
steps.add(l);
|
||||
}
|
||||
return steps;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
||||
class LoginForm extends StatefulWidget {
|
||||
const LoginForm({Key? key}) : super(key: key);
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:leg_barkr_app/view/home.dart';
|
||||
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
|
||||
import 'package:leg_barkr_app/view/auth/login_form.dart';
|
||||
|
||||
|
@ -202,7 +201,28 @@ class _RegisterFormState extends State<RegisterForm> {
|
|||
primary: Colors.green,
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
final Container loginBtn = Container(
|
||||
width: double.infinity,
|
||||
height: 50,
|
||||
child: ElevatedButton(
|
||||
onPressed: (){ Navigator.pushNamed(context, '/login'); },
|
||||
child: Text('Already have an account? Login now!',
|
||||
style: TextStyle(
|
||||
color: Colors.black,
|
||||
fontSize: 15
|
||||
)
|
||||
),
|
||||
style: ElevatedButton.styleFrom(
|
||||
alignment: Alignment.center,
|
||||
primary: Colors.white,
|
||||
side: BorderSide(
|
||||
color: Colors.green,
|
||||
width: 2
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
|
@ -228,7 +248,9 @@ class _RegisterFormState extends State<RegisterForm> {
|
|||
SizedBox(height: 20),
|
||||
confirmPasswordInput,
|
||||
SizedBox(height: 20),
|
||||
_registering ? loading : registerBtn
|
||||
_registering ? loading : registerBtn,
|
||||
SizedBox(height: 20),
|
||||
_registering ? Text("") : loginBtn
|
||||
]
|
||||
)
|
||||
)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:leg_barkr_app/service/map_service.dart';
|
||||
|
@ -17,8 +18,10 @@ class _MapPageState extends State<MapPage> {
|
|||
Future<void> _onMapCreated(GoogleMapController controller) async {
|
||||
_mapController = controller;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final user = await FirebaseAuth.instance.currentUser!;
|
||||
final String token = await user.getIdToken();
|
||||
final String deviceId = prefs.getString("current_device") ?? "";
|
||||
final lastLocation = await MapService().getPetLastLocation(deviceId);
|
||||
final lastLocation = await MapService().getPetLastLocation(deviceId, token);
|
||||
final myLocation = await MapService().getMyLocation();
|
||||
|
||||
setState(() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:leg_barkr_app/model/metrics_data.dart';
|
||||
import 'package:leg_barkr_app/model/metrics_response.dart';
|
||||
|
@ -31,7 +32,9 @@ class _MetricsPageState extends State<MetricsPage> {
|
|||
Future<MetricsResponse> onMetricsReceived() async{
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final String deviceId = prefs.getString("current_device") ?? "";
|
||||
return await MetricsService().getMetricsSummary(deviceId);
|
||||
final user = await FirebaseAuth.instance.currentUser!;
|
||||
final String token = await user.getIdToken();
|
||||
return await MetricsService().getMetricsSummary(deviceId, token);
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:leg_barkr_app/model/steps_series.dart';
|
||||
import 'package:leg_barkr_app/service/steps_service.dart';
|
||||
|
@ -17,7 +18,9 @@ class _StepsPageState extends State<StepsPage> {
|
|||
Future<List<int>> onStepsRetrieved() async{
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final String deviceId = prefs.getString("current_device") ?? "";
|
||||
return await StepsService().getStepsLastFiveDays(deviceId);
|
||||
final user = await FirebaseAuth.instance.currentUser!;
|
||||
final String token = await user.getIdToken();
|
||||
return await StepsService().getStepsLastFiveDays(deviceId, token);
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
Loading…
Reference in a new issue