Added user registration

This commit is contained in:
neviyn 2021-05-11 19:03:41 +01:00
parent 174955d1eb
commit ef039478d5
6 changed files with 82 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import org.springframework.security.access.annotation.Secured
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
@ -24,7 +25,9 @@ import javax.validation.constraints.NotEmpty
@Controller
class BaseController
@Autowired constructor(
val imageRepository: ImageRepository
val imageRepository: ImageRepository,
val memberRepository: MemberRepository,
val roleRepository: RoleRepository
) {
@GetMapping("/")
@ -40,6 +43,23 @@ class BaseController
model.addAttribute("redirect", redirect)
return "login"
}
@GetMapping("/register")
fun showRegister(model: Model) : String {
val user = Member(name = "", email = "", password = "")
model.addAttribute("user_details", user)
model.addAttribute("submitted", false)
return "register"
}
@PostMapping("/register")
fun doRegister(@Valid @ModelAttribute("user_details") newUser: Member, bindingResult: BindingResult): String {
if(bindingResult.hasErrors()) return "register"
newUser.password = passwordEncoder().encode(newUser.password)
newUser.roles.add(roleRepository.findByNameIs("USER"))
memberRepository.save(newUser)
return "login"
}
}
fun userCanEdit(userDetails: CustomUserDetails?, image: Image): Boolean =

View File

@ -12,14 +12,20 @@ import javax.persistence.JoinTable
import javax.persistence.ManyToMany
import javax.persistence.ManyToOne
import javax.persistence.SequenceGenerator
import javax.validation.constraints.Email
import javax.validation.constraints.NotBlank
import javax.validation.constraints.Pattern
@Entity
open class Member(
@Column(unique = true)
@field:NotBlank(message = "Username is required")
open var name: String = "",
@field:NotBlank(message = "Email address is required")
@field:Email(message = "Email address invalid")
open var email: String = "",
@JsonIgnore
@field:NotBlank(message = "Password is required")
open var password: String = "",
@JsonIgnore
open var enabled: Boolean = true,

View File

@ -11,7 +11,9 @@ interface MemberRepository : CrudRepository<Member, Long> {
fun findByName(name: String): Member?
}
interface RoleRepository : CrudRepository<Role, Long>
interface RoleRepository : CrudRepository<Role, Long> {
fun findByNameIs(name: String) : Role
}
interface ImageRepository : JpaRepository<Image, Long> {
@Suppress("SqlResolve")

View File

@ -43,6 +43,7 @@
<input class="form-control form-control-sm" id="password" name="password" placeholder="Password" type="password" autocomplete="current-password">
<input name="${_csrf.parameterName}" type="hidden" value="${_csrf.token}"/>
<button class="btn btn-primary btn-sm text-nowrap" type="submit">Log In</button>
<a class="btn btn-secondary btn-sm" th:href="@{/register}">Register</a>
</div>
<script>
// Set logging in via this page to redirect back to this exact page

View File

@ -19,6 +19,8 @@
<a class="text-decoration-none" th:href="${#mvc.url('UC#showUploadPage').build()}">Upload New</a>
<span class="text-secondary"> |</span>
<a class="text-decoration-none" th:href="${#mvc.url('MC#memberDetails').build()}">My Account</a>
<span class="text-secondary"> |</span>
<a class="text-decoration-none" th:href="${#mvc.url('BC#showRegister').build()}">Create Account</a>
</p>
</div>
</div>

View File

@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta th:replace="fragments :: header"/>
<title>Register</title>
</head>
<body>
<div th:replace="fragments :: navbar"></div>
<div class="container-fluid">
<div class="row mt-3">
<div class="col">
<h2 class="display-2 text-center">Create Account</h2>
</div>
</div>
<div class="row justify-content-center mt-3">
<div class="col-6 text-center">
<form method="post" novalidate th:action="@{/register}"
th:object="${user_details}">
<div class="mb-3 form-floating text-center">
<input class="form-control form-control-lg" id="emailInput" required th:field="*{email}"
th:classappend="${#fields.hasAnyErrors()} ? 'is-valid' : ''" th:errorclass="is-invalid" type="email"/>
<label for="emailInput">Email address</label>
<div class="invalid-feedback" th:errors="*{email}" th:if="${#fields.hasErrors('email')}">Email Error
</div>
</div>
<div class="mb-3 form-floating">
<input class="form-control form-control-lg" id="usernameInput" required th:field="*{name}"
th:classappend="${#fields.hasAnyErrors()} ? 'is-valid' : ''" th:errorclass="is-invalid" type="text"/>
<label class="form-label" for="usernameInput">Username</label>
<div class="invalid-feedback" th:errors="*{name}" th:if="${#fields.hasErrors('name')}">Username
Error
</div>
</div>
<div class="mb-3 form-floating">
<input class="form-control form-control-lg" id="passwordInput" required
th:classappend="${#fields.hasAnyErrors()} ? 'is-valid' : ''" th:errorclass="is-invalid" th:field="*{password}" type="password"/>
<label class="form-label" for="passwordInput">Password</label>
<div class="invalid-feedback" th:errors="*{password}" th:if="${#fields.hasErrors('password')}">Password
Error
</div>
</div>
<button class="btn btn-primary btn-lg" type="submit">Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>