source: resTools_backend/backend/Controllers/UsersController.cs@ 49b0bbd

Last change on this file since 49b0bbd was a26f6a1, checked in by Danilo <danilo.najkov@…>, 23 months ago

full auth flow

  • Property mode set to 100644
File size: 2.7 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 private readonly IRestaurantService _restaurantService = null;
16
17 public UsersController(IUserService userService, IRestaurantService restaurantService)
18 {
19 _userService = userService;
20 _restaurantService = restaurantService;
21 }
22
23 [HttpPost("login")]
24 public async Task<AuthenticateResponse> Authenticate(AuthenticateRequest model)
25 {
26 var response = await _userService.Authenticate(model);
27
28 if (response == null)
29 throw new Exception("Email or password is incorrect");
30
31 return response;
32 }
33
34 [HttpGet("authed")]
35 public async Task<AuthenticateResponse> IsAuthed()
36 {
37 int userId = 0;
38 try
39 {
40 userId = (int)this.HttpContext.Items["User"];
41 }catch (Exception ex){ return null; }
42 User user = await _userService.GetById(userId);
43 return new AuthenticateResponse() { Email=user.Email, Id = user.Id, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed};
44 }
45
46 [HttpPost("confirm")]
47 public async Task ConfirmEmail()
48 {
49 int userId = 0;
50 try
51 {
52 userId = (int)this.HttpContext.Items["User"];
53 }
54 catch (Exception ex) { return; }
55 User user = await _userService.GetById(userId);
56 await _userService.SendEmailConfirmation(user.Email);
57 }
58
59 [HttpPost("reset")]
60 public async Task ResetPassword(string email)
61 {
62 await _userService.SendPasswordReset(email);
63 }
64
65 [HttpPost("confirmed")]
66 public async Task ConfirmedEmail(string validityString)
67 {
68 int userId = 0;
69 try
70 {
71 userId = (int)this.HttpContext.Items["User"];
72 }
73 catch (Exception ex) { return; }
74 User user = await _userService.GetById(userId);
75 await _userService.ConfirmEmail(user, validityString);
76 }
77
78 [HttpPost("reseted")]
79 public async Task ResetedPassword(string validityString, string newPassword)
80 {
81 await _userService.ResetPassword(validityString, newPassword);
82 }
83
84 [HttpPost("register")]
85 public async Task<AuthenticateResponse> Register(CreateUserRequest req)
86 {
87 bool isFirst = await _restaurantService.GetRestaurant() == null;
88 var response = await _userService.Register(req, isFirst);
89 if (isFirst)
90 {
91 await _restaurantService.CreateRestaurant("", response.Id);
92 }
93 return response;
94 }
95}
Note: See TracBrowser for help on using the repository browser.