Skip to content

Commit d4eb8e0

Browse files
authored
Update LoginResource.java
1 parent 41083ac commit d4eb8e0

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

src/main/java/com/hemant/db/resource/LoginResource.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.hemant.db.model.Employee;
44
import com.hemant.db.repository.EmployeeRepository;
5+
import com.hemant.db.model.Response;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.web.bind.MethodArgumentNotValidException;
78
import org.springframework.web.bind.annotation.*;
@@ -25,25 +26,34 @@ public Employee fetchByEmail(@Valid @RequestParam("email") String email) {
2526
}
2627

2728
@PostMapping("/login")
28-
public ResponseEntity<String> login(@Valid @RequestParam("email") String email, @Valid @RequestParam("password") String password) {
29+
public ResponseEntity<Response> login(@Valid @RequestParam("email") String email, @Valid @RequestParam("password") String password) {
30+
Response r = new Response();
2931
Employee e = EmployeeRepository.findByEmail(email);
3032
try {
3133
String correctPassword = e.getPassword();
3234
if(e.getStatus().equals("Logged In")) {
33-
return new ResponseEntity<>("Error: User Already logged in", HttpStatus.FORBIDDEN);
35+
r.setStatus(false);
36+
r.setEmployee(e);
37+
return new ResponseEntity<>(r, HttpStatus.FORBIDDEN);
3438
}
3539
else {
3640
if(password.equals(correctPassword)) {
3741
e.setStatus("Logged In");
3842
EmployeeRepository.save(e);
39-
return new ResponseEntity<>("Logged In\nUsername: " + e.getEmail() + "\nName: " + e.getName(), HttpStatus.OK);
43+
r.setStatus(true);
44+
r.setEmployee(e);
45+
return new ResponseEntity<>(r, HttpStatus.OK);
4046
}
4147
else {
42-
return new ResponseEntity<>("Login Failure, Wrong email or password", HttpStatus.BAD_REQUEST);
48+
r.setStatus(false);
49+
r.setMessage("Incorrect Email or Password");
50+
return new ResponseEntity<>(r, HttpStatus.BAD_REQUEST);
4351
}
4452
}
4553
} catch(Exception e1) {
46-
return new ResponseEntity<>("Error:" + e1.getMessage(), HttpStatus.BAD_REQUEST);
54+
r.setStatus(false);
55+
r.setMessage("Something went wrong " + e1.getMessage());
56+
return new ResponseEntity<>(r, HttpStatus.BAD_REQUEST);
4757
}
4858

4959
}

0 commit comments

Comments
 (0)