Skip to content
Snippets Groups Projects
Commit 23e41916 authored by Bernard Roland (Student Com20)'s avatar Bernard Roland (Student Com20)
Browse files

Added some user query requests

parent 06a9f47a
No related branches found
No related tags found
No related merge requests found
test('dummy test case', () => {
expect(true).toEqual(true);
});
...@@ -12,12 +12,16 @@ app.use(bodyJson()); ...@@ -12,12 +12,16 @@ app.use(bodyJson());
app.use('/v1', v1); app.use('/v1', v1);
app.use((_req, res) => { app.use((_req, res) => {
res.sendStatus(404); res.status(404).json({
status: 'error',
message: 'unknown resource',
});
}); });
app.use((_err: Error, _req: Request, res: Response, _next: NextFunction) => { app.use((_err: Error, _req: Request, res: Response, _next: NextFunction) => {
return res.status(400).json({ return res.status(400).json({
status: 'error', status: 'error',
message: 'unknown error',
}); });
}); });
......
...@@ -3,10 +3,10 @@ import { readFile } from 'fs'; ...@@ -3,10 +3,10 @@ import { readFile } from 'fs';
export const isOfType = <T>( export const isOfType = <T>(
varToBeChecked: any, varToBeChecked: any,
propertyToCheckFor: (keyof T)[] propertyToCheckFor: [(keyof T), string][]
): varToBeChecked is T => { ): varToBeChecked is T => {
for (const key of propertyToCheckFor) { for (const [key, type] of propertyToCheckFor) {
if (!(varToBeChecked as T)[key]) { if (typeof (varToBeChecked as T)[key] !== type) {
return false; return false;
} }
} }
......
...@@ -16,7 +16,7 @@ interface RegisterBody { ...@@ -16,7 +16,7 @@ interface RegisterBody {
} }
auth.post('/register', async (req, res) => { auth.post('/register', async (req, res) => {
if (isOfType<RegisterBody>(req.body, ['username', 'password'])) { if (isOfType<RegisterBody>(req.body, [['username', 'string'], ['password', 'string']])) {
const body: RegisterBody = req.body; const body: RegisterBody = req.body;
const id = uuid(); const id = uuid();
const passwdHash = await hash(body.password, 10); const passwdHash = await hash(body.password, 10);
...@@ -30,6 +30,7 @@ auth.post('/register', async (req, res) => { ...@@ -30,6 +30,7 @@ auth.post('/register', async (req, res) => {
status: 'success', status: 'success',
}); });
} catch (e) { } catch (e) {
// Fails if unique constraint for username is not met
res.status(400).json({ res.status(400).json({
status: 'error', status: 'error',
message: 'failed to create user', message: 'failed to create user',
...@@ -49,7 +50,7 @@ interface TokenBody { ...@@ -49,7 +50,7 @@ interface TokenBody {
} }
auth.post('/token', async (req, res) => { auth.post('/token', async (req, res) => {
if (isOfType<TokenBody>(req.body, ['username', 'password'])) { if (isOfType<TokenBody>(req.body, [['username', 'string'], ['password', 'string']])) {
const body: TokenBody = req.body; const body: TokenBody = req.body;
try { try {
const user = await database('users').where({ user_name: body.username }); const user = await database('users').where({ user_name: body.username });
...@@ -89,6 +90,8 @@ auth.post('/token', async (req, res) => { ...@@ -89,6 +90,8 @@ auth.post('/token', async (req, res) => {
} }
}); });
auth.use(requireVerification);
auth.get("/extend", async function (req, res) { auth.get("/extend", async function (req, res) {
if (req.body?.token) { if (req.body?.token) {
const token = await asyncify(sign, { const token = await asyncify(sign, {
...@@ -112,8 +115,10 @@ export async function tokenVerification(req: Request, res: Response, next: NextF ...@@ -112,8 +115,10 @@ export async function tokenVerification(req: Request, res: Response, next: NextF
if (header) { if (header) {
const bearer = header.split(' '); const bearer = header.split(' ');
token = bearer[1]; token = bearer[1];
} else if (req.body?.token) { } else if (!req.body) {
token = req.body?.token; req.body = {};
} else if (req.body.token) {
token = req.body.token;
} }
if (token) { if (token) {
try { try {
...@@ -131,5 +136,16 @@ export async function tokenVerification(req: Request, res: Response, next: NextF ...@@ -131,5 +136,16 @@ export async function tokenVerification(req: Request, res: Response, next: NextF
} }
} }
export function requireVerification(req: Request, res: Response, next: NextFunction) {
if (req.body.token) {
next();
} else {
res.status(403).json({
status: 'error',
message: 'authentication failed',
});
}
}
export default auth; export default auth;
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
import express from 'express'; import express from 'express';
import auth, { tokenVerification } from './auth'; import auth, { tokenVerification } from './auth';
import user from './user';
const v1 = express(); const v1 = express();
v1.use(tokenVerification); v1.use(tokenVerification);
v1.use('/auth', auth); v1.use('/auth', auth);
v1.use('/user', user);
export default v1; export default v1;
import express from 'express';
import database from '../database';
import { isOfType } from '../util';
import { requireVerification } from './auth';
const user = express();
user.get('/name/:username', async (req, res) => {
try {
const user = await database('users')
.select({
id: 'id',
username: 'user_name',
email: 'email',
realname: 'real_name',
})
.where({ username: req.params.username });
if (user.length === 1) {
res.status(200).json({
status: 'success',
user: user[0],
});
} else {
res.status(404).json({
status: 'error',
message: 'user not found',
});
}
} catch (e) {
res.status(400).json({
status: 'error',
message: 'failed get user',
});
}
});
user.use(requireVerification);
user.get('/', async (req, res) => {
try {
const user = await database('users')
.select({
id: 'id',
username: 'user_name',
email: 'email',
realname: 'real_name',
})
.where({ id: req.body.token.id });
if (user.length === 1) {
res.status(200).json({
status: 'success',
user: user[0],
});
} else {
res.status(404).json({
status: 'error',
message: 'user not found',
});
}
} catch (e) {
res.status(400).json({
status: 'error',
message: 'failed get user',
});
}
});
export default user;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment