Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
splatterxl committed Oct 26, 2024
1 parent 51c886b commit 1a0f244
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 27 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm-lock.yaml
5 changes: 1 addition & 4 deletions src/common/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { psqlClient } from "./database";
import { ErrorCode, RESTError } from "./error";
import { Token, User } from "./types";

export async function checkCredentials(
email: string,
password: string
): Promise<bigint> {
export async function checkCredentials(email: string, password: string): Promise<bigint> {
const userQuery = await psqlClient.query("SELECT * FROM users WHERE email=$1", [email]);
if (!userQuery.rowCount)
throw new RESTError(ErrorCode.AuthError, "Invalid username or password");
Expand Down
6 changes: 3 additions & 3 deletions src/common/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export enum ErrorCode {
*/
export abstract class APIError extends Error {
abstract location: "ws" | "rest";
abstract extra: Record<string, any> & { validation?: any; stack?: string; };
abstract extra: Record<string, any> & { validation?: any; stack?: string };
abstract statusCode: number;

constructor(
Expand Down Expand Up @@ -75,8 +75,8 @@ export class SocketError extends APIError {
super(code, message);
}

statusCode = 500
message = "Internal Server Error"
statusCode = 500;
message = "Internal Server Error";
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/common/moderate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ export function moderateMessage(text: string) {
}

export function shouldModerate(text: string) {
return moderateMessage(text).newText !== text
}
return moderateMessage(text).newText !== text;
}
2 changes: 1 addition & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export type Token = {
};

export type Arguments<T> = T extends (...args: infer U) => any ? U : never;
export type Argument<T, Index extends number> = Arguments<T>[Index];
export type Argument<T, Index extends number> = Arguments<T>[Index];
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dotenv/config.js';
import "dotenv/config.js";
import fastify from "fastify";

import { bootstrap } from "fastify-decorators";
import { resolve } from "path";
import { ERROR_HANDLER } from './common/error.js';
import { ERROR_HANDLER } from "./common/error.js";

export const server = fastify({
logger: {
Expand All @@ -25,7 +25,7 @@ server.register(bootstrap, {
prefix: "/api"
});

server.setErrorHandler(ERROR_HANDLER)
server.setErrorHandler(ERROR_HANDLER);

server.listen({ port: parseInt(process.env.PORT), host: process.env.HOST }, (err, address) => {
if (err) {
Expand Down
23 changes: 9 additions & 14 deletions src/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ export default class AuthController {
const body = req.body as LoginBody;
const auth = await checkCredentials(body.email, body.password);

return { token: await generateToken(auth, true) };
return { token: await generateToken(auth, true) };
}


@POST({
url: "/signup",
options: {
Expand All @@ -67,7 +66,7 @@ export default class AuthController {
async signupHandler(req, res) {
const body = req.body as SignupBody;
if (!(await validateCaptcha(body.turnstileKey)))
throw new RESTError(ErrorCode.ValidationError, "Invalid captcha");
throw new RESTError(ErrorCode.ValidationError, "Invalid captcha");

// Check for existing info
if (
Expand All @@ -79,7 +78,7 @@ export default class AuthController {
throw new RESTError(ErrorCode.ConflictError, "Username or email already exists");

// Moderate username
if (shouldModerate(body.username))
if (shouldModerate(body.username))
throw new RESTError(ErrorCode.ValidationError, "Username contains restricted words");

// Hash password
Expand Down Expand Up @@ -129,18 +128,14 @@ export default class AuthController {
const token = (req.query as any).token;

// Check if token is valid
const query = await psqlClient.query(
"SELECT id FROM email_verifications WHERE token=$1",
[token]
);
if (query.rows.length === 0)
throw new RESTError(ErrorCode.NotFoundError, "Invalid token");
const query = await psqlClient.query("SELECT id FROM email_verifications WHERE token=$1", [
token
]);
if (query.rows.length === 0) throw new RESTError(ErrorCode.NotFoundError, "Invalid token");

// Delete token
await psqlClient.query("DELETE FROM email_verifications WHERE token=$1", [token]);
await psqlClient.query("UPDATE users SET activated=true WHERE id=$1", [
query.rows[0].id
]);
await psqlClient.query("UPDATE users SET activated=true WHERE id=$1", [query.rows[0].id]);
return res.redirect(`https://${process.env.CLIENT_HOSTNAME}/login?confirmed=true`);
}
}
}

0 comments on commit 1a0f244

Please sign in to comment.