platform/lib/src/routes/controllers/auth.dart

51 lines
1.4 KiB
Dart
Raw Normal View History

2016-09-21 06:50:44 +00:00
library angel.routes.controllers.auth;
import 'package:angel_auth/angel_auth.dart';
import 'package:angel_framework/angel_framework.dart';
2016-09-21 06:59:40 +00:00
import '../../services/user.dart';
2016-07-05 21:11:54 +00:00
@Expose("/auth")
class AuthController extends Controller {
2016-09-21 06:50:44 +00:00
final AngelAuth _auth = new AngelAuth();
_deserializer(String id) async => app.service("api/users").read(id);
_serializer(User user) async => user.id;
/// Attempt to log a user in
_verifier(UserService Users) {
return (String username, String password) async {
List<User> users = await Users.index({"username": username});
if (users.isNotEmpty) {
var hash = hashPassword(password);
return users.firstWhere((user) => user.password == hash,
orElse: () => null);
}
};
}
2016-07-05 21:11:54 +00:00
@override
call(Angel app) async {
2016-09-21 06:50:44 +00:00
// Wire up local authentication, connected to our User service
_auth.serializer = _serializer;
_auth.deserializer = _deserializer;
2016-11-23 22:21:41 +00:00
_auth.strategies
.add(new LocalAuthStrategy(_verifier(app.container.make(UserService))));
2016-09-21 05:44:56 +00:00
2016-09-21 06:50:44 +00:00
await super.call(app);
await app.configure(_auth);
2016-07-05 21:11:54 +00:00
}
2016-09-21 05:44:56 +00:00
bool loggedIn(RequestContext req) => req.session["userId"] != null;
2016-07-05 21:11:54 +00:00
@Expose("/login", method: "POST")
login(RequestContext req) async {
// Include log-in logic here...
}
2016-09-21 05:44:56 +00:00
@Expose("/register", method: "POST")
register(RequestContext req, UserService Users) async {
// And your registration logic...
}
2016-07-05 21:11:54 +00:00
}