platform/packages/auth/example/example.dart
2024-10-12 19:17:24 -07:00

38 lines
1 KiB
Dart

import 'dart:async';
import 'package:protevus_auth/protevus_auth.dart';
import 'package:protevus_framework/protevus_framework.dart';
import 'package:protevus_framework/http.dart';
void main() async {
var app = Protevus();
var auth = ProtevusAuth<User>(
serializer: (user) => user.id ?? '',
deserializer: (id) => fetchAUserByIdSomehow(id));
// Middleware to decode JWT's and inject a user object...
await app.configure(auth.configureServer);
auth.strategies['local'] = LocalAuthStrategy((username, password) {
// Retrieve a user somehow...
// If authentication succeeds, return a User object.
//
// Otherwise, return `null`.
return null;
});
app.post('/auth/local', auth.authenticate('local'));
var http = ProtevusHttp(app);
await http.startServer('127.0.0.1', 3000);
print('Listening at http://127.0.0.1:3000');
}
class User {
String? id, username, password;
}
Future<User> fetchAUserByIdSomehow(String id) async {
// Fetch a user somehow...
throw UnimplementedError();
}