source: resTools_backend/backend/Controllers/UsersController.cs@ d76b7ee

Last change on this file since d76b7ee was d76b7ee, checked in by Danilo <danilo.najkov@…>, 3 years ago

prototype final

  • Property mode set to 100644
File size: 1.3 KB
Line 
1namespace backend.Controllers;
2
3using backend.DTOs;
4using backend.Entities;
5using backend.Helpers;
6using backend.Models;
7using backend.Services;
8using Microsoft.AspNetCore.Mvc;
9
10[ApiController]
11[Route("[controller]")]
12public class UsersController : ControllerBase
13{
14 private readonly IUserService _userService = null;
15
16 public UsersController(IUserService userService)
17 {
18 _userService = userService;
19 }
20
21 [HttpPost("login")]
22 public async Task<AuthenticateResponse> Authenticate(AuthenticateRequest model)
23 {
24 var response = await _userService.Authenticate(model);
25
26 if (response == null)
27 throw new Exception("Email or password is incorrect");
28
29 return response;
30 }
31
32 [HttpGet("authed")]
33 public async Task<AuthenticateResponse> IsAuthed()
34 {
35 int userId = 0;
36 try
37 {
38 userId = (int)this.HttpContext.Items["User"];
39 }catch (Exception ex){ return null; }
40 User user = await _userService.GetById(userId);
41 return new AuthenticateResponse() { Email=user.Email, Id = user.Id};
42 }
43
44 [HttpPost("register")]
45 public async Task<AuthenticateResponse> Register(CreateUserRequest req)
46 {
47 var response = await _userService.Register(req);
48 return response;
49 }
50}
Note: See TracBrowser for help on using the repository browser.