Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modulo6/case4-doghero/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package-lock.json
build
.env
36 changes: 36 additions & 0 deletions modulo6/case4-doghero/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "case4-doghero",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start:dev": "ts-node-dev ./src/index.ts",
"start": "tsc && node ./build/index.js",
"build": "tsc",
"dev": "ts-node-dev ./src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/knex": "^0.16.1",
"knex": "^2.1.0",
"mysql": "^2.18.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.3"
},
"dependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/jsonwebtoken": "^8.5.8",
"@types/uuid": "^8.3.4",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"jsonwebtoken": "^8.5.1",
"uuid": "^8.3.2"
}
}
32 changes: 32 additions & 0 deletions modulo6/case4-doghero/src/Services/Authenticator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as jwt from "jsonwebtoken";

export class Authenticator {
public generateToken(input: AuthenticationData,
expiresIn: string = process.env.ACCESS_TOKEN_EXPIRES_IN!): string {
const token = jwt.sign(
{
id: input.id,
role: input.role
},
process.env.JWT_KEY as string,
{
expiresIn,
}
);
return token;
}

public getData(token: string): AuthenticationData {
const payload = jwt.verify(token, process.env.JWT_KEY as string) as any;
const result = {
id: payload.id,
role: payload.role
};
return result;
}
}

export interface AuthenticationData {
id: string;
role?: string;
}
17 changes: 17 additions & 0 deletions modulo6/case4-doghero/src/Services/HashManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as bcrypt from "bcryptjs";


export class HashManager {

public async hash(text: string): Promise<string> {
const rounds = 12;
const salt = await bcrypt.genSalt(rounds);
const result = await bcrypt.hash(text, salt);
return result;
}

public async compare(text: string, hash: string): Promise<boolean>{
return await bcrypt.compare(text, hash);
}

}
7 changes: 7 additions & 0 deletions modulo6/case4-doghero/src/Services/IdGeneretor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { v4 } from "uuid";

export class IdGenerator{
public generate(): string{
return v4()
}
}
34 changes: 34 additions & 0 deletions modulo6/case4-doghero/src/Services/TokenGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as jwt from "jsonwebtoken";
import dotenv from "dotenv";

dotenv.config();

export class TokenGenerator {
private static expiresIn: number = 1200;

public generate = (input: AuthenticationData): string => {
const newToken = jwt.sign(
{
id: input.id,
role: input.role,
},
process.env.JWT_KEY as string,
{
expiresIn: TokenGenerator.expiresIn,
}
);
return newToken;
};

public verify(token: string) {
const payload = jwt.verify(token, process.env.JWT_KEY as string) as any;
const result = { id: payload.id, role: payload.role };
return result;
}
}

export interface AuthenticationData {
id: string;
role: string;
}

21 changes: 21 additions & 0 deletions modulo6/case4-doghero/src/data/BaseDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import knex from 'knex'
import dotenv from 'dotenv'

dotenv.config()

export abstract class BaseDatabase{
protected static connection = knex({
client: 'mysql',
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_SCHEMA,
port: 3306,
multipleStatements: true
}
})
public static async destroyConnection(): Promise<void> {
await BaseDatabase.connection.destroy();
}
}
22 changes: 22 additions & 0 deletions modulo6/case4-doghero/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import express, {Express} from 'express'
import cors from 'cors'
import { AddressInfo } from 'net';
import dotenv from "dotenv";
const app: Express = express();

dotenv.config();


app.use(express.json());
app.use(cors())



const server = app.listen(process.env.PORT || 3003, () => {
if (server) {
const address = server.address() as AddressInfo;
console.log(`Servidor rodando em http://localhost:${address.port}`);
} else {
console.error(`Falha ao rodar o servidor.`);
}
});
12 changes: 12 additions & 0 deletions modulo6/case4-doghero/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version */
"module": "commonjs", /* Specify module code generation */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./build", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. */
"removeComments": true, /* Do not emit comments to output. */
"noImplicitAny": true, /* Raise error on declarations with an implied 'any' type. */
"strict": true,
}
}