Skip to content

Instantly share code, notes, and snippets.

@Senhordim
Created January 12, 2020 20:27
Show Gist options
  • Select an option

  • Save Senhordim/747e5a58b03b9e4450529c63aa2ca362 to your computer and use it in GitHub Desktop.

Select an option

Save Senhordim/747e5a58b03b9e4450529c63aa2ca362 to your computer and use it in GitHub Desktop.

Revisions

  1. Senhordim created this gist Jan 12, 2020.
    64 changes: 64 additions & 0 deletions UsersController.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.AspNetCore.Authorization;
    using Shop.Data;
    using Shop.Models;
    using System;
    using Shop.Services;

    namespace Shop.Controllers
    {
    [Route("v1/users")]
    public class UsersController : Controller
    {
    [HttpPost]
    [Route("")]
    public async Task<ActionResult<User>> Post(
    [FromBody]User model,
    [FromServices]DataContext context
    )
    {
    if(!ModelState.IsValid)
    return BadRequest(ModelState);

    try
    {
    context.Users.Add(model);
    await context.SaveChangesAsync();
    return Ok(model);
    }
    catch
    {
    return BadRequest(new { message = "Não foi possível criar o Usuário" });
    }
    }

    [HttpPost]
    [Route("login")]
    [AllowAnonymous]
    public async Task<ActionResult<dynamic>> Authenticate(
    [FromServices] DataContext context,
    [FromBody]User model)
    {
    var user = await context.Users
    .AsNoTracking()
    .Where(x => x.Username == model.Username && x.Password == model.Password)
    .FirstOrDefaultAsync();

    if (user == null)
    return NotFound(new { message = "Usuário ou senha inválidos" });

    var token = TokenService.GenerateToken(user);
    // Esconde a senha
    user.Password = "";
    return new
    {
    user = user,
    token = token
    };
    }
    }
    }