-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Reorganize config and add bcryptRounds config * wip * Progress * Signup working with automatic schema generation * Update readme with new run command * Improve GHA concurrency
- Loading branch information
Showing
31 changed files
with
848 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ npm-debug.log | |
yarn-error.log | ||
testem.log | ||
/typings | ||
.nx | ||
|
||
# System Files | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const fs = require("node:fs"); | ||
const path = require("node:path"); | ||
const _ = require("lodash"); | ||
|
||
exports["generate-schemas"] = async () => { | ||
const tsj = require("ts-json-schema-generator"); | ||
const schemasIndex = require("./tools/schemas-index.json"); | ||
const prettier = require("prettier"); | ||
|
||
const templateContent = fs.readFileSync(path.resolve(__dirname, "./tools/schema.ts.tpl")); | ||
const compiledTemplate = _.template(templateContent); | ||
|
||
/** @type {import('ts-json-schema-generator/dist/src/Config').Config} */ | ||
const baseConfig = { | ||
tsconfig: path.resolve(__dirname, "./apps/teloalapi/tsconfig.app.json"), | ||
topRef: true, | ||
skipTypeCheck: true, | ||
}; | ||
|
||
const baseOutputPath = path.resolve(__dirname, "apps/teloalapi/src/schemas/"); | ||
|
||
for (const { filePath, typeName, additionalOptions, keepDefinition, asConst } of schemasIndex) { | ||
console.log(`Processing type '${typeName}' from '${filePath}'`); | ||
|
||
const schema = tsj | ||
.createGenerator({ | ||
...baseConfig, | ||
path: filePath, | ||
...additionalOptions, | ||
}) | ||
.createSchema(typeName); | ||
|
||
let schemaString = JSON.stringify(schema, null, 2); | ||
|
||
if (keepDefinition) { | ||
schemaString = schemaString.replace( | ||
/"#\/definitions\/([a-z-_]+)"/gi, | ||
`"#/components/schemas/$1"`, | ||
); | ||
} | ||
|
||
const outputPath = path.resolve(baseOutputPath, `${typeName}.schema.ts`); | ||
|
||
const generatedFile = compiledTemplate({ | ||
typeName, | ||
schemaString, | ||
filePath, | ||
asConst: asConst ?? false, | ||
}); | ||
|
||
const formattedFile = await prettier.format(generatedFile, { filepath: outputPath }); | ||
|
||
fs.writeFileSync(outputPath, formattedFile); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
exports.parseIntDefault = function parseIntDefault(str, defaultValue) { | ||
const res = parseInt(str, 10); | ||
|
||
if (Object.is(NaN, res)) { | ||
return defaultValue; | ||
} | ||
|
||
return res; | ||
}; | ||
|
||
exports.parseBoolDefault = function parseBoolDefault(str, defaultValue) { | ||
if (str === "true") { | ||
return true; | ||
} | ||
|
||
if (str === "false") { | ||
return false; | ||
} | ||
|
||
return defaultValue; | ||
}; | ||
|
||
exports.urlDefault = function urlDefault(maybeUrl, defaultValue) { | ||
if (!maybeUrl) { | ||
return defaultValue; | ||
} | ||
|
||
return maybeUrl.endsWith("/") ? maybeUrl.substring(0, -1) : maybeUrl; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { HttpErrors, RequestBodyObject, api, post, requestBody, response } from "@loopback/rest"; | ||
import { repository } from "@loopback/repository"; | ||
import { UsersRepository } from "../repositories/Users.repository"; | ||
import { userSignupBodySchema } from "../schemas/UserSignupBody.schema"; | ||
import { ConflictErrorResponse } from "../schemas/responses/ConflictErrorResponse"; | ||
import { ValidationErrorResponse } from "../schemas/responses/ValidationErrorResponse"; | ||
import { SuccessResponse } from "../schemas/responses/SuccessResponse"; | ||
import { SuccessResponseObject } from "../types/SuccessResponseObject"; | ||
import { hashPassword } from "@teloal/pseudo-crypto"; | ||
import config from "config"; | ||
import { UserRole } from "../types/User"; | ||
|
||
export type UserSignupBody = { | ||
/** | ||
* The chosen username for the account. Must be unique | ||
* | ||
* @minLength 3 | ||
* @maxLength 20 | ||
* @pattern ^[a-zA-Z](?:[a-zA-Z0-9]+[-_]?)+[a-zA-Z0-9]+$ | ||
*/ | ||
username: string; | ||
|
||
/** | ||
* The chosen password for the account. Must be at least 10 chars long. | ||
* | ||
* @minLength 10 | ||
*/ | ||
password: string; | ||
}; | ||
|
||
const userSignupBodyDesc: Partial<RequestBodyObject> = { | ||
description: "Required input for signup", | ||
required: true, | ||
content: { | ||
"application/json": { | ||
schema: userSignupBodySchema, | ||
}, | ||
}, | ||
}; | ||
|
||
@api({ basePath: "/v1/auth" }) | ||
export class AuthController { | ||
@repository(UsersRepository) | ||
protected usersRepo: UsersRepository; | ||
|
||
@post("/signup") | ||
@response(200, SuccessResponse) | ||
@response(409, ConflictErrorResponse) | ||
@response(422, ValidationErrorResponse) | ||
async signup( | ||
@requestBody(userSignupBodyDesc) signupPayload: UserSignupBody, | ||
): Promise<SuccessResponseObject> { | ||
const { username, password } = signupPayload; | ||
|
||
if (password.length < 10) { | ||
throw new HttpErrors.BadRequest(`The password must be at least 10 characters long.`); | ||
} | ||
|
||
if (await this.usersRepo.exists(username)) { | ||
throw new HttpErrors.Conflict(`This username is already in use.`); | ||
} | ||
|
||
const hashedPassword = await hashPassword( | ||
password, | ||
config.auth.bcryptRounds, | ||
config.auth.pepper, | ||
); | ||
|
||
try { | ||
await this.usersRepo.create({ | ||
username, | ||
password: hashedPassword, | ||
role: UserRole.user, | ||
}); | ||
} catch (err) { | ||
console.error(err); | ||
throw new HttpErrors.InternalServerError("Something went wrong. Try again later."); | ||
} | ||
|
||
return { success: true }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.