Skip to content

Commit

Permalink
Merge pull request #84 from nixhantb/feature/nishantb/authentication
Browse files Browse the repository at this point in the history
Feature/nishantb/authentication
  • Loading branch information
nixhantb authored Jan 21, 2025
2 parents faf8687 + de4b9c9 commit f268e1c
Show file tree
Hide file tree
Showing 57 changed files with 4,887 additions and 1,182 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ artifacts/

#Ignore File holding secrects
jwtkey.key
Migrations

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using JobLeet.WebApi.JobLeet.Api.Models.Identity.Accounts;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace JobLeet.WebApi.JobLeet.Api.Controllers.Identity.Accounts
{
[Route("api/[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
private readonly ILogger<AccountController> _logger;

public AccountController(
UserManager<IdentityUser> userManager,
SignInManager<IdentityUser> signInManager,
ILogger<AccountController> logger
)
{
_userManager = userManager;
_signInManager = signInManager;
_logger = logger;
}

[HttpPost("register")]
public async Task<IActionResult> Register([FromBody] RegisterModel model)
{
if (ModelState.IsValid)
{
var user = new IdentityUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);

if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
// Send email confirmation link
return Ok(
new
{
message = "Registration successful, please check your email for confirmation.",
}
);
}

return BadRequest(result.Errors);
}

return BadRequest("Invalid data.");
}

[HttpPost("login")]
public async Task<IActionResult> Login([FromBody] LoginModel model)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(
model.Email,
model.Password,
model.RememberMe,
lockoutOnFailure: true
);
// @2FA
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
return Ok(new { message = "Login successful" });
}

if (result.IsLockedOut)
{
return BadRequest("Account is locked out.");
}

return BadRequest("Invalid login attempt.");
}

return BadRequest("Invalid login attempt.");
}

[HttpPost("logout")]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
_logger.LogInformation("User logged out.");
return Ok(new { message = "Logout successful" });
}
}
}

This file was deleted.

Loading

0 comments on commit f268e1c

Please sign in to comment.