aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
Diffstat (limited to 'backend')
-rw-r--r--backend/api/api/Controllers/AuthController.cs6
-rw-r--r--backend/api/api/Program.cs3
-rw-r--r--backend/api/api/Services/AuthService.cs41
-rw-r--r--backend/api/api/Services/IAuthService.cs10
4 files changed, 42 insertions, 18 deletions
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<IMongoClient>(s =>
new MongoClient(builder.Configuration.GetValue<string>("UserStoreDatabaseSettings:ConnectionString")));
builder.Services.AddScoped<IUserService, UserService>();
-
+builder.Services.AddScoped<IAuthService, AuthService>();
+
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<User> _users;
+ public AuthService(IConfiguration configuration, IUserStoreDatabaseSettings settings, IMongoClient mongoClient)
{
_configuration = configuration;
_jwt = new JwtToken(_configuration);
+ var database = mongoClient.GetDatabase(settings.DatabaseName);
+ _users = database.GetCollection<User>(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