IsaacMart
09/29/2021, 3:28 AMfun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@KtorExperimentalAPI
@KtorExperimentalLocationsAPI
@Suppress("unused") // Referenced in application.conf
fun Application.module() {
install(Locations) {
}
install(Sessions) {
cookie<MySession>("MY_SESSION") {
cookie.extensions["SameSite"] = "lax"
}
}
DatabaseFactory.init()
val userRepository = UserRepositoryImp()
val taskerRepository = TaskerRepositoryImp()
val requestRepository = RequestRepositoryImp()
val messageRepository = MessageRepositoryImp()
val reviewRepository = ReviewRepositoryImp()
val jwtService = JwtService()
val hashFunction = { s: String -> hash(s) }
install(Authentication) {
jwt("jwt") {
verifier(jwtService.verifier)
realm = "Tasksfy"
validate {
val payload = it.payload
val claim = payload.getClaim("id")
val claimString = claim.asInt()
val user = userRepository.getUser(claimString)
user
}
}
}
}
My userRoute class for login.Where am storing the session id
@KtorExperimentalLocationsAPI
fun Route.users(db: UserRepository, jwtService: JwtService, hashFunction: (String) -> String) {
post<UserLoginRoute> {
val signInParameters = call.receive<Parameters>()
val password =
signInParameters["password"] ?: return@post call.respond(HttpStatusCode.Unauthorized, "Missing Fields")
val phoneNumber = signInParameters["phone_number"] ?: return@post call.respond(
HttpStatusCode.Unauthorized,
"Missing Fields"
)
val hash = hashFunction(password)
try {
val currentUser = db.getUserByPhone(phoneNumber)
currentUser?.user_id?.let {
if (currentUser.password == hash) {
call.sessions.set(MySession(it)) //Storing the user id here
val token = jwtService.generateToken(currentUser)
// call.respondText(jwtService.generateToken(currentUser))
call.respond(
ResponseDefault(
BaseResponse(true, "Welcome back " + currentUser.first_name, token),
currentUser
)
)
} else {
call.respond(BaseResponse(false, "Invalid Login details!", ""))
}
}
} catch (e: Throwable) {
application.log.error("Failed to register user", e)
call.respond(BaseResponse(false, "Problem retrieving User.Try again later! " + e.message,""))
}
}
My Session Class
data class MySession(val userId : Int) {
}
Here is my depedencies incase there could be problemAleksei Tirman [JB]
09/29/2021, 1:17 PMIsaacMart
09/29/2021, 2:07 PMget<RequestsRoute> {
val userId = call.sessions.get<MySession>()?.userId
try {
val requests = userId?.let { it1 -> db.getAllRequestsById(it1) }
call.respond(ResponseDefault(BaseResponse(true, "Requests retrieved successfully", userId.toString()), requests))
} catch (e: Throwable) {
application.log.error("Failed to retrieve requests ", e)
call.respond(BaseResponse(false, "Problems retrieving requests " + e.message, ""))
}
}
Here is a link to my depedencies
https://kotlinlang.slack.com/archives/C0A974TJ9/p1632886098331100Aleksei Tirman [JB]
09/29/2021, 3:23 PMcall.sessions.set
call is actually executed. Please share a complete project or remove database dependency, otherwise it's very difficult to troubleshoot your problem.IsaacMart
09/29/2021, 3:51 PMAleksei Tirman [JB]
09/30/2021, 2:34 PM<http://localhost:8080/v1/users/create>
to create a user, then POST request to <http://localhost:8080/v1/users/login>
to update the session and a GET request to <http://localhost:8080/v1/users>
to display greetings for a user.IsaacMart
10/03/2021, 6:27 PMAleksei Tirman [JB]
10/04/2021, 7:46 AMIsaacMart
10/04/2021, 8:01 AMAleksei Tirman [JB]
10/12/2021, 2:28 PMcom.github.AfricasTalkingLtd.africastalking-java:core
artifact. As a quick workaround, you can use the Jetty engine (this is already fixed in the attached project).IsaacMart
10/12/2021, 2:37 PM