Skip to content

Commit

Permalink
Stuck on Creating a user #4
Browse files Browse the repository at this point in the history
  • Loading branch information
jhachtel committed Apr 6, 2019
1 parent f8feda9 commit 6ca1b10
Show file tree
Hide file tree
Showing 9 changed files with 811 additions and 10 deletions.
59 changes: 59 additions & 0 deletions api/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Linq;
using System.Threading.Tasks;
using Fisher.Bookstore.Api.Data;
using Fisher.Bookstore.Api.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Fisher.Bookstore.Api.Controllers
{
[Produces("application/json")]
[Route("api/account")]
[ApiController]
public class AccountController : ControllerBase
{
private UserManager<ApplicationUser> userManager;
private SignInManager<ApplicationUser> signInManager;
private IConfiguration configuration;

public AccountController(UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IConfiguration configuration)
{
this.userManager = userManager;
this.signInManager = signInManager;
this.configuration = configuration;
}

[HttpPost("register")]
public async Task<IActionResult> Register([FromBody] ApplicationUser registration)
{
if (!ModelState.IsValid)
{
return BadRequest();
}

ApplicationUser user = new ApplicationUser
{
Email = registration.Email,
UserName = registration.Email,
Id = registration.Email
};

IdentityResult result = await userManager.CreateAsync(user, registration.Password);

if (!result.Succeeded)
{
foreach (var err in result.Errors)
{
ModelState.AddModelError(err.Code, err.Description);
}

return BadRequest(ModelState);
}
return Ok();
}
}

}
3 changes: 2 additions & 1 deletion api/Data/BookstoreContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Microsoft.EntityFrameworkCore;
using Fisher.Bookstore.Api.Models;
using Fisher.Bookstore.Api;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace Fisher.Bookstore.Api.Data

{
public class BookstoreContext : DbContext
public class BookstoreContext : IdentityDbContext<ApplicationUser>
{
public BookstoreContext(DbContextOptions<BookstoreContext> options)
: base(options)
Expand Down
1 change: 1 addition & 0 deletions api/Fisher.Bookstore.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.0" />
</ItemGroup>
Expand Down
271 changes: 271 additions & 0 deletions api/Migrations/20190406142841_identity.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6ca1b10

Please sign in to comment.