Skip to content

Commit 7cfb16d

Browse files
committed
Updated access token payloads
- Removed user details in login response
1 parent d4f58fc commit 7cfb16d

File tree

2 files changed

+10
-31
lines changed

2 files changed

+10
-31
lines changed

src/Api/Features/Auth/GenerateAccessToken.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ public class Command : IRequest<LoginResponse>
1818
public Command(
1919
ClaimsIdentity claimsIdentity,
2020
EmployeeViewModel employee,
21-
string uername)
21+
string userName)
2222
{
2323
ClaimsIdentity = claimsIdentity;
2424
Employee = employee;
25-
Uername = uername;
25+
UserName = userName;
2626
}
2727

2828
public ClaimsIdentity ClaimsIdentity { get; }
2929
public EmployeeViewModel Employee { get; }
30-
public string Uername { get; }
30+
public string UserName { get; }
3131
}
3232

3333
public class CommandHandler : IRequestHandler<Command, LoginResponse>
@@ -46,16 +46,7 @@ public async Task<LoginResponse> Handle(Command request, CancellationToken cance
4646
{
4747
return new LoginResponse
4848
{
49-
User = new UserDetails
50-
{
51-
EmployeeId = (request.Employee.Id != Guid.Empty) ? request.Employee.Id: Guid.Empty,
52-
FullName = request.Employee.FullName,
53-
UserName = request.Uername,
54-
Roles = request.ClaimsIdentity.Claims.Where(c => c.Type == ClaimTypes.Role)
55-
.Select(c => c.Value)
56-
.ToList()
57-
},
58-
AccessToken = await GenerateEncodedToken(request.Uername, request.ClaimsIdentity),
49+
AccessToken = await GenerateEncodedToken(request, request.ClaimsIdentity),
5950
ExpiresIn = (int) _jwtOptions.ValidFor.TotalSeconds
6051
};
6152
}
@@ -66,11 +57,13 @@ public async Task<LoginResponse> Handle(Command request, CancellationToken cance
6657
}
6758
}
6859

69-
public async Task<string> GenerateEncodedToken(string userName, ClaimsIdentity identity)
60+
public async Task<string> GenerateEncodedToken(Command request, ClaimsIdentity identity)
7061
{
7162
var claims = new[]
7263
{
73-
new Claim(JwtRegisteredClaimNames.Sub, userName),
64+
new Claim(JwtRegisteredClaimNames.Sub, request.UserName),
65+
new Claim("full_name", request.Employee.FullName),
66+
new Claim("employee_id", request.Employee.Id.ToString()),
7467
new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()),
7568
new Claim(JwtRegisteredClaimNames.Iat, Extensions.ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
7669
identity.FindFirst(ClaimTypes.Role)
@@ -83,7 +76,8 @@ public async Task<string> GenerateEncodedToken(string userName, ClaimsIdentity i
8376
claims: claims,
8477
notBefore: _jwtOptions.NotBefore,
8578
expires: _jwtOptions.Expiration,
86-
signingCredentials: _jwtOptions.SigningCredentials);
79+
signingCredentials: _jwtOptions.SigningCredentials
80+
);
8781

8882
var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
8983

src/Api/Features/Auth/LoginResponse.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,10 @@ namespace WebApi.Features.Auth
66
{
77
public class LoginResponse
88
{
9-
[JsonProperty("user")]
10-
public UserDetails User { get; set; }
11-
129
[JsonProperty("access_token")]
1310
public string AccessToken { get; set; }
1411

1512
[JsonProperty("expires_in")]
1613
public int ExpiresIn { get; set; }
1714
}
18-
19-
public class UserDetails
20-
{
21-
[JsonProperty("empId")]
22-
public Guid EmployeeId { get; set; }
23-
[JsonProperty("fullName")]
24-
public string FullName { get; set; }
25-
[JsonProperty("username")]
26-
public string UserName { get; set; }
27-
[JsonProperty("roles")]
28-
public List<string> Roles { get; set; }
29-
}
3015
}

0 commit comments

Comments
 (0)