From 52dec403d19a732591f9ce27ec802e3f8c480a9a Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Tue, 8 Mar 2022 18:28:18 +0100 Subject: Dodat dependency injection. Auth Servis povezan sa bazom --- backend/api/api/Controllers/AuthController.cs | 6 ++-- backend/api/api/Program.cs | 3 +- backend/api/api/Services/AuthService.cs | 41 ++++++++++++++++++--------- backend/api/api/Services/IAuthService.cs | 10 +++++++ 4 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 backend/api/api/Services/IAuthService.cs (limited to 'backend') diff --git a/backend/api/api/Controllers/AuthController.cs b/backend/api/api/Controllers/AuthController.cs index 1f47067f..100ab3ca 100644 --- a/backend/api/api/Controllers/AuthController.cs +++ b/backend/api/api/Controllers/AuthController.cs @@ -9,10 +9,10 @@ namespace api.Controllers [ApiController] public class AuthController : ControllerBase { - private AuthService _auth; - public AuthController(IConfiguration configuration) + private IAuthService _auth; + public AuthController(IAuthService auth) { - _auth=new AuthService(configuration); + _auth = auth; } [HttpPost("register")] diff --git a/backend/api/api/Program.cs b/backend/api/api/Program.cs index c54b1b3e..4c2d1b9f 100644 --- a/backend/api/api/Program.cs +++ b/backend/api/api/Program.cs @@ -22,7 +22,8 @@ builder.Services.AddSingleton(s => new MongoClient(builder.Configuration.GetValue("UserStoreDatabaseSettings:ConnectionString"))); builder.Services.AddScoped(); - +builder.Services.AddScoped(); + builder.Services.AddControllers(); diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs index 1a901cb8..015fdac7 100644 --- a/backend/api/api/Services/AuthService.cs +++ b/backend/api/api/Services/AuthService.cs @@ -1,34 +1,47 @@ -using api.Models; +using api.Interfaces; +using api.Models; using api.Models.Users; +using MongoDB.Driver; namespace api.Services { - public class AuthService + public class AuthService : IAuthService { private JwtToken _jwt; private readonly IConfiguration _configuration; - public AuthService(IConfiguration configuration) + private readonly IMongoCollection _users; + public AuthService(IConfiguration configuration, IUserStoreDatabaseSettings settings, IMongoClient mongoClient) { _configuration = configuration; _jwt = new JwtToken(_configuration); + var database = mongoClient.GetDatabase(settings.DatabaseName); + _users = database.GetCollection(settings.CollectionName); } public string Login(AuthRequest user) { - //Check username in DB - - //Verify password - - //gen token - + User u = _users.Find(x => x.Username == user.UserName).FirstOrDefault(); + if (u == null) + return "Username doesn't exist"; + if (!PasswordCrypt.checkPassword(user.Password, u.Password)) + return "Wrong password"; return _jwt.GenToken(user); } - public RegisterRequest Register(RegisterRequest user) + public string Register(RegisterRequest user) { - //check for existing email and username - user.password = PasswordCrypt.hashPassword(user.password); - //Add to DB. TO DO - return user; + User u = new User(); + u.Username = user.username; + u.Email = user.email; + u.Password = PasswordCrypt.hashPassword(user.password); + u.FirstName = user.firstName; + u.LastName = user.lastName; + if (_users.Find(user => user.Username == u.Username).FirstOrDefault() != null) + return "Username Already Exists"; + if (_users.Find(user => user.Email == u.Email).FirstOrDefault() != null) + return "Email Already Exists"; + + _users.InsertOne(u); + return "User added"; } diff --git a/backend/api/api/Services/IAuthService.cs b/backend/api/api/Services/IAuthService.cs new file mode 100644 index 00000000..79085f8c --- /dev/null +++ b/backend/api/api/Services/IAuthService.cs @@ -0,0 +1,10 @@ +using api.Models.Users; + +namespace api.Services +{ + public interface IAuthService + { + string Login(AuthRequest user); + string Register(RegisterRequest user); + } +} \ No newline at end of file -- cgit v1.2.3