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
|
|
|
|
..deserializer = deserializer;
|
|
|
|
..strategies
|
2016-12-05 01:20:12 +00:00
|
|
|
.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')
|
2016-07-05 21:11:54 +00:00
|
|
|
login(RequestContext req) async {
|
|
|
|
// Include log-in logic here...
|
|
|
|
}
|
2016-09-21 05:44:56 +00:00
|
|
|
|
2016-12-10 18:51:02 +00:00
|
|
|
@Expose('/register', method: 'POST')
|
2016-09-21 05:44:56 +00:00
|
|
|
register(RequestContext req, UserService Users) async {
|
|
|
|
// And your registration logic...
|
|
|
|
}
|
2016-07-05 21:11:54 +00:00
|
|
|
}
|