Login and Register API added, further info shared between Owners
This commit is contained in:
84
api/main.ts
84
api/main.ts
@@ -32,8 +32,8 @@ router
|
||||
|
||||
// Account routes
|
||||
router
|
||||
.post("/api/account/login", () => {}) // TODO
|
||||
.post("/api/account/register", () => {}) // TODO
|
||||
.post("/api/account/login", api_login) // TODO
|
||||
.post("/api/account/register", api_register) // TODO
|
||||
.post("/api/account/logout", () => {}) // TODO
|
||||
.post("/api/account/password/forgot", () => {}) // TODO
|
||||
.post("/api/account/password/reset", () => {}) // TODO
|
||||
@@ -52,7 +52,7 @@ router
|
||||
// User routes
|
||||
router
|
||||
.get("/api/users", api_getAllUsers)
|
||||
.get("/api/user/:id/info", api_user_getInfo);
|
||||
.get("/api/user/:id/info", api_user_getInfo); // @error GEHT NICHT
|
||||
|
||||
// Post routes
|
||||
router
|
||||
@@ -69,6 +69,7 @@ router
|
||||
* Currently not implemented
|
||||
* Middleware
|
||||
*/
|
||||
|
||||
async function authenticator(ctx: Context, next: Next): Promise<void> {
|
||||
const authHeader = ctx.request.headers.get('Authorization');
|
||||
|
||||
@@ -111,8 +112,7 @@ async function tokenChecker(ctx: Context, next: Next): Promise<void> {
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
async function api_getAllUsers(ctx: any): Promise<void> {
|
||||
async function api_getAllUsers(ctx: Context): Promise<void> {
|
||||
const getUsers = await db_utils.getAllUsersFromDB();
|
||||
ctx.response.body = getUsers;
|
||||
}
|
||||
@@ -129,33 +129,89 @@ async function api_user_getInfo(ctx: any): Promise<void> {
|
||||
|
||||
try {
|
||||
const user = await db_utils.getAllUserInfoByID(id);
|
||||
|
||||
if (!user) {
|
||||
ctx.response.status = 404; // Not Found status/Doesn't exist
|
||||
ctx.response.body = { error: "User not found" };
|
||||
if (user === null || user === undefined) {
|
||||
helper_utils.errorResponse(ctx, 404, "User not found");
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.response.body = user;
|
||||
} catch (error) {
|
||||
ctx.response.status = 500; // Internal Server Error status
|
||||
ctx.response.body = { error: "Error" };
|
||||
helper_utils.errorResponse(ctx, 500, "Internal Server Error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Posts
|
||||
async function api_posts_getAll(ctx: any): Promise<void> {
|
||||
async function api_posts_getAll(ctx: Context): Promise<void> {
|
||||
const posts = await db_utils.getPostsFromDB();
|
||||
ctx.response.body = posts;
|
||||
}
|
||||
|
||||
// Comments
|
||||
|
||||
// login/register
|
||||
async function api_register(ctx: Context): Promise<void> {
|
||||
try {
|
||||
const body = ctx.request.body;
|
||||
const result = await body.json();
|
||||
const { username, password, userGroup, displayname, user_email, firstname, surname} = result;
|
||||
const account_created = `${Math.floor(Date.now() / 1000)}${new Date().toLocaleDateString('en-GB').split('/').join('.')}`;
|
||||
|
||||
|
||||
if ( !username || !password || !userGroup || !displayname || !user_email || !firstname || !surname) {
|
||||
helper_utils.errorResponse(ctx, 400, "Missing required fields");
|
||||
return;
|
||||
}
|
||||
|
||||
const hash = await helper_utils.hashPassword(password);
|
||||
const userId = await db_utils.registerUser(username, hash, userGroup, displayname, user_email, firstname, surname, account_created);
|
||||
helper_utils.sendResponse(ctx, { status: 200, body: `Registered under name: ${userId}` });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
helper_utils.errorResponse(ctx, 500, "Invalid request");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async function api_login(ctx: Context): Promise<string> {
|
||||
try {
|
||||
const body = ctx.request.body;
|
||||
const result = await body.json();
|
||||
const { username, password } = result;
|
||||
|
||||
if (!username || !password) {
|
||||
helper_utils.errorResponse(ctx, 400, "Missing required fields");
|
||||
return "Error";
|
||||
}
|
||||
|
||||
const user = await db_utils.getUserByUsername(username);
|
||||
if (!user) {
|
||||
helper_utils.errorResponse(ctx, 404, "User not found");
|
||||
return "Error";
|
||||
}
|
||||
|
||||
const hash = await helper_utils.hashPassword(password);
|
||||
if (user.password !== hash) {
|
||||
helper_utils.errorResponse(ctx, 401, "Invalid password");
|
||||
return "Error";
|
||||
}
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
helper_utils.errorResponse(ctx, 500, "Invalid request");
|
||||
return "Error";
|
||||
}
|
||||
|
||||
return "Success";
|
||||
}
|
||||
|
||||
// Filtering
|
||||
|
||||
// +++ APP ---------------------------------------------------------- //
|
||||
app.use(oakCors());
|
||||
app.use(oakCors({
|
||||
origin: '*',
|
||||
credentials: true,
|
||||
}));
|
||||
app.use(router.routes());
|
||||
app.use(router.allowedMethods());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user