Skip to content

Commit e2d736e

Browse files
authored
Merge pull request #74 from IgniteUI/dgdimitrov/wrap-login-and-register-results
Wrap login and register results
2 parents dad8eb7 + 5481031 commit e2d736e

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

NorthwindCRUD/Controllers/AuthController.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,71 @@ public ActionResult<string> Register(RegisterDto userModel)
8989
return StatusCode(500);
9090
}
9191
}
92+
93+
[AllowAnonymous]
94+
[HttpPost("LoginObject")]
95+
public ActionResult<string> LoginObject(LoginDto userModel)
96+
{
97+
try
98+
{
99+
if (ModelState.IsValid)
100+
{
101+
if (this.authService.IsAuthenticated(userModel.Email, userModel.Password))
102+
{
103+
var token = this.authService.GenerateJwtToken(userModel.Email);
104+
105+
return Ok(new { token });
106+
}
107+
108+
return BadRequest("Email or password are not correct!");
109+
}
110+
111+
return BadRequest(ModelState);
112+
}
113+
catch (Exception error)
114+
{
115+
logger.LogError(error.Message);
116+
return StatusCode(500);
117+
}
118+
}
119+
120+
[AllowAnonymous]
121+
[HttpPost("RegisterObject")]
122+
public ActionResult<string> RegisterObject(RegisterDto userModel)
123+
{
124+
try
125+
{
126+
if (ModelState.IsValid)
127+
{
128+
if (userModel.Password != userModel.ConfirmedPassword)
129+
{
130+
return BadRequest("Passwords does not match!");
131+
}
132+
133+
if (this.authService.DoesUserExists(userModel.Email))
134+
{
135+
return BadRequest("User does not exists!");
136+
}
137+
138+
var mappedModel = this.mapper.Map<RegisterDto, UserDb>(userModel);
139+
var user = this.authService.RegisterUser(mappedModel);
140+
141+
if (user != null)
142+
{
143+
var token = this.authService.GenerateJwtToken(user.Email);
144+
return Ok(new { token });
145+
}
146+
147+
return BadRequest("Email or password are not correct!");
148+
}
149+
150+
return BadRequest(ModelState);
151+
}
152+
catch (Exception error)
153+
{
154+
logger.LogError(error.Message);
155+
return StatusCode(500);
156+
}
157+
}
92158
}
93159
}

0 commit comments

Comments
 (0)