-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modify the controller's routing information for authentication
- Loading branch information
Showing
13 changed files
with
413 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
app/src/main/kotlin/io/sakurasou/controller/AuthController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.sakurasou.controller | ||
|
||
import io.github.smiley4.ktorswaggerui.dsl.routing.post | ||
import io.github.smiley4.ktorswaggerui.dsl.routing.route | ||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.sakurasou.controller.request.UserInsertRequest | ||
import io.sakurasou.controller.request.UserLoginRequest | ||
import io.sakurasou.model.dao.relation.RelationDao | ||
import io.sakurasou.model.dao.user.UserDao | ||
import io.sakurasou.service.auth.AuthService | ||
import io.sakurasou.service.auth.AuthServiceImpl | ||
|
||
/** | ||
* @author Shiina Kin | ||
* 2024/9/12 10:14 | ||
*/ | ||
fun Route.authRoute(userDao: UserDao, relationDao: RelationDao) { | ||
val authService = AuthServiceImpl(userDao, relationDao) | ||
val authController = AuthController(authService) | ||
route("user", { | ||
protected = false | ||
}) { | ||
post("login", { | ||
request { | ||
body<UserLoginRequest> { | ||
required = true | ||
} | ||
} | ||
}) { | ||
val loginRequest = call.receive<UserLoginRequest>() | ||
val token = authController.handleLogin(loginRequest) | ||
call.respond(mapOf("token" to token)) | ||
} | ||
post("signup", { | ||
protected = false | ||
request { | ||
body<UserInsertRequest> { | ||
required = true | ||
} | ||
} | ||
}) { | ||
val userInsertRequest = call.receive<UserInsertRequest>() | ||
authController.handleSignup(userInsertRequest) | ||
} | ||
} | ||
} | ||
|
||
class AuthController( | ||
private val authService: AuthService | ||
) { | ||
suspend fun handleLogin(loginRequest: UserLoginRequest): String { | ||
return authService.login(loginRequest) | ||
} | ||
|
||
suspend fun handleSignup(userInsertRequest: UserInsertRequest) { | ||
authService.saveUser(userInsertRequest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.