platform/packages/auth/example/example2.dart

68 lines
2.1 KiB
Dart
Raw Normal View History

2023-05-30 11:49:02 +00:00
import 'dart:async';
import 'package:angel3_auth/angel3_auth.dart';
import 'package:angel3_container/mirrors.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
import 'package:logging/logging.dart';
final Map<String, String> sampleUser = {'hello': 'world'};
final AngelAuth<Map<String, String>> auth = AngelAuth<Map<String, String>>(
serializer: (user) async => '1337', deserializer: (id) async => sampleUser);
//var headers = <String, String>{'accept': 'application/json'};
var localOpts = AngelAuthOptions<Map<String, String>>(
failureRedirect: '/failure', successRedirect: '/success');
var localOpts2 =
AngelAuthOptions<Map<String, String>>(canRespondWithJson: false);
Future<Map<String, String>> verifier(String? username, String? password) async {
if (username == 'username' && password == 'password') {
return sampleUser;
} else {
return {};
}
}
Future wireAuth(Protevus app) async {
2023-05-31 16:25:42 +00:00
//auth.strategies['local'] = LocalAuthStrategy(verifier);
2023-06-05 11:00:42 +00:00
auth.strategies['local'] =
LocalAuthStrategy(verifier, forceBasic: true, realm: 'test');
2023-05-30 11:49:02 +00:00
await app.configure(auth.configureServer);
}
2023-06-05 11:00:42 +00:00
/*
* Backend for local test cases
*/
2023-05-30 11:49:02 +00:00
void main() async {
Protevus app = Protevus(reflector: MirrorsReflector());
ProtevusHttp angelHttp = ProtevusHttp(app, useZone: false);
2023-05-30 11:49:02 +00:00
await app.configure(wireAuth);
app.get('/hello', (req, res) {
// => 'Woo auth'
return 'Woo auth';
}, middleware: [auth.authenticate('local', localOpts2)]);
app.post('/login', (req, res) => 'This should not be shown',
middleware: [auth.authenticate('local', localOpts)]);
app.get('/success', (req, res) => 'yep', middleware: [
requireAuthentication<Map<String, String>>(),
]);
app.get('/failure', (req, res) => 'nope');
app.logger = Logger('local_test')
..onRecord.listen((rec) {
print(
'${rec.time}: ${rec.level.name}: ${rec.loggerName}: ${rec.message}');
if (rec.error != null) {
print(rec.error);
print(rec.stackTrace);
}
});
await angelHttp.startServer('127.0.0.1', 3000);
}