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 | |
---|
1 | namespace backend.Controllers;
|
---|
2 |
|
---|
3 | using backend.DTOs;
|
---|
4 | using backend.Entities;
|
---|
5 | using backend.Helpers;
|
---|
6 | using backend.Models;
|
---|
7 | using backend.Services;
|
---|
8 | using Microsoft.AspNetCore.Mvc;
|
---|
9 |
|
---|
10 | [ApiController]
|
---|
11 | [Route("[controller]")]
|
---|
12 | public 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.