I have a login page in a nextjs app running on port 3000. The login page calls /sign-in api on vertx server on port 8082. After successful login, the user is redirect from login page to dashboard page which on same nexjs app. From dashboard, I call other secure apis on the vertx server. However, vertx is creating new session for every request -- meaning after user is redirected to dashboard page, the session data created in the login page is not available, so repeating the login cycle. Complete code:
public class HttpServerVerticle extends AbstractVerticle {
private static final Logger LOGGER = LogManager.getLogger(HttpServerVerticle.class);
public static final String CONFIG_HTTP_SERVER_PORT = "http.server.port";
@Override
public void start(Promise<Void> promise) throws Exception {
Router router = Router.router(vertx);
LocalSessionStore sessionStore = LocalSessionStore.create(vertx);
SessionHandler sessionHandler = SessionHandler.create(sessionStore)
.setCookieSecureFlag(true)
.setCookieHttpOnlyFlag(true)
.setCookieSameSite(CookieSameSite.NONE)
.setSessionCookiePath("/");//setSessionCookieName("vid");
router.route().handler(sessionHandler);
Handler<RoutingContext> loginHandler = ctx -> {
Session session = ctx.session();
System.out.println("Session at login handler for " + ctx.request().absoluteURI());
System.out.println(session.id());
System.out.println(session.value());
if(session != null && session.get("my-session") != null){
ctx.next();
}
else {
ctx.response()
.setStatusCode(302)
.putHeader("Access-Control-Allow-Origin", "*")
.putHeader("Access-Control-Allow-Methods", "*")
.putHeader("Access-Control-Allow-Headers", "*")
.putHeader("Location", "http://localhost:3000/login")
.end();
}
};
router.route("/api/*").handler(loginHandler);
CorsHandler corsHandler = CorsHandler.create()
.addOrigin("http://localhost:3000")
.allowedMethod(HttpMethod.POST)
.allowedMethod(HttpMethod.GET)
.allowedMethod(HttpMethod.OPTIONS)
.allowedHeader("Content-Type")
.allowCredentials(true);
router.route().handler(corsHandler);
router.post().handler(BodyHandler.create());
router.post("/v/sign-in").handler(this::logIn);
router.post("/api/do-protected-work").handler(this::createLinkToken);
int portNumber = System.getenv("HTTP_PLATFORM_PORT") != null ?
Integer.valueOf(System.getenv("HTTP_PLATFORM_PORT")) : config().getInteger(CONFIG_HTTP_SERVER_PORT, 8082);
System.out.println("Connecting to por number: " + portNumber);
HttpServer server = vertx.createHttpServer();
server
.requestHandler(router)
.listen(portNumber, ar -> {
if (ar.succeeded()) {
LOGGER.info("HTTP server running on port " + portNumber);
promise.complete();
} else {
LOGGER.error("Could not start a HTTP server", ar.cause());
promise.fail(ar.cause());
}
});
}
private void createLinkToken(RoutingContext context) {
System.out.println("Session=====================================================================");
System.out.println(context.session().id());
System.out.println(context.session().value());
System.out.println((String)context.session().get("my-session"));
JsonObject request = context.body().asJsonObject();
LOGGER.info("Create link token request received -- {}.", request);
vertx.eventBus().request("_set_plaid_link_token_", request, new DeliveryOptions(), reply -> {
if(reply.succeeded()){
context.response().putHeader("content-type", "application/json; charset=utf-8");
context.response().putHeader("Access-Control-Allow-Origin", "*");
context.response().putHeader("Access-Control-Allow-Methods", "*");
context.response().putHeader("Access-Control-Allow-Headers", "*");
context.response().setStatusCode(200).end(Json.encodePrettily(reply.result().body()));
} else {
LOGGER.info(reply.cause().getMessage());
context.response().setStatusCode(400).end(Json.encodePrettily(reply.cause().getMessage()));
}
});
}
private void logIn(RoutingContext context) {
Session session = context.session();
System.out.println("Session at login");
System.out.println(session.id());
System.out.println(session.value());
JsonObject request = context.body().asJsonObject();
LOGGER.info("Send verification text request received -- {}.", request);
String textCode = request.getString("textCode");
LOGGER.info("Text code is {}", textCode);
request.put("textCode", textCode);
vertx.eventBus().<JsonObject>request("__address___", request, new DeliveryOptions(), sendReply -> {
if(sendReply.succeeded()){
context.response().putHeader("content-type", "application/json; charset=utf-8");
JsonObject response = new JsonObject();
String id = sendReply.result().body().getString("result");
if(null != id) {
System.out.println("putting the id in the session");
response.put("Result", "Succeeded");
context.session().put("my-session", id);
}
else {
response.put("Result", "Failed");
}
context.response().setStatusCode(200).end(Json.encodePrettily(response));
}
else {
LOGGER.info(sendReply.cause().getMessage());
context.response().setStatusCode(400).end(Json.encodePrettily(sendReply.cause().getMessage()));
}
});
}
}
Help!!!
Session handler depends on cookie store, which is missing
Add
router.route().handler(CookieHandler.create())
before the session handler