Consider the following nested flatmap structure:
val isValid: F[Boolean] = userRepository.isValid(username, password)
isValid.flatMap(valid =>
if (valid) {
userRepository.getClaims(username).flatMap(claims => {
val token = JWTRefreshService.createToken(claims)
Created(token)
}
)
} else {
Unauthorized(headers.`WWW-Authenticate`(NonEmptyList.of(Challenge(scheme = "Bearer", realm =
"Access to authorize a request"))))
}
)
where F is F[_] : Sync.
How can i rewrite this structure into a for-comprehension. I cant figure out how to rewrite the if else clause without creating a nested for-comprehension.