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
7 changes: 7 additions & 0 deletions Semana17/Aula1/template-intro-autenticacao/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
build

.env

.vscode
requests.rest
2,652 changes: 2,652 additions & 0 deletions Semana17/Aula1/template-intro-autenticacao/package-lock.json

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions Semana17/Aula1/template-intro-autenticacao/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "to-do-list",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "tsc && node --inspect ./build/index.js",
"dev-start": "ts-node-dev ./src/index.ts",
"test": "ts-node-dev ./src/services/authenticator.ts"
},
"author": "Labenu",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"knex": "^0.21.5",
"mysql": "^2.18.1",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/jsonwebtoken": "^8.5.1",
"@types/knex": "^0.16.1",
"@types/node": "^14.11.2",
"@types/uuid": "^8.3.0",
"jsonwebtoken": "^8.5.1",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { v4 } from "uuid";
import * as jwt from "jsonwebtoken";
import { AuthenticationData } from '../types'

// Exercicio 1 letra b

export function idcreator(): string {
return v4();
}

console.log("Generated Id: ", idcreator);

export default idcreator

// Exercicio 3 letra b
const expiresIn = '1min'
export const idgenerator = (input: AuthenticationData): string => {

return jwt.sign(
input,

process.env.JWT_KEY! as string,


{
expiresIn
}

);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import connection from '../connection'

const userTableName = "User";

const userinserter = async (id: string, email: string, password: string) => {
try {
const retorno = await connection.raw(
`
INSERT INTO "${userTableName}" (id, email, password)
VALUES ( "${id}", "${email}", "${password}" );

`)
return retorno

} catch (error) {
return error
}

}
13 changes: 13 additions & 0 deletions Semana17/Aula1/template-intro-autenticacao/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import express from 'express'
import cors from 'cors'

const app = express()

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

app.listen(3003, ()=>{
console.log('Servidor rodando na porta 3003')
})

export default app
18 changes: 18 additions & 0 deletions Semana17/Aula1/template-intro-autenticacao/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,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_SCHEMA,
port: 3306,
multipleStatements: true
}
})

export default connection
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Request, Response } from "express";
import connection from "../connection";
import { user, userExercicio, AuthenticationData } from "../types";
import {idgenerator, idcreator} from '../Components/idcreator'

//Exercicio 2 letra c
export default async function createUser(
req: Request,
res: Response
): Promise<void> {
try {


} catch (error) {

if (res.statusCode === 200) {
res.status(500).send({ message: "Internal server error" })
} else {
res.send({ message: error.message })
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Request, Response } from "express";
import connection from "../connection";

export default async function createUser(
req: Request,
res: Response
): Promise<void> {
try {

const { name, nickname } = req.body

if (!name && !nickname) {
res.statusCode = 422
res.statusMessage = "Informe o(s) novo(s) 'name' ou 'nickname'"
throw new Error()
}

await connection('to_do_list_users')
.update({ name, nickname })
.where({ id: req.params.id })

res.end()

} catch (error) {

if (res.statusCode === 200) {
res.status(500).end()
}

res.end()
}
}
6 changes: 6 additions & 0 deletions Semana17/Aula1/template-intro-autenticacao/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import app from "./app"
import editUser from './endpoints/editUser'
import createUser from './endpoints/createUser'

app.post('/user/signup', createUser)
app.put('/user/edit/:id', editUser)
18 changes: 18 additions & 0 deletions Semana17/Aula1/template-intro-autenticacao/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type user = {
id: string
email: string
password: string
name: string
nickname: string
}

export type userExercicio = {
id: string
email: string
password: string

}

export type AuthenticationData = {
id: string;
}
24 changes: 24 additions & 0 deletions Semana17/Aula1/template-intro-autenticacao/tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CREATE TABLE IF NOT EXISTS to_do_list_users (
id VARCHAR(64) PRIMARY KEY,
name VARCHAR(64) NOT NULL,
nickname VARCHAR(64) NOT NULL,
email VARCHAR(64) NOT NULL
);

CREATE TABLE IF NOT EXISTS to_do_list_tasks (
id VARCHAR(64) PRIMARY KEY,
title VARCHAR(64) NOT NULL,
description VARCHAR(1024) DEFAULT "No description provided",
deadline DATE,
status ENUM("TO_DO", "DOING", "DONE") DEFAULT "TO_DO",
author_id VARCHAR(64),
FOREIGN KEY (author_id) REFERENCES to_do_list_users(id)
);

CREATE TABLE IF NOT EXISTS to_do_list_assignees (
task_id VARCHAR(64),
assignee_id VARCHAR(64),
PRIMARY KEY (task_id, assignee_id),
FOREIGN KEY (task_id) REFERENCES to_do_list_tasks(id),
FOREIGN KEY (assignee_id) REFERENCES to_do_list_users(id)
);
14 changes: 14 additions & 0 deletions Semana17/Aula1/template-intro-autenticacao/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./build",
"rootDir": "./src",
"removeComments": true,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}