File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed
Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Mvc ;
2+ using OsmoDoc . API . Models ;
3+ using OsmoDoc . API . Helpers ;
4+
5+ namespace OsmoDoc . API . Controllers ;
6+
7+ [ Route ( "api" ) ]
8+ [ ApiController ]
9+ public class LoginController : ControllerBase
10+ {
11+ private readonly ILogger < LoginController > _logger ;
12+
13+ public LoginController ( ILogger < LoginController > logger )
14+ {
15+ this . _logger = logger ;
16+ }
17+
18+ [ HttpPost ]
19+ [ Route ( "login" ) ]
20+ public async Task < ActionResult < BaseResponse > > Login ( [ FromBody ] LoginRequestDTO loginRequest )
21+ {
22+ BaseResponse response = new BaseResponse ( ResponseStatus . Fail ) ;
23+ try
24+ {
25+ string token = await Task . Run ( ( ) => AuthenticationHelper . JwtTokenGenerator ( loginRequest . Email ) ) ;
26+
27+ response . Status = ResponseStatus . Success ;
28+ response . AuthToken = token ;
29+ response . Message = "Token generated successfully" ;
30+ return this . Ok ( response ) ;
31+ }
32+ catch ( Exception ex )
33+ {
34+ response . Status = ResponseStatus . Error ;
35+ response . Message = ex . Message ;
36+ this . _logger . LogError ( ex . Message ) ;
37+ this . _logger . LogError ( ex . StackTrace ) ;
38+ return this . StatusCode ( StatusCodes . Status500InternalServerError , response ) ;
39+ }
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ using System . ComponentModel . DataAnnotations ;
2+
3+ namespace OsmoDoc . API . Models ;
4+
5+ public class LoginRequestDTO
6+ {
7+ [ Required ( ErrorMessage = "Email is required" ) ]
8+ [ EmailAddress ( ErrorMessage = "Invalid email format" ) ]
9+ public string Email { get ; set ; } = string . Empty ;
10+ }
You can’t perform that action at this time.
0 commit comments