feature #9: Create route for register
This commit is contained in:
@@ -145,7 +145,7 @@ fun Application.module() {
|
||||
// trace { application.log.trace(it.buildText()) }
|
||||
authenticate(optional = true) {
|
||||
article(get())
|
||||
auth(get())
|
||||
auth(get(), get())
|
||||
citizen(get())
|
||||
constitution(get())
|
||||
followArticle(get())
|
||||
|
||||
@@ -40,7 +40,7 @@ class Citizen(override var requester: Requester) : RepositoryI<CitizenEntity> {
|
||||
.selectOne("resource" to citizen)
|
||||
}
|
||||
|
||||
fun createWithUser(citizen: CitizenEntity): CitizenEntity? {
|
||||
fun insertWithUser(citizen: CitizenEntity): CitizenEntity? {
|
||||
return requester
|
||||
.getFunction("insert_citizen_with_user")
|
||||
.selectOne("resource" to citizen)
|
||||
|
||||
@@ -12,18 +12,27 @@ import io.ktor.request.receive
|
||||
import io.ktor.response.respondText
|
||||
import io.ktor.routing.Route
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
import fr.dcproject.entity.Citizen as CitizenEntity
|
||||
import fr.dcproject.repository.Citizen as CitizenRepository
|
||||
import fr.dcproject.repository.User as UserRepository
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@KtorExperimentalAPI
|
||||
fun Route.auth(repo: UserRepository) {
|
||||
fun Route.auth(userRepo: UserRepository, citizenRepo: CitizenRepository) {
|
||||
post <Paths.LoginRequest> {
|
||||
try {
|
||||
val credentials = call.receive<UserPasswordCredential>()
|
||||
val user = repo.findByCredentials(credentials) ?: throw BadRequestException("Username not exist or password is wrong")
|
||||
val user = userRepo.findByCredentials(credentials) ?: throw BadRequestException("Username not exist or password is wrong")
|
||||
call.respondText(JwtConfig.makeToken(user))
|
||||
} catch (e: MismatchedInputException) {
|
||||
throw BadRequestException("You must be send name and password to the request")
|
||||
}
|
||||
}
|
||||
|
||||
post <Paths.RegisterRequest> {
|
||||
val citizen = call.receive<CitizenEntity>()
|
||||
val created = citizenRepo.insertWithUser(citizen)?.user ?: throw BadRequestException("Bad request")
|
||||
|
||||
call.respondText(JwtConfig.makeToken(created))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import io.ktor.locations.Location
|
||||
@KtorExperimentalLocationsAPI
|
||||
object Paths {
|
||||
@Location("/login") class LoginRequest
|
||||
@Location("/register") class RegisterRequest
|
||||
|
||||
@Location("/articles") class ArticlesRequest(page: Int = 1, limit: Int = 50, val sort: String? = null, val direction: Direction? = null, val search: String? = null) {
|
||||
val page: Int = if (page < 1) 1 else page
|
||||
|
||||
Reference in New Issue
Block a user