Skip to content

Commit

Permalink
Merge pull request #22 from piashcse/fixing-login-http-method
Browse files Browse the repository at this point in the history
- Fixing login http method from get to post
  • Loading branch information
piashcse authored Oct 18, 2024
2 parents 969a529 + 5c7178c commit ff377c9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Project exclude paths
/.gradle/
/build/
/.idea/
/src/main/resources/hikari.properties
2 changes: 1 addition & 1 deletion src/main/kotlin/com/piashcse/models/user/body/LoginBody.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class LoginBody(
val password: String,
val userType: String
) {
init{
fun validate() {
validate(this) {
validate(LoginBody::email).isNotNull().isEmail()
validate(LoginBody::password).isNotNull().hasSize(4, 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import com.piashcse.models.shipping.AddShipping
import com.piashcse.models.shop.AddShop
import com.piashcse.models.shop.AddShopCategory
import com.piashcse.models.subcategory.AddProductSubCategory
import com.piashcse.models.user.body.LoginBody
import io.ktor.server.application.*
import io.ktor.server.plugins.requestvalidation.*

fun Application.configureRequestValidation() {
install(RequestValidation) {
validate<LoginBody> { login ->
login.validate()
ValidationResult.Valid
}
validate<AddProductCategory> { productCategory ->
productCategory.validation()
ValidationResult.Valid
Expand Down
21 changes: 5 additions & 16 deletions src/main/kotlin/com/piashcse/route/UserRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,18 @@ import io.ktor.server.routing.*

fun Route.userRoute(userController: UserController) {
route("user") {
get("Login", {
post("Login", {
tags("User")
request {
queryParameter<String>("email") {
required = true
}
queryParameter<String>("password") {
required = true
}
queryParameter<String>("userType") {
required = true
}
body<LoginBody>()
}
apiResponse()
}) {
val requiredParams = listOf("email", "password", "userType")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (email, password, userType) = requiredParams.map { call.parameters[it]!! }
val requestBody = call.receive<LoginBody>()
call.respond(
ApiResponse.success(
userController.login(LoginBody(email, password, userType)), HttpStatusCode.OK
userController.login(LoginBody(requestBody.email, requestBody.password, requestBody.userType)),
HttpStatusCode.OK
)
)
}
Expand Down

0 comments on commit ff377c9

Please sign in to comment.