diff --git a/README.md b/README.md
index b41cf3c..8a7cb2b 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,8 @@
This is an API for allowing users with health issues to safely enjoy a theme park.
It provides freedom to people with health issues to enjoy the park while ensuring their safety and well-being.
+Friendly reminder: clone the repository recursively!
+
# Deployment
Environment variables:
diff --git a/endpoints.http b/endpoints.http
new file mode 100644
index 0000000..cf0bf60
--- /dev/null
+++ b/endpoints.http
@@ -0,0 +1,26 @@
+@baseUri=http://localhost:3000
+@username={{$randomInt}}
+@email={{$randomInt}}@google.com
+
+POST {{baseUri}}/auth/register HTTP/1.1
+Content-Type: application/json
+
+{"username":"{{username}}","password":"password","email":"{{email}}","type":"monitor"}
+###
+
+POST {{baseUri}}/auth/login HTTP/1.1
+Content-Type: application/json
+
+{"username":"{{username}}","password":"password"}
+###
+
+POST {{baseUri}}/auth/logout HTTP/1.1
+Content-Type: application/json
+
+{"username":"{{username}}","password":"password"}
+###
+
+DELETE {{baseUri}}/auth/delete
+Content-Type: application/json
+
+{"username":"{{username}}","password":"password"}
diff --git a/plan.md b/plan.md
index 39ccb9b..d9e3637 100644
--- a/plan.md
+++ b/plan.md
@@ -16,7 +16,7 @@
- [ ] User management (user/monitor)
- [x] Registration
- [ ] Validation based on type
- - [ ] Login
+ - [x] Login
- [ ] Password reset
- [ ] User profile management
- [ ] Device management
diff --git a/src/api/middlewares/authMiddleware.js b/src/api/middlewares/authMiddleware.js
index a1ba498..1187112 100644
--- a/src/api/middlewares/authMiddleware.js
+++ b/src/api/middlewares/authMiddleware.js
@@ -16,6 +16,7 @@ export function validateAuth(options) {
const user = (req.user = await DbUser.findById(auth.id).exec());
if (options.roles && !options.roles.includes(user.type)) {
+ res.status(401).send('Unauthorized');
return;
}
diff --git a/src/api/routes/auth/accountRoutes.js b/src/api/routes/auth/accountRoutes.js
index 5c88c22..18c204d 100644
--- a/src/api/routes/auth/accountRoutes.js
+++ b/src/api/routes/auth/accountRoutes.js
@@ -5,13 +5,22 @@ export const registerRoute = {
route: '/auth/register',
async onPost(req, res) {
const data = await RegisterDto.create(req.body);
- const registerResult = await registerUser(data);
- res.send(registerResult);
+ await registerUser(data);
+ res.status(204).send();
}
};
export const loginRoute = {
route: '/auth/login',
+ async onPost(req, res) {
+ const data = await AuthDto.create(req.body);
+ const loginResult = await loginUser(data, req.headers['user-agent']);
+ res.send(loginResult);
+ }
+};
+
+export const logoutRoute = {
+ route: '/auth/logout',
/**
*
* @param req {Request}
@@ -20,8 +29,8 @@ export const loginRoute = {
*/
async onPost(req, res) {
const data = await AuthDto.create(req.body);
- const loginResult = await loginUser(data, req.headers['user-agent']);
- res.send(loginResult);
+ // const loginResult = await deleteDevice(data, );
+ res.status(204).send();
}
};
diff --git a/src/db/dbAccess/user.js b/src/db/dbAccess/user.js
index 7357b59..fad5ba3 100644
--- a/src/db/dbAccess/user.js
+++ b/src/db/dbAccess/user.js
@@ -84,10 +84,11 @@ export async function loginUser(data, deviceName) {
const whoAmI = await WhoAmIDto.create({
userId: user._id,
username: user.username,
- deviceId: device._id
+ deviceId: device._id,
+ type: user.type
});
- whoAmI.access_token = await generateJwtToken({
+ whoAmI.accessToken = await generateJwtToken({
type: user.type,
sub: user._id.toString(),
deviceId: device._id.toString(),
diff --git a/src/dto/auth/AuthDto.js b/src/dto/auth/AuthDto.js
index 14e09ae..22e2620 100644
--- a/src/dto/auth/AuthDto.js
+++ b/src/dto/auth/AuthDto.js
@@ -6,9 +6,9 @@ import Joi from 'joi';
*/
export class AuthDto {
static schema = new Joi.object({
- username: Joi.string().required(),
- email: Joi.string().email().required(),
- password: Joi.string().required()
+ password: Joi.string().required(),
+ username: Joi.string(),
+ email: Joi.string().email()
}).or('username', 'email');
username;
diff --git a/src/dto/auth/WhoAmIDto.js b/src/dto/auth/WhoAmIDto.js
index ae1795a..686194c 100644
--- a/src/dto/auth/WhoAmIDto.js
+++ b/src/dto/auth/WhoAmIDto.js
@@ -8,6 +8,7 @@ export class WhoAmIDto {
userId;
username;
deviceId;
+ type;
/**
* @param data {WhoAmIDto}
diff --git a/testFrontend/SafeNSound.Frontend/Pages/Auth.razor b/testFrontend/SafeNSound.Frontend/Pages/Auth.razor
index 6c28bf1..3db77a1 100644
--- a/testFrontend/SafeNSound.Frontend/Pages/Auth.razor
+++ b/testFrontend/SafeNSound.Frontend/Pages/Auth.razor
@@ -1,6 +1,5 @@
@page "/Auth"
-
<h1>Auth</h1>
<span>Username (L?, R): </span>
@@ -8,7 +7,7 @@
<span>Email (L? R): </span>
<FancyTextBox @bind-Value="@Email"/><br/>
<span>Password (L, R): </span>
-<FancyTextBox @bind-Value="@Password"/><br/>
+<FancyTextBox @bind-Value="@Password" IsPassword="true" /><br/>
<span>Type (R): </span>
<FancyTextBox @bind-Value="@UserType"/><span> (one of user|monitor|admin)</span><br/>
<LinkButton OnClick="@Randomise">Randomise</LinkButton>
@@ -46,7 +45,7 @@
private async Task Randomise() {
Username = Guid.NewGuid().ToString();
- Email = Guid.NewGuid().ToString() + "@example.com";
+ Email = Guid.NewGuid() + "@example.com";
Password = Guid.NewGuid().ToString();
UserType = Random.Shared.GetItems(["user", "monitor", "admin"], 1)[0];
StateHasChanged();
@@ -56,7 +55,7 @@
Result = null;
Exception = null;
try {
- Result = await Authentication.Register(new() {
+ await Authentication.Register(new() {
Username = Username,
Password = Password,
Email = Email,
@@ -91,7 +90,7 @@
Result = null;
Exception = null;
try {
- Result = await Authentication.Delete(new() {
+ await Authentication.Delete(new() {
Username = Username,
Password = Password,
Email = Email
diff --git a/testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs b/testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs
index cbff880..f63d8b0 100644
--- a/testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs
+++ b/testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs
@@ -1,15 +1,16 @@
-using System.Text.Json.Serialization;
+using System.Net.Http.Json;
+using System.Text.Json.Serialization;
namespace SafeNSound.Sdk;
public class SafeNSoundAuthentication(SafeNSoundConfiguration config) {
- public async Task<SafeNSoundAuthResult> Register(RegisterDto registerDto) {
+ public async Task Register(RegisterDto registerDto) {
var hc = new WrappedHttpClient() {
BaseAddress = new Uri(config.BaseUri)
};
var res = await hc.PostAsJsonAsync("/auth/register", registerDto);
- return null!;
+ res.EnsureSuccessStatusCode();
}
public async Task<SafeNSoundAuthResult> Login(AuthDto authDto) {
@@ -18,17 +19,16 @@ public class SafeNSoundAuthentication(SafeNSoundConfiguration config) {
};
var res = await hc.PostAsJsonAsync("/auth/login", authDto);
- return null!;
+ return (await res.Content.ReadFromJsonAsync<SafeNSoundAuthResult>())!;
}
- public async Task<SafeNSoundAuthResult> Delete(AuthDto authDto) {
+ public async Task Delete(AuthDto authDto) {
var hc = new WrappedHttpClient() {
BaseAddress = new Uri(config.BaseUri)
};
var res = await hc.DeleteAsJsonAsync("/auth/delete", authDto);
res.EnsureSuccessStatusCode();
- return null!;
}
}
@@ -57,4 +57,21 @@ public class AuthDto {
public string Email { get; set; } = string.Empty;
}
-public class SafeNSoundAuthResult { }
\ No newline at end of file
+public class WhoAmI {
+ [JsonPropertyName("userId")]
+ public required string UserId { get; set; }
+
+ [JsonPropertyName("username")]
+ public required string UserName { get; set; }
+
+ [JsonPropertyName("deviceId")]
+ public required string DeviceId { get; set; }
+
+ [JsonPropertyName("type")]
+ public required string UserType { get; set; }
+}
+
+public class SafeNSoundAuthResult : WhoAmI {
+ [JsonPropertyName("accessToken")]
+ public required string AccessToken { get; set; }
+}
\ No newline at end of file
diff --git a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
index 7a7023c..dee3913 100644
--- a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
+++ b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
@@ -1,6 +1,6 @@
namespace SafeNSound.Sdk;
-public class SafeNSoundClient
+public class SafeNSoundClient(SafeNSoundConfiguration config)
{
}
\ No newline at end of file
|