source: iknow-api/Repository/Implementations/UserRepository.cs@ e49c3ed

Last change on this file since e49c3ed was e49c3ed, checked in by stefansaveski <stefansaveski19@…>, 11 months ago

Add JWT authentication and refactor namespaces

Introduced JWT-based authentication and authorization:

  • Configured JWT authentication in Program.cs with token validation.
  • Added [Authorize] attribute to a new getstring endpoint.

Refactored namespaces for consistency:

  • Updated namespaces from iknow_api.Core.* to iknow_api.*.

Enhanced middleware and dependency injection:

  • Registered IAuthService and IUserRepository.
  • Added app.UseAuthentication() and app.UseAuthorization().

Removed unused code and dependencies:

  • Deleted WeatherForecast class.
  • Cleaned up redundant imports.

Updated project dependencies to include JWT authentication library.

  • Property mode set to 100644
File size: 981 bytes
RevLine 
[e49c3ed]1using iknow_api.Models;
2using iknow_api.Data;
3using iknow_api.Models;
[5b42c8d]4using Microsoft.EntityFrameworkCore;
[9a57e89]5
[277bd72]6namespace iknow_api.Repositories
[9a57e89]7{
8 public class UserRepository : IUserRepository
9 {
10 private readonly AppDbContext _context;
11
12 public UserRepository(AppDbContext context)
13 {
14 _context = context;
15 }
16
[5b42c8d]17 public async Task<bool> UserExistsAsync(string username)
18 {
19 return await _context.User.AnyAsync(u => u.Email == username);
20 }
[9a57e89]21
[5b42c8d]22 public async Task<User?> GetUserByUsernameAsync(string username)
[9a57e89]23 {
[5b42c8d]24 return await _context.User.FirstOrDefaultAsync(u => u.Email == username);
[9a57e89]25 }
26
[5b42c8d]27 public async Task AddUserAsync(User user)
28 {
29 _context.User.Add(user);
30 await _context.SaveChangesAsync();
31 }
32
33 public async Task<int> GetUsersCountAsync()
34 {
35 return await _context.User.CountAsync();
36 }
[9a57e89]37 }
38}
Note: See TracBrowser for help on using the repository browser.