Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 18bf803

Browse files
Coded verifying user
1 parent 0e983cf commit 18bf803

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.naturalprogrammer.spring5tutorial.controllers;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
8+
9+
import com.naturalprogrammer.spring5tutorial.services.UserService;
10+
import com.naturalprogrammer.spring5tutorial.utils.MyUtils;
11+
12+
@Controller
13+
@RequestMapping("/users")
14+
public class UserController {
15+
16+
private UserService userService;
17+
18+
public UserController(UserService userService) {
19+
this.userService = userService;
20+
}
21+
22+
@GetMapping("/{verificationCode}/verify")
23+
public String verify(@PathVariable String verificationCode,
24+
RedirectAttributes redirectAttributes) {
25+
26+
userService.verify(verificationCode);
27+
MyUtils.flash(redirectAttributes, "success", "verificationSuccessful");
28+
return "redirect:/";
29+
}
30+
}

‎src/main/java/com/naturalprogrammer/spring5tutorial/services/UserService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public interface UserService {
88

99
void signup(UserCommand userCommand);
1010
void afterApplicationReady(ApplicationReadyEvent event);
11+
void verify(String verificationCode);
1112
}

‎src/main/java/com/naturalprogrammer/spring5tutorial/services/UserServiceImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,22 @@ private void sendVerificationMail(User user) throws MessagingException {
110110
mailSender.send(user.getEmail(), MyUtils.getMessage("verifySubject"),
111111
MyUtils.getMessage("verifyBody", verificationLink));
112112
}
113+
114+
@Override
115+
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
116+
public void verify(String verificationCode) {
117+
118+
User currentUser = MyUtils.currentUser().get();
119+
120+
User user = userRepository.getOne(currentUser.getId());
121+
122+
MyUtils.validate(user.getRoles().contains(Role.UNVERIFIED), "alreadyVerified");
123+
MyUtils.validate(verificationCode.equals(user.getVerificationCode()), "wrongVerificationCode");
124+
125+
user.getRoles().remove(Role.UNVERIFIED);
126+
user.setVerificationCode(null);
127+
128+
userRepository.save(user);
129+
MyUtils.afterCommit(() -> MyUtils.login(user));
130+
}
113131
}

‎src/main/java/com/naturalprogrammer/spring5tutorial/utils/MyUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ public void afterCommit() {
7979
}
8080
);
8181
}
82+
83+
public static void validate(boolean valid, String messageKey, Object ... messageArgs) {
84+
85+
if (!valid)
86+
throw new RuntimeException(MyUtils.getMessage(messageKey, messageArgs));
87+
}
8288
}

‎src/main/resources/messages.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ verifyBody: Hi,<br/><br/>Your email at XYZ is unverified. Please click on\
88
the following link to get verified:<br/><br/>{0}
99

1010
pleaseVerify: Hey, please verify yourself!
11+
12+
verificationSuccessful: Verification succeeded!
13+
alreadyVerified: You are already verified!
14+
wrongVerificationCode: The verification code doesn't match

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /