Skip to content

Commit ac5fa73

Browse files
Add "Username" field to user model
- Introduced `UserName` as a required property in the `User` domain model. - Updated database schema and migrations to include `UserName` column in the `users` table. - Modified registration and repository interfaces to handle `UserName` along with `Email`. - Updated API DTOs and service methods to accommodate the new field. - Enabled CORS for frontend development and improved JWT key handling in development.
1 parent e56c617 commit ac5fa73

File tree

16 files changed

+153
-14
lines changed

16 files changed

+153
-14
lines changed

Migrations/20251114220854_Username.Designer.cs

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace sparkly_server.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class Username : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.AddColumn<string>(
14+
name: "UserName",
15+
table: "users",
16+
type: "text",
17+
nullable: false,
18+
defaultValue: "");
19+
}
20+
21+
/// <inheritdoc />
22+
protected override void Down(MigrationBuilder migrationBuilder)
23+
{
24+
migrationBuilder.DropColumn(
25+
name: "UserName",
26+
table: "users");
27+
}
28+
}
29+
}

Migrations/AppDbContextModelSnapshot.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
4444
.IsRequired()
4545
.HasColumnType("text");
4646

47+
b.Property<string>("UserName")
48+
.IsRequired()
49+
.HasColumnType("text");
50+
4751
b.HasKey("Id");
4852

4953
b.HasIndex("Email")

Program.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,20 @@ public static void Main(string[] args)
1919

2020

2121
var jwtKey = builder.Configuration["SPARKLY_JWT_KEY"]
22-
?? Environment.GetEnvironmentVariable("SPARKLY_JWT_KEY")
23-
?? throw new Exception("JWT key missing");
22+
?? Environment.GetEnvironmentVariable("SPARKLY_JWT_KEY");
23+
24+
if (string.IsNullOrWhiteSpace(jwtKey))
25+
{
26+
if (builder.Environment.IsDevelopment())
27+
{
28+
jwtKey = "dev-only-jwt-key-change-me";
29+
}
30+
else
31+
{
32+
throw new Exception("JWT key missing");
33+
}
34+
}
35+
2436

2537
var jwtIssuer = builder.Configuration["SPARKLY_JWT_ISSUER"]
2638
?? Environment.GetEnvironmentVariable("SPARKLY_JWT_ISSUER")
@@ -57,6 +69,18 @@ public static void Main(string[] args)
5769
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
5870
builder.Services.AddOpenApi();
5971

72+
builder.Services.AddCors(options =>
73+
{
74+
options.AddPolicy("FrontendDev", policy =>
75+
{
76+
policy
77+
.WithOrigins("http://localhost:4200")
78+
.AllowAnyHeader()
79+
.AllowAnyMethod()
80+
.AllowCredentials();
81+
});
82+
});
83+
6084
builder.Services
6185
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
6286
.AddJwtBearer(options =>
@@ -86,6 +110,8 @@ public static void Main(string[] args)
86110

87111
app.UseAuthentication();
88112
app.UseAuthorization();
113+
114+
app.UseCors("FrontendDev");
89115

90116
app.MapControllers();
91117

src/Controllers/AuthController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public AuthController(IUserService userService, IAuthService authService)
2424
public async Task<IActionResult> Register([FromBody] RegisterRequest request, CancellationToken ct)
2525
{
2626
// Add validation email and password??
27-
await _userService.RegisterAsync(request.Email, request.Password, ct);
27+
await _userService.RegisterAsync(userName: request.Username, email: request.Email, password: request.Password, ct: ct);
2828
return NoContent();
2929
}
3030

src/Controllers/ProfileController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public IActionResult Me()
2929
if (!_currentUser.IsAuthenticated)
3030
return Unauthorized();
3131

32-
return Ok(new { _currentUser.UserId, _currentUser.Email, _currentUser.Role });
32+
return Ok(new { _currentUser.UserId, _currentUser.Email, _currentUser.UserName, _currentUser.Role });
3333
}
3434
}
3535
}

src/DTO/Auth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace sparkly_server.DTO
22
{
3-
public record RegisterRequest(string Email, string Password);
3+
public record RegisterRequest(string Username, string Email, string Password);
44
public record LoginRequest(string Email, string Password);
55
public record AuthResponse(string AccessToken, string RefreshToken);
66
}

src/Domain/Users/User.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ public class User
66
{
77
public Guid Id { get; private set; }
88
public string Email { get; private set; } = default!;
9+
public string UserName { get; set; } = default!;
910
public string PasswordHash { get; private set; } = default!;
1011
public string Role { get; private set; } = "User";
1112
public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
1213

1314
private User() { }
1415

15-
public User(string email, string role = "User")
16+
public User(string userName, string email, string role = "User")
1617
{
1718
Id = Guid.NewGuid();
1819
Email = email;
20+
UserName = userName;
1921
Role = role;
2022
}
2123

src/Infrastructure/AppDbContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2121
cfg.ToTable("users");
2222

2323
cfg.HasKey(u => u.Id);
24-
24+
2525
cfg.Property(u => u.Email)
2626
.IsRequired()
2727
.HasMaxLength(256);
2828

29+
cfg.Property(u => u.UserName)
30+
.IsRequired();
31+
2932
cfg.HasIndex(u => u.Email)
3033
.IsUnique();
3134

src/Services/Auth/AuthService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public AuthService(IUserService userService, IJwtProvider jwtProvider)
2424
var accessToken = _jwtProvider.GenerateAccessToken(user);
2525
var refreshToken = _jwtProvider.GenerateRefreshToken();
2626

27-
// TODO: save refresh token to db
27+
// TODO: save refresh token to db (!BLOCKER!)
2828

2929
var now = DateTime.UtcNow;
3030

0 commit comments

Comments
 (0)