Skip to content

Commit

Permalink
feat: forget password api
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Oct 13, 2024
1 parent d317de9 commit 35976af
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/main/java/fuck/manthe/nmsl/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
http
.authorizeHttpRequests(conf -> conf
.requestMatchers("/user/redeem").permitAll()
.requestMatchers("/user/forgetPassword").anonymous()
.requestMatchers("/user/**").authenticated()
.requestMatchers("/dashboard").authenticated()
.requestMatchers("/admin/**").hasAuthority("ADMIN")
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/fuck/manthe/nmsl/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fuck.manthe.nmsl.entity.RedeemCode;
import fuck.manthe.nmsl.entity.RestBean;
import fuck.manthe.nmsl.entity.User;
import fuck.manthe.nmsl.entity.dto.ForgetPasswordDTO;
import fuck.manthe.nmsl.entity.dto.RedeemDTO;
import fuck.manthe.nmsl.entity.webhook.UserRegisterMessage;
import fuck.manthe.nmsl.entity.webhook.UserRenewMessage;
Expand Down Expand Up @@ -83,4 +84,22 @@ public ResponseEntity<RestBean<String>> redeem(@RequestBody RedeemDTO dto) throw
}
return new ResponseEntity<>(RestBean.failure(409, "User exists or wrong password"), HttpStatus.CONFLICT);
}

@PostMapping("forgetPassword")
public ResponseEntity<RestBean<String>> forgetPassword(@RequestBody ForgetPasswordDTO dto) {
User user = userService.findByUsername(dto.getUsername());
if (user == null) {
return new ResponseEntity<>(RestBean.failure(404, "User not found."), HttpStatus.NOT_FOUND);
}

RedeemCode redeemCode = redeemService.infoOrNull(dto.getRedeemCode());
if (redeemCode == null || redeemCode.isAvailable() || redeemCode.getRedeemer().getId().equals(user.getId())) {
// 邀请码找不到或者根本没被人用过
// 写到一起是为了防止被刷API
return new ResponseEntity<>(RestBean.failure(404, "Code not found."), HttpStatus.NOT_FOUND);
}
user.setPassword(passwordEncoder.encode(dto.getPassword()));
userService.save(user);
return ResponseEntity.ok(RestBean.success("Password reset successfully"));
}
}
26 changes: 18 additions & 8 deletions src/main/java/fuck/manthe/nmsl/controller/WebController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,39 @@ public String index() {
}

@GetMapping("colddown")
public String coldDown() {
return "colddown";
public String coldDownRedirect() {
return "redirect:/user/colddown";
}

@GetMapping("queue")
public String queue() {
return "queue";
@GetMapping("user/colddown")
public String coldDown() {
return "user/colddown";
}

// @GetMapping("queue")
// public String queue() {
// return "queue";
// }

@Deprecated
@GetMapping("redeem")
public String register() {
public String redeemRedirect() {
return "redirect:/user/redeem";
}

@GetMapping("user/redeem")
public String redeem() {
return "redeem";
return "user/redeem";
}

@GetMapping("user/login")
public String login() {
return "login";
return "user/login";
}

@GetMapping("user/forgetPassword")
public String forgetPassword() {
return "user/forget-password";
}

@GetMapping("maintain")
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/fuck/manthe/nmsl/entity/dto/ForgetPasswordDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fuck.manthe.nmsl.entity.dto;

import lombok.Data;

@Data
public class ForgetPasswordDTO {
private String username;
private String redeemCode;

private String password; // new password
}
File renamed without changes.
10 changes: 10 additions & 0 deletions src/main/resources/templates/user/forget-password.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>找回密码</title>
</head>
<body>
<h1>WIP</h1>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<div class="mdui-container">
<div class="mdui-card login-card">
<div class="mdui-card-primary">
<div class="mdui-card-primary-title">登录到DingZhenServlet</div>
<div class="mdui-card-primary-title">登录到顶针服务</div>
<div class="mdui-card-primary-subtitle">请输入您的用户名和密码</div>
</div>
<div class="mdui-card-content">
Expand All @@ -41,6 +41,7 @@
<label class="mdui-textfield-label">密码</label>
<input class="mdui-textfield-input" name="password" required type="password"/>
</div>
<div><a href="/user/forgetPassword">忘记密码</a></div>
<div class="mdui-p-t-3">
<button class="mdui-btn mdui-btn-raised mdui-btn-block mdui-color-theme-accent" type="submit">登录
</button>
Expand Down
File renamed without changes.

0 comments on commit 35976af

Please sign in to comment.