platform/packages/auth/example/example.dart

40 lines
993 B
Dart
Raw Normal View History

2018-09-12 00:51:30 +00:00
import 'dart:async';
import 'package:angel_auth/angel_auth.dart';
import 'package:angel_framework/angel_framework.dart';
2018-12-09 16:29:15 +00:00
import 'package:angel_framework/http.dart';
2018-09-12 00:51:30 +00:00
2021-03-20 23:51:20 +00:00
void main() async {
2019-04-19 07:50:04 +00:00
var app = Angel();
2021-03-20 23:51:20 +00:00
var auth = AngelAuth<User?>();
2018-09-12 00:51:30 +00:00
2021-03-20 23:51:20 +00:00
auth.serializer = (user) => user!.id;
2018-09-12 00:51:30 +00:00
auth.deserializer = (id) => fetchAUserByIdSomehow(id);
// 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`.
});
app.post('/auth/local', auth.authenticate('local'));
2019-04-19 07:50:04 +00:00
var http = AngelHttp(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
}
Future<User> fetchAUserByIdSomehow(id) async {
// Fetch a user somehow...
2019-04-19 07:50:04 +00:00
throw UnimplementedError();
2018-09-12 00:51:30 +00:00
}