Skip to content

Commit

Permalink
Adds a sessions controller
Browse files Browse the repository at this point in the history
  • Loading branch information
gstark committed Sep 20, 2021
1 parent b60850f commit fa9f88f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
70 changes: 70 additions & 0 deletions Controllers/SessionsController.cs
@@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using TacoTuesday.Models;
using TacoTuesday.Utils;

namespace TacoTuesday.Controllers
{
// All of these routes will be at the base URL: /api/Sessions
// That is what "api/[controller]" means below. It uses the name of the controller
// in this case RestaurantsController to determine the URL
[Route("api/[controller]")]
[ApiController]
public class SessionsController : ControllerBase
{
// This is the variable you use to have access to your database
private readonly DatabaseContext _context;

readonly protected string JWT_KEY;

// Constructor that receives a reference to your database context
// and stores it in _context_ for you to use in your API methods
public SessionsController(DatabaseContext context, IConfiguration config)
{
_context = context;
JWT_KEY = config["JWT_KEY"];
}

public class LoginUser
{
public string Email { get; set; }
public string Password { get; set; }
}

[HttpPost]
public async Task<ActionResult> Login(LoginUser loginUser)
{
var foundUser = await _context.Users.FirstOrDefaultAsync(user => user.Email == loginUser.Email);

if (foundUser != null && foundUser.IsValidPassword(loginUser.Password))
{
// create a custom response
var response = new
{
// This is the login token
token = new TokenGenerator(JWT_KEY).TokenFor(foundUser),

// The is the user details
user = foundUser
};

return Ok(response);
}
else
{
// Make a custom error response
var response = new
{
status = 400,
errors = new List<string>() { "User does not exist" }
};

// Return our error with the custom response
return BadRequest(response);
}
}
}
}
4 changes: 2 additions & 2 deletions TacoTuesday.csproj
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="5.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.3" />
Expand Down Expand Up @@ -51,7 +51,7 @@
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)build\**" />
<ResolvedFileToPublish Include="@(DistFiles-&gt;'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
Expand Down

0 comments on commit fa9f88f

Please sign in to comment.