Skip to content

Commit e44f5fe

Browse files
author
herilmuratovic
committed
Service for getting user data by username and readme updated.
1 parent 61e0906 commit e44f5fe

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Spring Boot project with demonstration of OTP authentication technique.
55

66
## Running the project
77

8-
1. Create database with name **test**
8+
1. Create database with name **otp**
99
2. Open terminal and navigate to your project
1010
3. Type command **mvn install**
1111
4. Type command **mvn spring-boot:run**
@@ -34,12 +34,13 @@ Response: `{ id_token: "token_hash" }`
3434

3535
1. Generate OTP and send it to e-mail <br>
3636
Route: /api/otp/generate <br>
37-
Method: GET <br><br>
37+
Method: POST <br><br>
38+
Empty request body in this case.
3839

3940
2. Validate OTP <br>
4041
Route: /api/otp/validate <br>
4142
Method: POST <br>
42-
Example Payload: { "otp": "your otp number" }
43+
Example Request Payload: { "otp": "your otp number" }
4344
<br>
4445

4546

src/main/java/com/starter/springboot/services/OtpService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ public class OtpService {
1717

1818
private OtpGenerator otpGenerator;
1919
private EmailService emailService;
20+
private UserService userService;
2021

2122
/**
2223
* Constructor dependency injector
2324
* @param otpGenerator - otpGenerator dependency
2425
* @param emailService - email service dependency
26+
* @param userService - user service dependency
2527
*/
26-
public OtpService(OtpGenerator otpGenerator, EmailService emailService)
28+
public OtpService(OtpGenerator otpGenerator, EmailService emailService, UserService userService)
2729
{
2830
this.otpGenerator = otpGenerator;
2931
this.emailService = emailService;
32+
this.userService = userService;
3033
}
3134

3235
/**
@@ -48,7 +51,7 @@ public Boolean generateOtp(String key)
4851
LOGGER.info("Generated OTP: {}", otpValue);
4952

5053
// fetch user e-mail from database
51-
String userEmail = "[email protected]";
54+
String userEmail = userService.findEmailByUsername(key);
5255
List<String> recipients = new ArrayList<>();
5356
recipients.add(userEmail);
5457

src/main/java/com/starter/springboot/services/UserService.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.stereotype.Service;
77

88
import java.util.List;
9+
import java.util.Optional;
910

1011
@Service
1112
public class UserService {
@@ -22,4 +23,18 @@ public List<User> findAllUsers() {
2223
return this.userRepository.findAll();
2324
}
2425

26+
/**
27+
* Method for getting e-mail by username (key)
28+
*
29+
* @param username - provided username
30+
* @return e-mail
31+
*/
32+
public String findEmailByUsername(String username)
33+
{
34+
Optional<User> user = userRepository.findByUsername(username);
35+
if (user.isPresent()) {
36+
return user.get().getEmail();
37+
}
38+
return null;
39+
}
2540
}

src/main/resources/application-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ spring:
33
restart:
44
enabled: true
55
datasource:
6-
url: jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf8&useSSL=false
6+
url: jdbc:mysql://localhost/otp?useUnicode=true&characterEncoding=utf8&useSSL=false
77
name:
88
username: root
99
password: root
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"id";"first_name";"last_name";"username";"email";"password";"enabled";"last_password_reset_date"
2-
"1";"Administrator";"";"admin";"admin@admin.com";"$2a$10$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC";true;NULL
2+
"1";"Administrator";"";"admin";"heril.muratovic@logate.com";"$2a$10$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC";true;NULL

0 commit comments

Comments
 (0)