Skip to content

Commit 682a0bd

Browse files
committed
added AuthenticateResponse model and removed Token prop from User entity
1 parent d78c1e8 commit 682a0bd

File tree

5 files changed

+49
-21
lines changed

5 files changed

+49
-21
lines changed

Controllers/UsersController.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.AspNetCore.Authorization;
33
using WebApi.Services;
44
using WebApi.Models;
5-
using System.Linq;
65

76
namespace WebApi.Controllers
87
{
@@ -20,14 +19,14 @@ public UsersController(IUserService userService)
2019

2120
[AllowAnonymous]
2221
[HttpPost("authenticate")]
23-
public IActionResult Authenticate([FromBody]AuthenticateModel model)
22+
public IActionResult Authenticate([FromBody]AuthenticateRequest model)
2423
{
25-
var user = _userService.Authenticate(model.Username, model.Password);
24+
var response = _userService.Authenticate(model);
2625

27-
if (user == null)
26+
if (response == null)
2827
return BadRequest(new { message = "Username or password is incorrect" });
2928

30-
return Ok(user);
29+
return Ok(response);
3130
}
3231

3332
[HttpGet]

Entities/User.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@ public class User
1111

1212
[JsonIgnore]
1313
public string Password { get; set; }
14-
15-
public string Token { get; set; }
1614
}
1715
}

Models/AuthenticateModel.cs renamed to Models/AuthenticateRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace WebApi.Models
44
{
5-
public class AuthenticateModel
5+
public class AuthenticateRequest
66
{
77
[Required]
88
public string Username { get; set; }

Models/AuthenticateResponse.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using WebApi.Entities;
2+
3+
namespace WebApi.Models
4+
{
5+
public class AuthenticateResponse
6+
{
7+
public int Id { get; set; }
8+
public string FirstName { get; set; }
9+
public string LastName { get; set; }
10+
public string Username { get; set; }
11+
public string Token { get; set; }
12+
13+
14+
public AuthenticateResponse(User user, string token)
15+
{
16+
Id = user.Id;
17+
FirstName = user.FirstName;
18+
LastName = user.LastName;
19+
Username = user.Username;
20+
Token = token;
21+
}
22+
}
23+
}

Services/UserService.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
using Microsoft.IdentityModel.Tokens;
99
using WebApi.Entities;
1010
using WebApi.Helpers;
11+
using WebApi.Models;
1112

1213
namespace WebApi.Services
1314
{
1415
public interface IUserService
1516
{
16-
User Authenticate(string username, string password);
17+
AuthenticateResponse Authenticate(AuthenticateRequest model);
1718
IEnumerable<User> GetAll();
1819
}
1920

@@ -32,15 +33,29 @@ public UserService(IOptions<AppSettings> appSettings)
3233
_appSettings = appSettings.Value;
3334
}
3435

35-
public User Authenticate(string username, string password)
36+
public AuthenticateResponse Authenticate(AuthenticateRequest model)
3637
{
37-
var user = _users.SingleOrDefault(x => x.Username == username && x.Password == password);
38+
var user = _users.SingleOrDefault(x => x.Username == model.Username && x.Password == model.Password);
3839

3940
// return null if user not found
40-
if (user == null)
41-
return null;
41+
if (user == null) return null;
4242

4343
// authentication successful so generate jwt token
44+
var token = generateJwtToken(user);
45+
46+
return new AuthenticateResponse(user, token);
47+
}
48+
49+
public IEnumerable<User> GetAll()
50+
{
51+
return _users;
52+
}
53+
54+
// helper methods
55+
56+
private string generateJwtToken(User user)
57+
{
58+
// generate token that is valid for 7 days
4459
var tokenHandler = new JwtSecurityTokenHandler();
4560
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
4661
var tokenDescriptor = new SecurityTokenDescriptor
@@ -53,14 +68,7 @@ public User Authenticate(string username, string password)
5368
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
5469
};
5570
var token = tokenHandler.CreateToken(tokenDescriptor);
56-
user.Token = tokenHandler.WriteToken(token);
57-
58-
return user;
59-
}
60-
61-
public IEnumerable<User> GetAll()
62-
{
63-
return _users;
71+
return tokenHandler.WriteToken(token);
6472
}
6573
}
6674
}

0 commit comments

Comments
 (0)