platform/packages/auth/example/example.dart

39 lines
1 KiB
Dart
Raw Permalink Normal View History

2018-09-12 00:51:30 +00:00
import 'dart:async';
import 'package:protevus_auth/protevus_auth.dart';
import 'package:protevus_framework/protevus_framework.dart';
import 'package:protevus_framework/http.dart';
2018-09-12 00:51:30 +00:00
2021-03-20 23:51:20 +00:00
void main() async {
var app = Protevus();
var auth = ProtevusAuth<User>(
2021-09-29 07:40:27 +00:00
serializer: (user) => user.id ?? '',
2021-06-07 00:50:39 +00:00
deserializer: (id) => fetchAUserByIdSomehow(id));
2018-09-12 00:51:30 +00:00
// Middleware to decode JWT's and inject a user object...
await app.configure(auth.configureServer);
2018-09-12 00:51:30 +00:00
2019-04-19 07:50:04 +00:00
auth.strategies['local'] = LocalAuthStrategy((username, password) {
2018-09-12 00:51:30 +00:00
// Retrieve a user somehow...
// If authentication succeeds, return a User object.
//
// Otherwise, return `null`.
2022-08-17 03:17:00 +00:00
return null;
2018-09-12 00:51:30 +00:00
});
app.post('/auth/local', auth.authenticate('local'));
var http = ProtevusHttp(app);
2018-09-12 00:51:30 +00:00
await http.startServer('127.0.0.1', 3000);
print('Listening at http://127.0.0.1:3000');
}
class User {
2021-03-20 23:51:20 +00:00
String? id, username, password;
2018-09-12 00:51:30 +00:00
}
2021-09-29 07:40:27 +00:00
Future<User> fetchAUserByIdSomehow(String id) async {
2018-09-12 00:51:30 +00:00
// Fetch a user somehow...
2019-04-19 07:50:04 +00:00
throw UnimplementedError();
2018-09-12 00:51:30 +00:00
}