platform/packages/auth/example/example.dart

38 lines
1,002 B
Dart
Raw Normal View History

2018-09-12 00:51:30 +00:00
import 'dart:async';
2021-05-14 11:09:48 +00:00
import 'package:angel3_auth/angel3_auth.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_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-06-07 00:50:39 +00:00
var auth = AngelAuth<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`.
});
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
}
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
}