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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
build
.env
.vscode
5,677 changes: 5,677 additions & 0 deletions modulo4/arquitetura-de-software-1/template-arquitetura/package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"scripts": {
"start": "tsc && node --inspect ./build/index.js",
"dev": "tsnd --transpile-only --ignore-watch node_modules ./src/index.ts"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"knex": "^0.21.5",
"mysql": "^2.18.1",
"uuid": "^8.3.1"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/jsonwebtoken": "^8.5.0",
"@types/knex": "^0.16.1",
"@types/node": "^14.11.2",
"@types/uuid": "^8.3.0",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

import { UserDataBase } from "../data/UserDataBase"
import { Authenticator } from "../services/authenticator"
import { compare } from "../services/hashManager"
import { user } from "../types/user"

const userDB = new UserDataBase()

export class UserBusiness{

singUp = async () => {


}

login = async (email:string, password:string) => {
if (!email || !password) {
throw new Error("'email' e 'senha' são obrigatórios")
}

const user: user = await userDB.selectUserByEmail(email)

if (!user) {
throw new Error("Usuário não encontrado ou senha incorreta")
}

const passwordIsCorrect: boolean = await compare(password, user.password)

if (!passwordIsCorrect) {
throw new Error("Usuário não encontrado ou senha incorreta")
}
const authenticator = new Authenticator()
const token: string = authenticator.generateToken({
id: user.id,
role: user.role
})
return token
}






}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Request, Response } from "express"
import { UserBusiness } from "../business/UserBusiness"

const userBusiness = new UserBusiness()
export class UserControler {
login = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { email, password } = req.body

const token = userBusiness.login(email, password)

res.send({
message: "Usuário logado!",
token
})

} catch (error: any) {
res.status(400).send(error.message)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from 'express'
import cors from 'cors'

export const app = express()

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

app.listen(3003, () => {
console.log('Servidor rodando na porta 3003')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import knex from 'knex'
import dotenv from 'dotenv'

dotenv.config()


export 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
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { user } from "../types/user";
import { BaseDataBase } from "./BaseDataBase";

export class UserDataBase extends BaseDataBase {

public insertUser = async(
user: user
) => {
await UserDataBase.connection.insert({
id: user.id,
name: user.name,
nickname: user.nickname,
email: user.email,
password: user.password,
role: user.role
}).into('to_do_list_users')
}


selectUserByEmail = async (
email: string
): Promise<user> => {
try {
const result = await UserDataBase.connection("to_do_list_users")
.select("*")
.where({ email })

return {
id: result[0].id,
name: result[0].name,
nickname: result[0].nickname,
email: result[0].email,
password: result[0].password,
role: result[0].role
}

} catch (error: any) {
throw new Error(error.slqMessage || error.message)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// import { connection } from "./BaseDataBase";
// import { task } from "../types/task";

// export const insertTask = async (
// task: task
// ) => {
// await connection('to_do_list_tasks')
// .insert({
// id: task.id,
// title: task.title,
// description: task.description,
// deadline: task.deadline,
// author_id: task.authorId
// })
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import { connection } from "./BaseDataBase";

// export const selectTaskById = async (
// id: string
// ): Promise<any> => {
// const result = await connection.raw(`
// SELECT tasks.*, nickname FROM to_do_list_tasks AS tasks
// JOIN to_do_list_users AS users
// ON author_id = users.id
// WHERE tasks.id = '${id}';
// `)

// return result[0][0]
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// import { Request, Response } from "express";
// import {insertTask} from "../data/insertTask";
// import { generateId } from "../services/idGenerator";

// export const createTask = async (
// req: Request,
// res: Response
// ) => {
// try {

// const { title, description, deadline, authorId } = req.body

// if (
// !title ||
// !description ||
// !deadline ||
// !authorId
// ) {
// throw new Error('"title", "description", "deadline" e "authorId" são obrigatórios')
// }

// const id: string = generateId()

// await insertTask({
// id,
// title,
// description,
// deadline,
// authorId,
// })

// res.status(201).end()

// } catch (error : any) {

// res.statusMessage = error.message
// res.status(500).end()
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// import { Request, Response } from "express";
// import {selectTaskById} from "../data/selectTaskById";

// export const getTaskById = async (
// req: Request,
// res: Response
// ) => {
// try {

// const { id } = req.params

// const result = await selectTaskById(id)

// if (!result) {
// throw new Error("Tarefa não encontrada")
// }

// const taskWithUserInfo = {
// id: result.id,
// title: result.title,
// description: result.description,
// deadline: result.deadline,
// status: result.status,
// authorId: result.author_id,
// authorNickname: result.nickname
// }

// res.status(200).send(taskWithUserInfo)

// } catch (error: any) {
// res.status(400).send(error.message)
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// import { Request, Response } from "express";
// import { insertUser } from "../data/insertUser";
// import { Authenticator } from "../services/authenticator";
// import { hash } from "../services/hashManager";
// import { generateId } from "../services/idGenerator";

// export const signup = async (
// req: Request,
// res: Response
// ) => {
// try {
// const { name, nickname, email, password, role } = req.body

// if (
// !name ||
// !nickname ||
// !email ||
// !password ||
// !role
// ) {
// throw new Error('Preencha os campos "name","nickname", "email" e "password"')
// }

// const id: string = generateId()

// const cypherPassword = await hash(password);

// await insertUser({
// id,
// name,
// nickname,
// email,
// password: cypherPassword,
// role
// })
// const authenticator = new Authenticator()
// const token: string = authenticator.generateToken({
// id,
// role: role
// })

// res
// .status(201)
// .send({
// message: "Usuário criado!",
// token
// })

// } catch (error: any) {
// res.status(400).send(error.message)
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { app } from "./controller/app"
import { UserControler } from "./controller/UserControler"
// import { signup } from './endpoints/signup'
// import { createTask } from './endpoints/createTask'
// import { getTaskById } from './endpoints/getTaskById'
// import { login } from './endpoints/login'

const userControler = new UserControler()


// app.post('/user/signup', signup)
app.post('/user/login', userControler.login)

// app.put('/task', createTask)
// app.get('/task/:id', getTaskById)

console.log("tudo 2...")
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as jwt from "jsonwebtoken"
import { authenticationData } from "../types/user"


export class Authenticator{


public generateToken = (
payload: authenticationData
): string => {
return jwt.sign(
payload,
process.env.JWT_KEY as string,
{
expiresIn: "24min"
}
)
}

public getTokenData = (
token: string
): authenticationData => {
return jwt.verify(
token,
process.env.JWT_KEY as string
) as authenticationData
}

}
Loading