From 900bb5e845c3ad44defbd427cae3d44a4a43321f Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sat, 25 Feb 2023 13:15:44 +0100 Subject: feat: Initial commit --- .../Internal/Account/CreateInitialAccountRoute.cs | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 code/api/src/Endpoints/Internal/Account/CreateInitialAccountRoute.cs (limited to 'code/api/src/Endpoints/Internal/Account/CreateInitialAccountRoute.cs') diff --git a/code/api/src/Endpoints/Internal/Account/CreateInitialAccountRoute.cs b/code/api/src/Endpoints/Internal/Account/CreateInitialAccountRoute.cs new file mode 100644 index 0000000..e1d13dd --- /dev/null +++ b/code/api/src/Endpoints/Internal/Account/CreateInitialAccountRoute.cs @@ -0,0 +1,32 @@ +namespace IOL.GreatOffice.Api.Endpoints.Internal.Account; + +public class CreateInitialAccountRoute : RouteBaseAsync.WithoutRequest.WithActionResult +{ + private readonly MainAppDatabase _database; + private readonly UserService _userService; + + public CreateInitialAccountRoute(MainAppDatabase database, UserService userService) { + _database = database; + _userService = userService; + } + + /// + /// Create an initial user account. + /// + /// + /// + [AllowAnonymous] + [HttpGet("~/_/account/create-initial")] + public override async Task HandleAsync(CancellationToken cancellationToken = default) { + if (_database.Users.Any()) { + return NotFound(); + } + + var user = new User("admin@ivarlovlie.no"); + user.HashAndSetPassword("ivar123"); + _database.Users.Add(user); + await _database.SaveChangesAsync(cancellationToken); + await _userService.LogInUserAsync(HttpContext, user, cancellationToken: cancellationToken); + return Redirect("/"); + } +} \ No newline at end of file -- cgit v1.3