mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
fix/refactor/simplify powersync auth setup
This commit is contained in:
@@ -14,32 +14,6 @@ class ApiClient {
|
||||
|
||||
const ApiClient(this.baseUrl);
|
||||
|
||||
Future<Map<String, dynamic>> authenticate(String username, String password) async {
|
||||
final response = await http.post(
|
||||
Uri.parse('$baseUrl/api/v2/login/'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: json.encode({'username': username, 'password': password}),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
log.log(Level.ALL, response.body);
|
||||
return json.decode(response.body);
|
||||
}
|
||||
throw Exception('Failed to authenticate');
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getWgerJWTToken() async {
|
||||
final response = await http.post(
|
||||
Uri.parse('$baseUrl/api/v2/token'),
|
||||
headers: {HttpHeaders.contentTypeHeader: 'application/json'},
|
||||
body: json.encode({'username': 'admin', 'password': 'adminadmin'}), // FIXME
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
log.log(Level.ALL, response.body);
|
||||
return json.decode(response.body);
|
||||
}
|
||||
throw Exception('Failed to fetch token');
|
||||
}
|
||||
|
||||
/// Returns a powersync JWT token token
|
||||
///
|
||||
/// Note that at the moment we use the permanent API token for authentication
|
||||
|
||||
@@ -77,11 +77,6 @@ void main() async {
|
||||
// Needs to be called before runApp
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
await openDatabase();
|
||||
|
||||
final loggedIn = await isLoggedIn();
|
||||
print('main(): is logged in $loggedIn');
|
||||
|
||||
// Locator to initialize exerciseDB
|
||||
await ServiceLocator().configure();
|
||||
print('running myapp');
|
||||
|
||||
@@ -3,7 +3,6 @@ import 'package:logging/logging.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:powersync/powersync.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:wger/api_client.dart';
|
||||
|
||||
import './app_config.dart';
|
||||
@@ -37,6 +36,7 @@ class DjangoConnector extends PowerSyncBackendConnector {
|
||||
// https://docs.powersync.com/usage/installation/authentication-setup/custom
|
||||
// final wgerSession = await apiClient.getWgerJWTToken();
|
||||
final session = await apiClient.getPowersyncToken();
|
||||
// note: we don't set userId and expires property here. not sure if needed
|
||||
return PowerSyncCredentials(endpoint: AppConfig.powersyncUrl, token: session['token']);
|
||||
}
|
||||
|
||||
@@ -86,24 +86,13 @@ late final PowerSyncDatabase db;
|
||||
// Hacky flag to ensure the database is only initialized once, better to do this with listeners
|
||||
bool _dbInitialized = false;
|
||||
|
||||
/// id of the user currently logged in
|
||||
Future<String?> getUserId() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getString('id');
|
||||
}
|
||||
|
||||
Future<bool> isLoggedIn() async {
|
||||
final userId = await getUserId();
|
||||
return userId != null;
|
||||
}
|
||||
|
||||
Future<String> getDatabasePath() async {
|
||||
final dir = await getApplicationSupportDirectory();
|
||||
return join(dir.path, 'powersync-demo.db');
|
||||
}
|
||||
|
||||
// opens the database and connects if logged in
|
||||
Future<void> openDatabase() async {
|
||||
Future<void> openDatabase(bool connect) async {
|
||||
// Open the local database
|
||||
if (!_dbInitialized) {
|
||||
db = PowerSyncDatabase(schema: schema, path: await getDatabasePath(), logger: attachedLogger);
|
||||
@@ -111,12 +100,10 @@ Future<void> openDatabase() async {
|
||||
_dbInitialized = true;
|
||||
}
|
||||
|
||||
DjangoConnector? currentConnector;
|
||||
|
||||
if (await isLoggedIn()) {
|
||||
if (connect) {
|
||||
// If the user is already logged in, connect immediately.
|
||||
// Otherwise, connect once logged in.
|
||||
currentConnector = DjangoConnector(db);
|
||||
final currentConnector = DjangoConnector(db);
|
||||
db.connect(connector: currentConnector);
|
||||
|
||||
// TODO: should we respond to login state changing? like here:
|
||||
|
||||
@@ -182,6 +182,7 @@ class AuthProvider with ChangeNotifier {
|
||||
}
|
||||
|
||||
// Log user in
|
||||
// should we update the backend to just include a powersync token also?
|
||||
token = responseData['token'];
|
||||
notifyListeners();
|
||||
|
||||
|
||||
@@ -54,8 +54,8 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> with SingleTickerProvid
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
final authProvider = context.read<AuthProvider>();
|
||||
print('auth provider says surverurl is ${authProvider.serverUrl}');
|
||||
// do we need to await this? or if it's async, how do we handle failures?
|
||||
_setupPowersync();
|
||||
|
||||
// Loading data here, since the build method can be called more than once
|
||||
_initialData = _loadEntries();
|
||||
@@ -75,19 +75,27 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> with SingleTickerProvid
|
||||
const GalleryScreen(),
|
||||
];
|
||||
|
||||
/// Load initial data from the server
|
||||
Future<void> _loadEntries() async {
|
||||
Future<void> _setupPowersync() async {
|
||||
final authProvider = context.read<AuthProvider>();
|
||||
print('auth provider says surverurl is ${authProvider.serverUrl}');
|
||||
await openDatabase(false);
|
||||
|
||||
final connector = DjangoConnector(db);
|
||||
try {
|
||||
// TODO: should we cache these credentials? that's what their demo does?
|
||||
// we could maybe get the initial token from the /api/v2/login call
|
||||
final credentials = await connector.fetchCredentials();
|
||||
print('----------');
|
||||
print(credentials);
|
||||
print('----------');
|
||||
await openDatabase(true);
|
||||
} catch (e) {
|
||||
print('fail' + e.toString());
|
||||
}
|
||||
final loggedIn = await isLoggedIn();
|
||||
print('_loadEntries(): is logged in $loggedIn');
|
||||
}
|
||||
|
||||
/// Load initial data from the server
|
||||
Future<void> _loadEntries() async {
|
||||
final authProvider = context.read<AuthProvider>();
|
||||
|
||||
if (!authProvider.dataInit) {
|
||||
|
||||
Reference in New Issue
Block a user