-
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.
Merge pull request #20 from CodeURJC-DAW-2022-23/feat/functional_logi…
…n_register Feat/functional login register
- Loading branch information
Showing
24 changed files
with
756 additions
and
37 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
36 changes: 35 additions & 1 deletion
36
back/src/main/java/net/daw/alist/controllers/RegisterController.java
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 |
---|---|---|
@@ -1,14 +1,48 @@ | ||
package net.daw.alist.controllers; | ||
|
||
import lombok.AllArgsConstructor; | ||
import net.daw.alist.security.RegistrationRequest; | ||
import net.daw.alist.services.RegistrationService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.net.http.HttpResponse; | ||
import java.util.Optional; | ||
|
||
@AllArgsConstructor | ||
@Controller | ||
public class RegisterController { | ||
@Autowired | ||
private final RegistrationService registrationService; | ||
|
||
@GetMapping("/register") | ||
public String register() { | ||
return "register"; | ||
} | ||
|
||
@PostMapping("/register") | ||
public String registerPost(Model model, RegistrationRequest request) { | ||
String result = registrationService.register(request); | ||
if (result.equals("Success")){ | ||
model.addAttribute("notVerified", true); | ||
model.addAttribute("verifyMSG", "Please check your inbox to verify your email address"); | ||
return "/register"; | ||
} else{ | ||
model.addAttribute("error", true); | ||
model.addAttribute("errorMSG", result); | ||
return "/register"; | ||
} | ||
} | ||
|
||
@GetMapping("/registration/confirm") | ||
public String confirm(@RequestParam("token") String token, HttpServletResponse httpResponse) throws IOException { | ||
registrationService.confirmToken(token); | ||
httpResponse.sendRedirect("/sign-in"); | ||
return "/sign-in"; | ||
} | ||
|
||
} |
7 changes: 6 additions & 1 deletion
7
back/src/main/java/net/daw/alist/controllers/SignInController.java
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
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
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,6 @@ | ||
package net.daw.alist.models; | ||
|
||
public enum UserRole { | ||
USER, | ||
ADMIN | ||
} |
27 changes: 27 additions & 0 deletions
27
back/src/main/java/net/daw/alist/repositories/ConfirmationTokenRepository.java
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,27 @@ | ||
package net.daw.alist.repositories; | ||
|
||
import net.daw.alist.security.ConfirmationToken; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
@Transactional(readOnly = true) | ||
public interface ConfirmationTokenRepository | ||
extends JpaRepository<ConfirmationToken, Long> { | ||
|
||
Optional<ConfirmationToken> findByToken(String token); | ||
|
||
@Transactional | ||
@Modifying | ||
@Query("UPDATE ConfirmationToken c " + | ||
"SET c.confirmedAt = ?2 " + | ||
"WHERE c.token = ?1") | ||
int updateConfirmedAt(String token, | ||
LocalDateTime confirmedAt); | ||
} |
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
34 changes: 34 additions & 0 deletions
34
back/src/main/java/net/daw/alist/security/CSRFHandlerConfiguration.java
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,34 @@ | ||
package net.daw.alist.security; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.web.csrf.CsrfToken; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class CSRFHandlerConfiguration implements WebMvcConfigurer { | ||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(new CSRFHandlerInterceptor()); | ||
} | ||
} | ||
|
||
class CSRFHandlerInterceptor implements HandlerInterceptor { | ||
|
||
@Override | ||
public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, | ||
final ModelAndView modelAndView) throws Exception { | ||
|
||
if (modelAndView != null) { | ||
CsrfToken token = (CsrfToken) request.getAttribute("_csrf"); | ||
if (token != null) { | ||
modelAndView.addObject("token", token.getToken()); | ||
} | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
back/src/main/java/net/daw/alist/security/ConfirmationToken.java
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,56 @@ | ||
package net.daw.alist.security; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import net.daw.alist.models.User; | ||
|
||
import javax.persistence.*; | ||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@Entity | ||
public class ConfirmationToken { | ||
|
||
@SequenceGenerator( | ||
name = "confirmation_token_sequence", | ||
sequenceName = "confirmation_token_sequence", | ||
allocationSize = 1 | ||
) | ||
@Id | ||
@GeneratedValue( | ||
strategy = GenerationType.SEQUENCE, | ||
generator = "confirmation_token_sequence" | ||
) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private String token; | ||
|
||
@Column(nullable = false) | ||
private LocalDateTime createdAt; | ||
|
||
@Column(nullable = false) | ||
private LocalDateTime expiresAt; | ||
|
||
private LocalDateTime confirmedAt; | ||
|
||
@ManyToOne | ||
@JoinColumn( | ||
nullable = false, | ||
name = "app_user_id" | ||
) | ||
private User user; | ||
|
||
public ConfirmationToken(String token, | ||
LocalDateTime createdAt, | ||
LocalDateTime expiresAt, | ||
User appUser) { | ||
this.token = token; | ||
this.createdAt = createdAt; | ||
this.expiresAt = expiresAt; | ||
this.user = appUser; | ||
} | ||
} |
Oops, something went wrong.