User sessions are now passed to the backend

This commit is contained in:
Benjamin Ramhorst 2022-02-15 11:52:19 +00:00
parent f4d0527a3d
commit 7a9723bb34
9 changed files with 83 additions and 29 deletions

View file

@ -3,19 +3,23 @@ import 'package:http/http.dart' as http;
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
class AuthService{ class AuthService{
Future<List<String>> getUserDevices(sessionToken) async { Future<List<String>> getUserDevices(String sessionToken) async {
final response = await http.get( final response = await http.get(
Uri.parse(Endpoints.getUserDevices), Uri.parse(Endpoints.getUserDevices),
headers: <String, String>{ headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8', 'Content-Type': 'application/json; charset=UTF-8',
'token': sessionToken, 'Authorization': sessionToken,
}, },
); );
List<dynamic> list = jsonDecode(response.body)['devices']; if (response.statusCode == 200){
List<String> res = []; List<dynamic> list = jsonDecode(response.body)['devices'];
for (final l in list) { List<String> res = [];
res.add(l.toString()); for (final l in list) {
res.add(l.toString());
}
return res;
} else{
return [];
} }
return res;
} }
} }

View file

@ -5,15 +5,20 @@ import 'package:leg_barkr_app/model/latitude_longitude.dart';
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
class MapService{ class MapService{
Future<LatitudeLongitude> getPetLastLocation(deviceId) async { Future<LatitudeLongitude> getPetLastLocation(String deviceId, String sessionToken) async {
final response = await http.get( final response = await http.get(
Uri.parse(Endpoints.getLastLocation), Uri.parse(Endpoints.getLastLocation),
headers: <String, String>{ headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8', 'Content-Type': 'application/json; charset=UTF-8',
'deviceid': deviceId, 'Authorization': sessionToken,
'Device-ID': deviceId,
}, },
); );
return LatitudeLongitude.fromJson(jsonDecode(response.body)); if (response.statusCode == 200){
return LatitudeLongitude.fromJson(jsonDecode(response.body));
} else {
return throw Exception('Pet not found');
}
} }
Future<Position> getMyLocation() async{ Future<Position> getMyLocation() async{

View file

@ -4,14 +4,19 @@ import 'package:leg_barkr_app/model/metrics_response.dart';
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
class MetricsService { class MetricsService {
Future<MetricsResponse> getMetricsSummary(deviceId) async { Future<MetricsResponse> getMetricsSummary(String deviceId, String sessionToken) async {
final response = await http.get( final response = await http.get(
Uri.parse(Endpoints.getMetricsSummary), Uri.parse(Endpoints.getMetricsSummary),
headers: <String, String>{ headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8', 'Content-Type': 'application/json; charset=UTF-8',
'deviceid': deviceId, 'Authorization': sessionToken,
'Device-ID': deviceId,
}, },
); );
return MetricsResponse.fromJson(jsonDecode(response.body)); if (response.statusCode == 200) {
return MetricsResponse.fromJson(jsonDecode(response.body));
} else {
return MetricsResponse(0, 0, 0, 0, 0, 0, 0, 0, 0);
}
} }
} }

View file

@ -3,30 +3,40 @@ import 'package:http/http.dart' as http;
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
class StepsService { class StepsService {
Future<int> getStepsToday(deviceId) async { Future<int> getStepsToday(String deviceId, String sessionToken) async {
final response = await http.get( final response = await http.get(
Uri.parse(Endpoints.getStepsToday), Uri.parse(Endpoints.getStepsToday),
headers: <String, String>{ headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8', 'Content-Type': 'application/json; charset=UTF-8',
'deviceid': deviceId, 'Authorization': sessionToken,
'Device-ID': deviceId,
}, },
); );
return jsonDecode(response.body)['cumulative_steps_today']; 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( final response = await http.get(
Uri.parse(Endpoints.getStepsLastFiveDays), Uri.parse(Endpoints.getStepsLastFiveDays),
headers: <String, String>{ headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8', 'Content-Type': 'application/json; charset=UTF-8',
'deviceid': deviceId, 'Authorization': sessionToken,
'Device-ID': deviceId,
}, },
); );
List<dynamic> list = jsonDecode(response.body)['daily_steps']; if (response.statusCode == 200) {
List<int> steps = []; List<dynamic> list = jsonDecode(response.body)['daily_steps'];
for (final l in list){ List<int> steps = [];
steps.add(l); for (final l in list){
steps.add(l);
}
return steps;
} else {
return [];
} }
return steps;
} }
} }

View file

@ -1,7 +1,6 @@
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class LoginForm extends StatefulWidget { class LoginForm extends StatefulWidget {
const LoginForm({Key? key}) : super(key: key); const LoginForm({Key? key}) : super(key: key);

View file

@ -1,7 +1,6 @@
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; 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/utils/endpoints.dart' as Endpoints;
import 'package:leg_barkr_app/view/auth/login_form.dart'; import 'package:leg_barkr_app/view/auth/login_form.dart';
@ -202,7 +201,28 @@ class _RegisterFormState extends State<RegisterForm> {
primary: Colors.green, 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( return Scaffold(
@ -228,7 +248,9 @@ class _RegisterFormState extends State<RegisterForm> {
SizedBox(height: 20), SizedBox(height: 20),
confirmPasswordInput, confirmPasswordInput,
SizedBox(height: 20), SizedBox(height: 20),
_registering ? loading : registerBtn _registering ? loading : registerBtn,
SizedBox(height: 20),
_registering ? Text("") : loginBtn
] ]
) )
) )

View file

@ -1,3 +1,4 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:leg_barkr_app/service/map_service.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 { Future<void> _onMapCreated(GoogleMapController controller) async {
_mapController = controller; _mapController = controller;
final prefs = await SharedPreferences.getInstance(); 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 String deviceId = prefs.getString("current_device") ?? "";
final lastLocation = await MapService().getPetLastLocation(deviceId); final lastLocation = await MapService().getPetLastLocation(deviceId, token);
final myLocation = await MapService().getMyLocation(); final myLocation = await MapService().getMyLocation();
setState(() { setState(() {

View file

@ -1,3 +1,4 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:leg_barkr_app/model/metrics_data.dart'; import 'package:leg_barkr_app/model/metrics_data.dart';
import 'package:leg_barkr_app/model/metrics_response.dart'; import 'package:leg_barkr_app/model/metrics_response.dart';
@ -31,7 +32,9 @@ class _MetricsPageState extends State<MetricsPage> {
Future<MetricsResponse> onMetricsReceived() async{ Future<MetricsResponse> onMetricsReceived() async{
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
final String deviceId = prefs.getString("current_device") ?? ""; 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 @override

View file

@ -1,3 +1,4 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:leg_barkr_app/model/steps_series.dart'; import 'package:leg_barkr_app/model/steps_series.dart';
import 'package:leg_barkr_app/service/steps_service.dart'; import 'package:leg_barkr_app/service/steps_service.dart';
@ -17,7 +18,9 @@ class _StepsPageState extends State<StepsPage> {
Future<List<int>> onStepsRetrieved() async{ Future<List<int>> onStepsRetrieved() async{
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
final String deviceId = prefs.getString("current_device") ?? ""; 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 @override