//
//oauthProvider.on('lookupGrant', function(clientId, clientSecret, code, res, callback) {
// callback("userId");
//});
final Yoke app = new Yoke(this);
app.use(new ErrorHandler(true));
app.use(new Logger());
app.use(new BodyParser());
app.use(new CookieParser());
app.use(new Session(mac));
app.use(oauthProvider);
app.use(new Router() {{
get("/", new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
request.loadSessionData(new Handler<JsonObject>() {
@Override
public void handle(JsonObject session) {
if (session == null) {
request.response().end("home, logged in? false");
} else {
System.out.println(session.encodePrettily());
request.response().end("home, logged in? " + (session.getString("user") != null));
}
}
});
}
});
get("/login", new Middleware() {
@Override
public void handle(final YokeRequest request, Handler<Object> next) {
request.loadSessionData(new Handler<JsonObject>() {
@Override
public void handle(JsonObject session) {
if (session != null && session.getString("user") != null) {
request.response().redirect(303, "/");
return;
}
String next_url = request.getParameter("next", "/");
request.response().end("<html><form method=\"post\" action=\"/login\"><input type=\"hidden\" name=\"next\" value=\"" + next_url + "\"><input type=\"text\" placeholder=\"username\" name=\"username\"><input type=\"password\" placeholder=\"password\" name=\"password\"><button type=\"submit\">Login</button></form>");
}
});
}
});
post("/login", new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
JsonObject session = new JsonObject();
session.putString("user", request.getFormParameter("username"));
request.saveSessionData(session, new Handler<String>() {
@Override
public void handle(String status) {
if (!"ok".equals(status)) {
next.handle(status);
return;
}
request.response().redirect(303, request.getFormParameter("next", "/"));
}
});
}
});
get("/logout", new Middleware() {
@Override
public void handle(YokeRequest request, Handler<Object> next) {
request.destroySession();
request.response().redirect(303, "/");
}
});
get("/protected_resource", new Middleware() {
@Override
public void handle(YokeRequest request, Handler<Object> next) {
if (request.getParameter("access_token") != null) {
String accessToken = request.getParameter("access_token");
request.response().end(new JsonObject().putString("access_token", accessToken));
} else {
// no token found
next.handle(403);
}
}
});
}});
app.listen(8081);
}