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

47 lines
1.3 KiB
Dart
Raw Normal View History

2016-09-21 06:50:44 +00:00
library angel.routes.controllers.auth;
2016-12-21 22:05:11 +00:00
import 'package:angel_common/angel_common.dart';
2016-09-21 06:59:40 +00:00
import '../../services/user.dart';
2016-07-05 21:11:54 +00:00
2016-12-10 18:51:02 +00:00
@Expose('/api/auth')
2016-07-05 21:11:54 +00:00
class AuthController extends Controller {
2016-12-24 04:44:28 +00:00
AngelAuth auth;
2016-09-21 06:50:44 +00:00
2016-12-10 18:51:02 +00:00
deserializer(String id) async => app.service('api/users').read(id);
2016-12-05 01:20:12 +00:00
serializer(User user) async => user.id;
2016-09-21 06:50:44 +00:00
/// Attempt to log a user in
2016-12-05 01:20:12 +00:00
verifier(UserService Users) {
2016-09-21 06:50:44 +00:00
return (String username, String password) async {
2016-12-10 18:51:02 +00:00
List<User> users = await Users.index({'username': username});
2016-09-21 06:50:44 +00:00
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
2016-12-24 04:44:28 +00:00
auth = new AngelAuth(jwtKey: app.jwt_secret)
..serializer = serializer
2016-12-24 16:12:50 +00:00
..deserializer = deserializer
..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);
2016-12-05 01:20:12 +00:00
await app.configure(auth);
2016-07-05 21:11:54 +00:00
}
2016-12-10 18:51:02 +00:00
@Expose('/login', method: 'POST')
2017-02-19 03:23:43 +00:00
login(RequestContext req) async => auth.authenticate('local');
2016-09-21 05:44:56 +00:00
2016-12-10 18:51:02 +00:00
@Expose('/register', method: 'POST')
2017-02-19 03:23:43 +00:00
register(RequestContext req, UserService userService) async {
2016-09-21 05:44:56 +00:00
// And your registration logic...
}
2016-07-05 21:11:54 +00:00
}