404 page gone

This commit is contained in:
thosakwe 2016-09-21 02:50:44 -04:00
parent 49bf073a22
commit 78ae1dfe92
5 changed files with 44 additions and 66 deletions

View file

@ -1,16 +1,40 @@
part of angel.routes.controllers; library angel.routes.controllers.auth;
import 'package:angel_auth/angel_auth.dart';
import 'package:angel_framework/angel_framework.dart';
import '../../services/user/user.dart';
@Expose("/auth") @Expose("/auth")
class AuthController extends Controller { class AuthController extends Controller {
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);
}
};
}
@override @override
call(Angel app) async { call(Angel app) async {
// Wire up local authentication, connected to our User service
_auth.serializer = _serializer;
_auth.deserializer = _deserializer;
_auth.strategies.add(new LocalAuthStrategy(
_auth, _verifier(app.container.make(UserService)),
forceBasic: true));
await super.call(app); await super.call(app);
await app.configure(_auth);
app.registerMiddleware("auth", (req, res) async {
if (!loggedIn(req)) throw new AngelHttpException.Forbidden();
return true;
});
} }
bool loggedIn(RequestContext req) => req.session["userId"] != null; bool loggedIn(RequestContext req) => req.session["userId"] != null;

View file

@ -1,8 +1,7 @@
library angel.routes.controllers; library angel.routes.controllers;
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import '../../services/user/user.dart'; import 'auth.dart';
part 'auth.dart';
configureServer(Angel app) async { configureServer(Angel app) async {
await app.configure(new AuthController()); await app.configure(new AuthController());

View file

@ -15,9 +15,9 @@ configureRoutes(Angel app) async {
configureAfter(Angel app) async { configureAfter(Angel app) async {
// 404 handler // 404 handler
app.after.add((req, res) async => res app.after.add((req, ResponseContext res) async {
..status(404) throw new AngelHttpException.NotFound();
..render("404", {"path": req.path})); });
// Default error handler // Default error handler
app.onError( app.onError(

View file

@ -15,6 +15,10 @@ configureServer(Db db) {
}; };
} }
/// SHA-256 hash any string, particularly a password.
String hashPassword(String password) =>
sha256.convert(password.codeUnits).toString();
/// Manages users. /// Manages users.
/// ///
/// Here, we extended the base service class. This allows to only expose /// Here, we extended the base service class. This allows to only expose
@ -22,7 +26,7 @@ configureServer(Db db) {
class UserService extends Service { class UserService extends Service {
MongoTypedService<User> _inner; MongoTypedService<User> _inner;
UserService(DbCollection collection):super() { UserService(DbCollection collection) : super() {
_inner = new MongoTypedService<User>(collection); _inner = new MongoTypedService<User>(collection);
} }
@ -46,9 +50,10 @@ class UserService extends Service {
try { try {
Validate.isKeyInMap("username", data); Validate.isKeyInMap("username", data);
Validate.isEmail(data["email"]); Validate.isEmail(data["email"]);
data["password"] = sha256.convert(data["password"].codeUnits).toString(); data["password"] = hashPassword(data["password"]);
} catch(e) { } catch (e) {
throw new AngelHttpException.BadRequest(message: "User must have a username, e-mail address and password."); throw new AngelHttpException.BadRequest(
message: "User must have a username, e-mail address and password.");
} }
return _inner.create(data, params); return _inner.create(data, params);

View file

@ -1,50 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>404 Not Found</title>
<link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
<style>
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
display: table;
font-weight: 100;
font-family: 'Lato', sans-serif;
}
.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.content {
text-align: center;
display: inline-block;
}
.title {
font-size: 96px;
}
.subtitle {
font-size: 32px;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">404 Not Found</div>
<div class="subtitle">No file was found at "{{path}}".</div>
</div>
</div>
</body>
</html>