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

Fixed the CORS requests beeing blocked

parent fa9e8c58
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,18 @@ export function addDefaultHeaders(req: Request, res: Response, next: NextFunctio
const origin = req.header('Origin');
if (allowedOrigins.includes('*') || origin && allowedOrigins.includes(origin)) {
res.header('Access-Control-Allow-Origin', origin);
const headers = req.header('Access-Control-Request-Headers');
if (headers) {
res.header('Access-Control-Allow-Headers', headers);
}
res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
res.header('Access-Control-Max-Age', '86400');
}
if (req.method === 'OPTIONS') {
// Handle preflight requests
res.send();
} else {
next();
}
next();
}
......@@ -69,7 +69,7 @@ auth.post('/register', async (req, res) => {
const token = await generateToken({ id: id });
await database('users').insert({
id: id,
user_name: body.username,
user_name: body.username.trim(),
passwd_hash: passwdHash,
email: body.email ?? null,
real_name: body.realname ?? null,
......@@ -102,7 +102,7 @@ auth.post('/token', async (req, res) => {
if (isOfType<TokenBody>(req.body, [['username', 'string'], ['password', 'string']])) {
const body: TokenBody = req.body;
try {
const user = await database('users').where({ user_name: body.username });
const user = await database('users').where({ user_name: body.username.trim() });
if (user.length === 1) {
if (await compare(body.password, user[0].passwd_hash)) {
const token = await generateToken({ id: user[0].id });
......@@ -165,7 +165,7 @@ auth.put("/username", async function (req, res) {
const body: UsernameBody = req.body;
try {
await database('users').update({
user_name: body.username,
user_name: body.username.trim(),
}).where({
id: body.token.id,
});
......
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