aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers/UserController.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-04-13 15:47:40 +0800
committerGitHub <noreply@github.com>2019-04-13 15:47:40 +0800
commit825778f4058d107186be66af05b1f8d16cd1e32c (patch)
tree1aa37565aad734b604eb94ed3a62db2308f4b30d /Timeline/Controllers/UserController.cs
parent8c5e7069d2651fb6fae641dfe482d7a0910b3fd1 (diff)
parentc2b9b32ada535bb09ee06ab0dfc0a3405e12485a (diff)
downloadtimeline-825778f4058d107186be66af05b1f8d16cd1e32c.tar.gz
timeline-825778f4058d107186be66af05b1f8d16cd1e32c.tar.bz2
timeline-825778f4058d107186be66af05b1f8d16cd1e32c.zip
Merge pull request #20 from crupest/separate
Separate front end and back end.
Diffstat (limited to 'Timeline/Controllers/UserController.cs')
-rw-r--r--Timeline/Controllers/UserController.cs54
1 files changed, 42 insertions, 12 deletions
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index eb1b8513..3b4e7b4f 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -1,12 +1,14 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
+using System;
+using System.Threading.Tasks;
using Timeline.Entities;
using Timeline.Services;
namespace Timeline.Controllers
{
- [Route("api/[controller]")]
+ [Route("[controller]")]
public class UserController : Controller
{
private static class LoggingEventIds
@@ -16,23 +18,22 @@ namespace Timeline.Controllers
}
private readonly IUserService _userService;
- private readonly IJwtService _jwtService;
private readonly ILogger<UserController> _logger;
- public UserController(IUserService userService, IJwtService jwtService, ILogger<UserController> logger)
+ public UserController(IUserService userService, ILogger<UserController> logger)
{
_userService = userService;
- _jwtService = jwtService;
_logger = logger;
}
[HttpPost("[action]")]
[AllowAnonymous]
- public ActionResult<CreateTokenResponse> CreateToken([FromBody] CreateTokenRequest request)
+ public async Task<ActionResult<CreateTokenResponse>> CreateToken([FromBody] CreateTokenRequest request)
{
- var user = _userService.Authenticate(request.Username, request.Password);
+ var result = await _userService.CreateToken(request.Username, request.Password);
- if (user == null) {
+ if (result == null)
+ {
_logger.LogInformation(LoggingEventIds.LogInFailed, "Attemp to login with username: {} and password: {} failed.", request.Username, request.Password);
return Ok(new CreateTokenResponse
{
@@ -45,17 +46,46 @@ namespace Timeline.Controllers
return Ok(new CreateTokenResponse
{
Success = true,
- Token = _jwtService.GenerateJwtToken(user),
- UserInfo = user.GetUserInfo()
+ Token = result.Token,
+ UserInfo = result.UserInfo
});
}
[HttpPost("[action]")]
[AllowAnonymous]
- public ActionResult<TokenValidationResponse> ValidateToken([FromBody] TokenValidationRequest request)
+ public async Task<ActionResult<TokenValidationResponse>> ValidateToken([FromBody] TokenValidationRequest request)
{
- var result = _jwtService.ValidateJwtToken(request.Token);
- return Ok(result);
+ var result = await _userService.VerifyToken(request.Token);
+
+ if (result == null)
+ {
+ return Ok(new TokenValidationResponse
+ {
+ IsValid = false,
+ });
+ }
+
+ return Ok(new TokenValidationResponse
+ {
+ IsValid = true,
+ UserInfo = result
+ });
+ }
+
+ [HttpPost("[action]")]
+ [Authorize(Roles = "admin")]
+ public async Task<ActionResult<CreateUserResponse>> CreateUser([FromBody] CreateUserRequest request)
+ {
+ var result = await _userService.CreateUser(request.Username, request.Password, request.Roles);
+ switch (result)
+ {
+ case CreateUserResult.Success:
+ return Ok(new CreateUserResponse { ReturnCode = CreateUserResponse.SuccessCode });
+ case CreateUserResult.AlreadyExists:
+ return Ok(new CreateUserResponse { ReturnCode = CreateUserResponse.AlreadyExistsCode });
+ default:
+ throw new Exception("Unreachable code.");
+ }
}
}
}