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

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

full auth flow

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[b66b3ac]1namespace backend.Controllers;
2
[057037b]3using backend.DTOs;
[d76b7ee]4using backend.Entities;
[b66b3ac]5using backend.Helpers;
6using backend.Models;
7using backend.Services;
8using Microsoft.AspNetCore.Mvc;
9
10[ApiController]
11[Route("[controller]")]
12public class UsersController : ControllerBase
13{
[057037b]14 private readonly IUserService _userService = null;
[899b19d]15 private readonly IRestaurantService _restaurantService = null;
[b66b3ac]16
[899b19d]17 public UsersController(IUserService userService, IRestaurantService restaurantService)
[b66b3ac]18 {
19 _userService = userService;
[899b19d]20 _restaurantService = restaurantService;
[b66b3ac]21 }
22
[057037b]23 [HttpPost("login")]
24 public async Task<AuthenticateResponse> Authenticate(AuthenticateRequest model)
[b66b3ac]25 {
[057037b]26 var response = await _userService.Authenticate(model);
[b66b3ac]27
28 if (response == null)
[057037b]29 throw new Exception("Email or password is incorrect");
[b66b3ac]30
[057037b]31 return response;
[b66b3ac]32 }
33
[d76b7ee]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);
[a26f6a1]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);
[d76b7ee]82 }
83
[057037b]84 [HttpPost("register")]
85 public async Task<AuthenticateResponse> Register(CreateUserRequest req)
[b66b3ac]86 {
[899b19d]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 }
[057037b]93 return response;
[b66b3ac]94 }
95}
Note: See TracBrowser for help on using the repository browser.