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
3 changes: 3 additions & 0 deletions modulo5/IWFS-cookenu/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
.env
3,076 changes: 3,076 additions & 0 deletions modulo5/IWFS-cookenu/package-lock.json

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions modulo5/IWFS-cookenu/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "iwfs-cookenu",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "tsc && node ./build/index.js",
"dev": "ts-node-dev --transpile-only --ignore-watch node_modules ./src/index.ts",
"test": "ts-node-dev ./src/services/authenticator.ts"
},
"keywords": [],
"author": "Jose Robinaldo",
"license": "ISC",
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.14",
"@types/jsonwebtoken": "^8.5.9",
"@types/knex": "^0.16.1",
"@types/uuid": "^8.3.4",
"ts-node-dev": "^2.0.0",
"typescript": "^4.8.3"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.0.2",
"express": "^4.18.1",
"jsonwebtoken": "^8.5.1",
"knex": "^2.3.0",
"mysql": "^2.18.1",
"ts-node": "^10.9.1",
"uuid": "^9.0.0"
}
}
20 changes: 20 additions & 0 deletions modulo5/IWFS-cookenu/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import express from "express";
import cors from "cors";
import { AddressInfo } from "net";


const app = express();

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 no http://localhost:${address.port}`);
} else {
console.error(`Failure upon starting server.`);
}
});

export default app
18 changes: 18 additions & 0 deletions modulo5/IWFS-cookenu/src/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import knex from "knex";
import dotenv from "dotenv";


dotenv.config();

const connection = knex({
client: "mysql",
connection: {
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_SCHEMA,
}
});

export default connection
24 changes: 24 additions & 0 deletions modulo5/IWFS-cookenu/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import app from "./app"
import { generateToken, getTokenData } from "./services/authenticator";
//import { generateHans, compareHash } from "./services/hashManager";
// import generateId from "./services/idGenerator"


app.get("/")

//const cypherText = generateHans("hello my friends")

const token = generateToken({id:"1qqa3edf5tgbij"})

const tokenData = getTokenData(token)

console.log({
token,
tokenData
}
//generateId()
//compareHash("hello my friends", cypherText)
);



28 changes: 28 additions & 0 deletions modulo5/IWFS-cookenu/src/services/authenticator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import{sign, verify } from "jsonwebtoken"
import { authenticationData } from "../types"
import dotenv from 'dotenv'

dotenv.config()

const {JWT_KEY} = process.env

export const generateToken = (
payload: authenticationData
): string => sign(
payload,
JWT_KEY!,
{expiresIn: "1h"}
)

export const getTokenData = (
token: string
): authenticationData | null => {
try {
const { id } = verify(token, JWT_KEY!) as authenticationData

return { id }

} catch (error) {
return null
}
}
18 changes: 18 additions & 0 deletions modulo5/IWFS-cookenu/src/services/hashManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {hashSync, compareSync, genSaltSync} from "bcryptjs"
import dotenv from "dotenv"

dotenv.config()

export const generateHans = (
plainText: string
): string => {
const rounds: number = Number(process.env.BCRYPT_COST)
const salt: string = genSaltSync(rounds)
return hashSync(plainText, salt)
}


export const compareHash = (
plainText:string,
cypherText: string
): boolean => compareSync(plainText, cypherText)
6 changes: 6 additions & 0 deletions modulo5/IWFS-cookenu/src/services/idGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {v4} from "uuid"


const generateId = () => v4()

export default generateId
3 changes: 3 additions & 0 deletions modulo5/IWFS-cookenu/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type authenticationData = {
id: string
}
15 changes: 15 additions & 0 deletions modulo5/IWFS-cookenu/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "./src", /* Specify the root folder within your source files. */
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
"outDir": "./build", /* Specify an output folder for all emitted files. */
"removeComments": true, /* Disable emitting comments. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}