platform/example/example.dart

40 lines
972 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
main() async {
2019-04-19 07:50:04 +00:00
var app = Angel();
var auth = AngelAuth<User>();
2018-09-12 00:51:30 +00:00
auth.serializer = (user) => user.id;
auth.deserializer = (id) => fetchAUserByIdSomehow(id);
// Middleware to decode JWT's and inject a user object...
app.fallback(auth.decodeJwt);
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 {
String id, username, password;
}
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
}