-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthenticationController.java
37 lines (31 loc) · 1.49 KB
/
AuthenticationController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.music.review.app.controllers;
import com.music.review.app.domain.entities.users.User;
import com.music.review.app.domain.entities.users.dtos.DataLogin;
import com.music.review.app.infra.security.Tokens;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/login")
public class AuthenticationController {
private final AuthenticationManager manager;
private final Tokens tokens;
@Autowired
public AuthenticationController(AuthenticationManager manager, Tokens tokens){
this.manager = manager;
this.tokens = tokens;
}
@PostMapping("/do")
public ResponseEntity<String> login(@RequestBody @Valid DataLogin dataLogin){
var dataLoginSpring = new UsernamePasswordAuthenticationToken(dataLogin.email(), dataLogin.password());
var authentication = this.manager.authenticate(dataLoginSpring);
var token = this.tokens.gerarToken((User) authentication.getPrincipal());
return ResponseEntity.ok(token);
}
}