Added user registration
This commit is contained in:
parent
174955d1eb
commit
ef039478d5
@ -7,6 +7,7 @@ import org.springframework.security.access.annotation.Secured
|
|||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||||
import org.springframework.stereotype.Controller
|
import org.springframework.stereotype.Controller
|
||||||
import org.springframework.ui.Model
|
import org.springframework.ui.Model
|
||||||
|
import org.springframework.validation.BindingResult
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping
|
import org.springframework.web.bind.annotation.DeleteMapping
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute
|
import org.springframework.web.bind.annotation.ModelAttribute
|
||||||
@ -24,7 +25,9 @@ import javax.validation.constraints.NotEmpty
|
|||||||
@Controller
|
@Controller
|
||||||
class BaseController
|
class BaseController
|
||||||
@Autowired constructor(
|
@Autowired constructor(
|
||||||
val imageRepository: ImageRepository
|
val imageRepository: ImageRepository,
|
||||||
|
val memberRepository: MemberRepository,
|
||||||
|
val roleRepository: RoleRepository
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
@ -40,6 +43,23 @@ class BaseController
|
|||||||
model.addAttribute("redirect", redirect)
|
model.addAttribute("redirect", redirect)
|
||||||
return "login"
|
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 =
|
fun userCanEdit(userDetails: CustomUserDetails?, image: Image): Boolean =
|
||||||
|
@ -12,14 +12,20 @@ import javax.persistence.JoinTable
|
|||||||
import javax.persistence.ManyToMany
|
import javax.persistence.ManyToMany
|
||||||
import javax.persistence.ManyToOne
|
import javax.persistence.ManyToOne
|
||||||
import javax.persistence.SequenceGenerator
|
import javax.persistence.SequenceGenerator
|
||||||
|
import javax.validation.constraints.Email
|
||||||
|
import javax.validation.constraints.NotBlank
|
||||||
import javax.validation.constraints.Pattern
|
import javax.validation.constraints.Pattern
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
open class Member(
|
open class Member(
|
||||||
@Column(unique = true)
|
@Column(unique = true)
|
||||||
|
@field:NotBlank(message = "Username is required")
|
||||||
open var name: String = "",
|
open var name: String = "",
|
||||||
|
@field:NotBlank(message = "Email address is required")
|
||||||
|
@field:Email(message = "Email address invalid")
|
||||||
open var email: String = "",
|
open var email: String = "",
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
|
@field:NotBlank(message = "Password is required")
|
||||||
open var password: String = "",
|
open var password: String = "",
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
open var enabled: Boolean = true,
|
open var enabled: Boolean = true,
|
||||||
|
@ -11,7 +11,9 @@ interface MemberRepository : CrudRepository<Member, Long> {
|
|||||||
fun findByName(name: String): Member?
|
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> {
|
interface ImageRepository : JpaRepository<Image, Long> {
|
||||||
@Suppress("SqlResolve")
|
@Suppress("SqlResolve")
|
||||||
|
@ -43,6 +43,7 @@
|
|||||||
<input class="form-control form-control-sm" id="password" name="password" placeholder="Password" type="password" autocomplete="current-password">
|
<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}"/>
|
<input name="${_csrf.parameterName}" type="hidden" value="${_csrf.token}"/>
|
||||||
<button class="btn btn-primary btn-sm text-nowrap" type="submit">Log In</button>
|
<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>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
// Set logging in via this page to redirect back to this exact page
|
// Set logging in via this page to redirect back to this exact page
|
||||||
|
@ -19,6 +19,8 @@
|
|||||||
<a class="text-decoration-none" th:href="${#mvc.url('UC#showUploadPage').build()}">Upload New</a>
|
<a class="text-decoration-none" th:href="${#mvc.url('UC#showUploadPage').build()}">Upload New</a>
|
||||||
<span class="text-secondary"> |</span>
|
<span class="text-secondary"> |</span>
|
||||||
<a class="text-decoration-none" th:href="${#mvc.url('MC#memberDetails').build()}">My Account</a>
|
<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>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
49
src/main/resources/templates/register.html
Normal file
49
src/main/resources/templates/register.html
Normal 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>
|
Loading…
Reference in New Issue
Block a user