diff --git a/modulo6/case4-doghero/.gitignore b/modulo6/case4-doghero/.gitignore new file mode 100644 index 0000000..8ece3ba --- /dev/null +++ b/modulo6/case4-doghero/.gitignore @@ -0,0 +1,4 @@ +node_modules +package-lock.json +build +.env \ No newline at end of file diff --git a/modulo6/case4-doghero/package.json b/modulo6/case4-doghero/package.json new file mode 100644 index 0000000..599891f --- /dev/null +++ b/modulo6/case4-doghero/package.json @@ -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" + } +} diff --git a/modulo6/case4-doghero/src/Services/Authenticator.ts b/modulo6/case4-doghero/src/Services/Authenticator.ts new file mode 100644 index 0000000..4a859a0 --- /dev/null +++ b/modulo6/case4-doghero/src/Services/Authenticator.ts @@ -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; +} \ No newline at end of file diff --git a/modulo6/case4-doghero/src/Services/HashManager.ts b/modulo6/case4-doghero/src/Services/HashManager.ts new file mode 100644 index 0000000..45704fb --- /dev/null +++ b/modulo6/case4-doghero/src/Services/HashManager.ts @@ -0,0 +1,17 @@ +import * as bcrypt from "bcryptjs"; + + +export class HashManager { + + public async hash(text: string): Promise { + 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{ + return await bcrypt.compare(text, hash); + } + +} \ No newline at end of file diff --git a/modulo6/case4-doghero/src/Services/IdGeneretor.ts b/modulo6/case4-doghero/src/Services/IdGeneretor.ts new file mode 100644 index 0000000..3392265 --- /dev/null +++ b/modulo6/case4-doghero/src/Services/IdGeneretor.ts @@ -0,0 +1,7 @@ +import { v4 } from "uuid"; + +export class IdGenerator{ + public generate(): string{ + return v4() + } +} \ No newline at end of file diff --git a/modulo6/case4-doghero/src/Services/TokenGenerator.ts b/modulo6/case4-doghero/src/Services/TokenGenerator.ts new file mode 100644 index 0000000..1e212ba --- /dev/null +++ b/modulo6/case4-doghero/src/Services/TokenGenerator.ts @@ -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; +} + diff --git a/modulo6/case4-doghero/src/data/BaseDatabase.ts b/modulo6/case4-doghero/src/data/BaseDatabase.ts new file mode 100644 index 0000000..1d5f6bb --- /dev/null +++ b/modulo6/case4-doghero/src/data/BaseDatabase.ts @@ -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 { + await BaseDatabase.connection.destroy(); + } +} \ No newline at end of file diff --git a/modulo6/case4-doghero/src/index.ts b/modulo6/case4-doghero/src/index.ts new file mode 100644 index 0000000..b6224d0 --- /dev/null +++ b/modulo6/case4-doghero/src/index.ts @@ -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.`); + } +}); \ No newline at end of file diff --git a/modulo6/case4-doghero/tsconfig.json b/modulo6/case4-doghero/tsconfig.json new file mode 100644 index 0000000..b092733 --- /dev/null +++ b/modulo6/case4-doghero/tsconfig.json @@ -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, + } +} \ No newline at end of file