Repository navigation
Expand file tree
/
Copy pathAuthorController.cs
More file actions
105 lines (91 loc) · 2.63 KB
/
Copy pathAuthorController.cs
File metadata and controls
105 lines (91 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Blogifier.Core.Providers;
using Blogifier.Shared;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Blogifier.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AuthorController : ControllerBase
{
private readonly IAuthorProvider _authorProvider;
public AuthorController(IAuthorProvider authorProvider)
{
_authorProvider = authorProvider;
}
[Authorize]
[HttpGet("all")]
public async Task<List<Author>> All()
{
return await _authorProvider.GetAuthors();
}
[Authorize]
[HttpGet("email/{email}")]
public async Task<ActionResult<Author>> FindByEmail(string email)
{
return await _authorProvider.FindByEmail(email);
}
[HttpGet("getcurrent")]
public async Task<ActionResult<Author>> GetCurrentAuthor()
{
if (User.Identity.IsAuthenticated)
return await FindByEmail(User.FindFirstValue(ClaimTypes.Name));
return new Author();
}
[Authorize]
[HttpDelete("{id:int}")]
public async Task<ActionResult<bool>> RemoveAuthor(int id)
{
return await _authorProvider.Remove(id);
}
[Authorize]
[HttpPost("add")]
public async Task<ActionResult<bool>> Add(Author author)
{
var success = await _authorProvider.Add(author);
return success ? Ok() : BadRequest();
}
[Authorize]
[HttpPut("update")]
public async Task<ActionResult<bool>> Update(Author author)
{
var success = await _authorProvider.Update(author);
return success ? Ok() : BadRequest();
}
[HttpPost("register")]
public async Task<ActionResult<bool>> Register(RegisterModel model)
{
var success = await _authorProvider.Register(model);
return success ? Ok() : BadRequest();
}
[HttpPost("login")]
public async Task<ActionResult> Login(LoginModel model)
{
if (await _authorProvider.Verify(model) == false)
return BadRequest();
var claim = new Claim(ClaimTypes.Name, model.Email);
var claimsIdentity = new ClaimsIdentity(new[] { claim }, "serverAuth");
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await HttpContext.SignInAsync(claimsPrincipal);
return Ok();
}
[HttpGet("logout")]
public async Task<ActionResult<bool>> LogOutUser()
{
await HttpContext.SignOutAsync();
return await Task.FromResult(true);
}
[Authorize]
[HttpPut("changepassword")]
public async Task<ActionResult<bool>> ChangePassword(RegisterModel model)
{
var success = await _authorProvider.ChangePassword(model);
return success ? Ok() : BadRequest();
}
}
}