This commit creates new methods for the authentication system, especially the user registration feature. The update also validates input data and checks if a user already exists in the database. It modifies the application entry point to include the updated user registration route and provides updates to the database service to include user-related functions. The MariaDB collation was also updated in the database schema script to support unicode.
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import {IRegisterInput, IRegisterOutput} from "@interfaces/services/register.types";
|
|
import {UserInDatabase} from "@interfaces/db/mariadb.interface";
|
|
import CredentialService from "@services/authentication/credentials.service";
|
|
import IntCodeService from "@services/authentication/intcode.service";
|
|
import {v4} from "uuid";
|
|
import {DatabasesService} from "@services/databases/databases.service";
|
|
import JwtService from "@services/authentication/jwt.service";
|
|
|
|
const db = new DatabasesService('OnlyDevs')
|
|
//TODO Logs
|
|
|
|
async function registerService(data: IRegisterInput): Promise<IRegisterOutput> {
|
|
const User: UserInDatabase = {
|
|
id: v4(),
|
|
username: data.username,
|
|
display_name: data.displayName || data.username,
|
|
hash: await CredentialService.hash(data.password),
|
|
email: data.email,
|
|
email_activation: IntCodeService.generate()
|
|
}
|
|
|
|
const dbResult = await db.insertUser(User)
|
|
|
|
if (dbResult) {
|
|
//await sendActivationEmail(User.email, User.email_activation);
|
|
const token = await JwtService.sign({
|
|
sub: User.id,
|
|
iat: Date.now(),
|
|
}, {
|
|
alg: "HS256"
|
|
}, '7d', 'Registered user')
|
|
return {
|
|
success: true,
|
|
message: "User registered successfully",
|
|
id: User.id,
|
|
token: token
|
|
};
|
|
} else {
|
|
return {
|
|
success: false,
|
|
message: "Failed to register user",
|
|
};
|
|
}
|
|
}
|
|
|
|
export default registerService; |