diff options
author | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-11-08 15:02:03 +0000 |
---|---|---|
committer | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-11-08 15:02:03 +0000 |
commit | 9659c8868d19b31cf9ecae40bd28a30f13127f26 (patch) | |
tree | 428f090f1d20cc0b4a2865fdc2fdbf5f8af38fbc /Backend/Api/Api/Controllers/AuthController.cs | |
parent | f695db67989fd9c2c7b045bc55d8aa780b388d5d (diff) | |
parent | 6181721dc548421ec2926c17a73f0d48a2c99482 (diff) |
Merge branch 'develop' into 'master'
Merge develop -> master
See merge request BrzoDoLokacije2022/odyssey/brzodolokacije!3
Diffstat (limited to 'Backend/Api/Api/Controllers/AuthController.cs')
-rw-r--r-- | Backend/Api/Api/Controllers/AuthController.cs | 63 |
1 files changed, 55 insertions, 8 deletions
diff --git a/Backend/Api/Api/Controllers/AuthController.cs b/Backend/Api/Api/Controllers/AuthController.cs index d835d97..abb7adc 100644 --- a/Backend/Api/Api/Controllers/AuthController.cs +++ b/Backend/Api/Api/Controllers/AuthController.cs @@ -1,5 +1,6 @@ using Api.Interfaces; using Api.Models; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Api.Controllers @@ -8,12 +9,14 @@ namespace Api.Controllers public class AuthController : Controller { private readonly IUserService _userService; - public AuthController(IUserService userService) + private readonly IJwtService _jwtService; + public AuthController(IUserService userService,IJwtService jwtService) { _userService = userService; + _jwtService = jwtService; } - [HttpPost("register")] + [HttpPost("registerdeprecated")] public async Task<ActionResult<string>> Register([FromBody] Register creds) { //this is beyond scuffed and will be cleaned up later, when users,login and controllers are made @@ -34,6 +37,17 @@ namespace Api.Controllers return Ok(); } + [HttpPost("refreshJwt")] + [Authorize(Roles ="User")] + public async Task<ActionResult<string>> refreshJwt() + { + var jwt = await _userService.RenewToken(); + if (jwt != null) + { + return Ok(jwt); + } + return BadRequest("Pogresno uneti podaci"); + } [HttpPost("login")] public async Task<ActionResult<string>> Login([FromBody] Login creds) { @@ -47,19 +61,26 @@ namespace Api.Controllers } return BadRequest("Pogresno uneti podaci"); } - [HttpPost("registeractual")] + [HttpPost("register")] public async Task<ActionResult<string>> RegisterActual([FromBody] Register creds) { var msg = await _userService.Register(creds); - if (msg == "Email Exists") - return Forbid(msg); - if (msg == "Username Exists") - return Forbid(msg); - return Ok(msg); + switch (msg) + { + case "User Registered": + return Ok(msg); + default: + return BadRequest(msg); + } } [HttpPost("verify")] public async Task<ActionResult<string>> VerifyEmail([FromBody] VerifyUser creds) { + var vrfchk = new Login(); + vrfchk.email = creds.email; + vrfchk.password = creds.password; + if (await _userService.CheckVerification(vrfchk)) + return Ok("User already verified"); var uspeh = await _userService.VerifyUser(creds); if (!uspeh) return BadRequest("Kod netacan ili istekao"); @@ -73,5 +94,31 @@ namespace Api.Controllers return BadRequest("Kod netacan ili istekao"); return Ok("Sifra uspesno resetovana"); } + [HttpPost("forgotpass")] + public async Task<ActionResult<string>> ForgotPass([FromBody] JustMail justMail) + { + if (await _userService.ForgotPassword(justMail)) + return Ok("Email poslat"); + return BadRequest("Email nema registrovan nalog"); + } + [HttpGet("verifytoken/{token}")] + public async Task<ActionResult<string>> VerifyEmailToken(string token) + { + var username =_jwtService.EmailTokenToClaim(token,"username"); + string html; + if (username == null) + { + html = await System.IO.File.ReadAllTextAsync(@"./Assets/VerifyFailed.html"); + return base.Content(html, "text/html"); + } + else + { + html = await System.IO.File.ReadAllTextAsync(@"./Assets/VerifySuccess.html"); + html = html.Replace("{{name}}", username); + + await _userService.VerifyFromToken(token); + return base.Content(html, "text/html"); + } + } } } |