Hi,
this is not an issue but a question that I've been struggling the last days and don't seem to find a solution.
So far everything is good with Renarde, I implemented a login mechanism using the database as it was the previous mean we used (I am porting the application we had).
I based my login mechanism on the todo example and my code looks like this
@POST
public Response manualLogin(@RestForm String username, @RestForm String password) {
var action = (this.getClass().getSimpleName() + "." + getInstance().walk(frames -> frames.findFirst().map(StackWalker.StackFrame::getMethodName)).get()).replace("_Subclass", "");
var timer = registry.timer(applicationName + "." + action);
return timer.record(() -> {
var user = User.findByUsername(username);
if (user == null || !matches(password, user.credentials.password)) {
validation.addError("username", "Invalid username/password");
prepareForErrorRedirect();
login();
}
var cookie = security.makeUserCookie(user);
URI uri = null;
if (ADMINISTRATOR.equalsIgnoreCase(user.credentials.role)) {
uri = getURI(Administrators::display_dashboard);
} else if (INSURANCE.equalsIgnoreCase(user.credentials.role) || INSURANCE_EMPLOYEE.equalsIgnoreCase(user.credentials.role)) {
uri = getURI(Insurances::display_dashboard);
} else if (MECHANIC.equalsIgnoreCase(user.credentials.role) || MECHANIC_EMPLOYEE.equalsIgnoreCase(user.credentials.role)) {
uri = getURI(Mechanics:: display_dashboard);
} else if (EXPERT.equalsIgnoreCase(user.credentials.role) || EXPERT_EMPLOYEE.equalsIgnoreCase(user.credentials.role)) {
uri = getURI(Experts:: display_dashboard);
}
return Response.seeOther(uri).cookie(cookie).build();
});
}
Everything works fine, in the browser panel I can see the cookie value and all that.
I am now trying to get the user data in an Class extending the ControllerWithUser one.
I tried this
@POST
@Consumes(MULTIPART_FORM_DATA)
public TemplateInstance search(final SearchCompanies request) {
var action = (this.getClass().getSimpleName() + "." + getInstance().walk(frames -> frames.findFirst().map(StackWalker.StackFrame::getMethodName)).get()).replace("_Subclass", "");
logEntry(LOGGER, applicationName, profile, action, request);
var timer = registry.timer(applicationName + "." + action);
return timer.record(() -> {
String requestUser = security.getUser().getUserId(); // <-- from @Inject RenardeSecurity security;
request.requestUser = requestUser;
GatewayResponse response = client.search(request);
TemplateInstance templateInstance = Templates.search()
.data(UI_USER, userUtility.getUIUser(requestUser))
.data("insurances", response.entity)
.data("request", request);
count(registry, applicationName, profile, action, response);
logExit(LOGGER, applicationName, profile, action, request, response.entity != null ? response.entity : response.errorMessage);
return templateInstance;
});
}
and this
@POST
@Consumes(MULTIPART_FORM_DATA)
public TemplateInstance search(final SearchCompanies request) {
var action = (this.getClass().getSimpleName() + "." + getInstance().walk(frames -> frames.findFirst().map(StackWalker.StackFrame::getMethodName)).get()).replace("_Subclass", "");
logEntry(LOGGER, applicationName, profile, action, request);
var timer = registry.timer(applicationName + "." + action);
return timer.record(() -> {
String requestUser = getUser().getUserId(); // <-- difference between the two implementations
request.requestUser = security.getUser().getUserId();
GatewayResponse response = client.search(request);
TemplateInstance templateInstance = Templates.search()
.data(UI_USER, userUtility.getUIUser(requestUser))
.data("insurances", response.entity)
.data("request", request);
count(registry, applicationName, profile, action, response);
logExit(LOGGER, applicationName, profile, action, request, response.entity != null ? response.entity : response.errorMessage);
return templateInstance;
});
}
But the value for requestUser is always returning null or an NPE is thrown. I need the data of the user to be set in a Request that I have to forward to some other microservices. The Class extending ControllerWithUser was first annotated with @authenticated and then not but it doesn't seem to make any difference.
Any idea on what I am missing?
Regards,
D.