Compare commits
2 Commits
6a0696b9c1
...
6fd8c365f6
Author | SHA1 | Date | |
---|---|---|---|
6fd8c365f6 | |||
5f1773f614 |
@ -2,31 +2,30 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:developer';
|
|
||||||
|
|
||||||
enum ApiService {
|
enum ApiService {
|
||||||
auth,
|
auth,
|
||||||
app,
|
app,
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String?> request(BuildContext context, ApiService service, String method,
|
Future<String?> request(BuildContext context, ApiService service, String method, String path, Object? body) async {
|
||||||
String path, Object? body) async {
|
|
||||||
log('hello');
|
|
||||||
final messenger = ScaffoldMessenger.of(context);
|
final messenger = ScaffoldMessenger.of(context);
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
final host = switch (service) {
|
final host = switch (service) {
|
||||||
ApiService.auth => const String.fromEnvironment('AUTH_SERVICE_HOST'),
|
ApiService.auth => const String.fromEnvironment('AUTH_SERVICE_HOST'),
|
||||||
ApiService.app => const String.fromEnvironment('APP_SERVICE_HOST'),
|
ApiService.app => const String.fromEnvironment('APP_SERVICE_HOST'),
|
||||||
};
|
};
|
||||||
|
|
||||||
log('hello');
|
final token = prefs.getString('token');
|
||||||
log(const String.fromEnvironment('AUTH_SERVICE_HOST'));
|
final Map<String, String> headers = {};
|
||||||
|
if (token != null) headers.addAll({'Authorization': 'Bearer $token'});
|
||||||
|
|
||||||
final http.Response response;
|
final http.Response response;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (method == 'GET') {
|
if (method == 'GET') {
|
||||||
response = await http.get(Uri.parse(host + path));
|
response = await http.get(Uri.parse(host + path), headers: headers);
|
||||||
} else {
|
} else {
|
||||||
final function = switch (method) {
|
final function = switch (method) {
|
||||||
'POST' => http.post,
|
'POST' => http.post,
|
||||||
@ -35,15 +34,16 @@ Future<String?> request(BuildContext context, ApiService service, String method,
|
|||||||
_ => throw const FormatException('Invalid method'),
|
_ => throw const FormatException('Invalid method'),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
headers.addAll({'Content-Type': 'application/json'});
|
||||||
|
|
||||||
response = await function(
|
response = await function(
|
||||||
Uri.parse(host + path),
|
Uri.parse(host + path),
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: headers,
|
||||||
body: body != null ? jsonEncode(body) : null,
|
body: body != null ? jsonEncode(body) : null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
messenger.showSnackBar(
|
messenger.showSnackBar(const SnackBar(content: Text('Unable to connect to server')));
|
||||||
const SnackBar(content: Text('Unable to connect to server')));
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,8 +52,7 @@ Future<String?> request(BuildContext context, ApiService service, String method,
|
|||||||
final json = jsonDecode(response.body);
|
final json = jsonDecode(response.body);
|
||||||
messenger.showSnackBar(SnackBar(content: Text(json['message'])));
|
messenger.showSnackBar(SnackBar(content: Text(json['message'])));
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
messenger.showSnackBar(SnackBar(
|
messenger.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})')));
|
||||||
content: Text('Something went wrong (HTTP ${response.statusCode})')));
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -75,14 +74,12 @@ Future<bool> isLoggedIn(BuildContext context) async {
|
|||||||
final payload = jsonDecode(String.fromCharCodes(base64Decode(base64)));
|
final payload = jsonDecode(String.fromCharCodes(base64Decode(base64)));
|
||||||
|
|
||||||
if (payload['exp'] < DateTime.now().millisecondsSinceEpoch / 1000) {
|
if (payload['exp'] < DateTime.now().millisecondsSinceEpoch / 1000) {
|
||||||
messenger.showSnackBar(
|
messenger.showSnackBar(const SnackBar(content: Text('Token expired, please sign in again')));
|
||||||
const SnackBar(content: Text('Token expired, please sign in again')));
|
|
||||||
prefs.remove('token');
|
prefs.remove('token');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
messenger.showSnackBar(
|
messenger.showSnackBar(const SnackBar(content: Text('Invalid token, please sign in again')));
|
||||||
const SnackBar(content: Text('Invalid token, please sign in again')));
|
|
||||||
prefs.remove('token');
|
prefs.remove('token');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
final passwordInput = TextEditingController();
|
final passwordInput = TextEditingController();
|
||||||
|
|
||||||
Future<void> _login() async {
|
Future<void> _login() async {
|
||||||
final token = await api
|
final token = await api.request(context, api.ApiService.auth, 'POST', '/api/Users/login', {
|
||||||
.request(context, api.ApiService.auth, 'POST', '/api/Users/login', {
|
|
||||||
'email': emailInput.text,
|
'email': emailInput.text,
|
||||||
'password': passwordInput.text,
|
'password': passwordInput.text,
|
||||||
});
|
});
|
||||||
@ -27,8 +26,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
prefs.setString('token', token);
|
prefs.setString('token', token);
|
||||||
|
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged in')));
|
||||||
const SnackBar(content: Text('Successfully logged in')));
|
|
||||||
Navigator.pushReplacementNamed(context, '/home');
|
Navigator.pushReplacementNamed(context, '/home');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,17 +45,18 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
const SizedBox(height: 30),
|
const SizedBox(height: 30),
|
||||||
const Text('Password'),
|
const Text('Password'),
|
||||||
TextField(
|
TextField(
|
||||||
controller: passwordInput,
|
controller: passwordInput,
|
||||||
obscureText: true,
|
obscureText: true,
|
||||||
enableSuggestions: false,
|
enableSuggestions: false,
|
||||||
autocorrect: false),
|
autocorrect: false,
|
||||||
|
),
|
||||||
const SizedBox(height: 30),
|
const SizedBox(height: 30),
|
||||||
ElevatedButton(onPressed: _login, child: const Text('Login')),
|
ElevatedButton(onPressed: _login, child: const Text('Login')),
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
TextButton(
|
TextButton(
|
||||||
child: const Text('Register account'),
|
child: const Text('Register account'),
|
||||||
onPressed: () =>
|
onPressed: () => Navigator.pushReplacementNamed(context, '/register')
|
||||||
Navigator.pushReplacementNamed(context, '/register')),
|
),
|
||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
Loading…
Reference in New Issue
Block a user