-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RED-16 - criação de conta - email de confirmação
- Loading branch information
Showing
39 changed files
with
372 additions
and
175 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
service-node-koa/app/config/db/migrations/20240904200402_create_confirma_cadastro_table.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
/** | ||
* confirm user creation table | ||
* | ||
* - all user data | ||
* - confirm expiration time | ||
* - six digit challenge code for this | ||
* | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void>|import("knex").Knex.SchemaBuilder } | ||
*/ | ||
export const up = async (knex) => { | ||
return knex.schema.createTable('confirma_cadastro', tb => { | ||
tb.increments() | ||
tb.string('nome').notNullable() | ||
tb.string('email').notNullable() | ||
tb.string('senha').notNullable() | ||
tb.string('challenge').notNullable() | ||
tb.timestamp("criacao").notNullable().defaultTo(knex.fn.now()) | ||
tb.timestamp("alteracao").notNullable().defaultTo(knex.fn.now()) | ||
tb.boolean('enviado').notNullable().defaultTo(false) | ||
tb.boolean('consumido').notNullable().defaultTo(false) | ||
}) | ||
}; | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void>|import("knex").Knex.SchemaBuilder } | ||
*/ | ||
export const down = async (knex) => { | ||
return knex.schema.dropTable('confirma_cadastro'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {knex} from "../config/db/index.mjs"; | ||
import {logger} from "../config/base-logging.mjs"; | ||
import {encrypt, newChallenge} from "../config/security/index.mjs"; | ||
import {differenceInMinutes} from "date-fns"; | ||
|
||
const log = logger.scope('services/confirma_cadastro.mjs') | ||
|
||
export const validaConfirmaCadastro = async ({email}) => { | ||
log.info('validaConfirmaCadastro') | ||
const exists = await knex("usuario").where({email}).first(); | ||
if (exists) throw {status: 422, message: "Usuario já existe"}; | ||
} | ||
|
||
export const novoConfirmaCadastro = async ({nome, email, senha}) => { | ||
log.info('novoConfirmaCadastro') | ||
const challenge = newChallenge() | ||
senha = encrypt(senha) | ||
return knex("confirma_cadastro").insert({nome, email, senha, challenge}).returning("*") | ||
} | ||
|
||
export const marcarEnvioConfirmaCadastro = async ({email, challenge}) => { | ||
log.info('marcarEnvioConfirmaCadastro') | ||
return knex("confirma_cadastro").update({enviado: true, alteracao: new Date()}).where({email, challenge}); | ||
} | ||
|
||
export const marcarConsumoConfirmaCadastro = async ({email, challenge}) => { | ||
log.info('marcarConsumoConfirmaCadastro') | ||
const confirmaCadastro = await knex("confirma_cadastro").where({email, challenge}).first(); | ||
if (!confirmaCadastro) throw {code: 422, message: 'Código ou email incorretos'} | ||
if (confirmaCadastro.consumido) throw {code: 422, message: 'Código já foi utilizado'} | ||
if (differenceInMinutes(new Date(), confirmaCadastro.criado) > 5) throw {code: 422, message: 'Código expirado'} | ||
await knex("confirma_cadastro").update({consumido: true}).where({email, challenge}); | ||
return confirmaCadastro | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
import { | ||
emailApiUrl, | ||
emailCreds, | ||
singlePayload, | ||
emailApiUrl, | ||
emailCreds, | ||
singlePayload, | ||
} from "../config/email-config.mjs"; | ||
import {logger} from "../config/base-logging.mjs"; | ||
|
||
const log = logger.scope('services/email.mjs') | ||
|
||
// https://developer.mozilla.org/pt-BR/docs/Web/API/Window/fetch | ||
const sendEmail = async ({ email, name, subject, body }) => { | ||
return await fetch(emailApiUrl, { | ||
body: JSON.stringify(singlePayload({ email, name, subject, body })), | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: emailCreds(), | ||
}, | ||
method: "POST", | ||
}); | ||
const sendEmail = async ({email, nome, subject, body}) => { | ||
log.info('sendEmail') | ||
// TODO send to a queue instead | ||
return await fetch(emailApiUrl, { | ||
body: JSON.stringify(singlePayload({email, name: nome, subject, body})), | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Basic ${emailCreds()}`, | ||
}, | ||
method: "POST", | ||
}); | ||
}; | ||
|
||
export const emailConfirmaCadastro = async ({email, nome, challenge}) => { | ||
log.info('emailConfirmaCadastro') | ||
const subject = "Redline - Confirmação de cadastro" | ||
return sendEmail(({ | ||
nome, email, subject, body: ` | ||
Olá ${nome}! | ||
O código de confirmação da criação de conta é: | ||
${challenge} | ||
Informe este código no aplicativo e ative a sua conta. | ||
` | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { knex } from "../config/db/index.mjs"; | ||
import {knex} from "../config/db/index.mjs"; | ||
|
||
export const validaInvite = async ({ email, code }) => { | ||
if (!email) throw { status: 401, message: "Invalid email" }; | ||
if (!code) throw { status: 401, message: "Invalid invite code" }; | ||
export const validaInvite = async ({email, code}) => { | ||
if (!email) throw {status: 401, message: "Email inválido"}; | ||
if (!code) throw {status: 401, message: "Código de convite inválido"}; | ||
|
||
const invite = await knex("invite").where({ email, code }).first(); | ||
const invite = await knex("invite").where({email, code}).first(); | ||
if (!invite) throw {status: 401, message: "Convite não encontrado"}; | ||
|
||
if (!invite) throw { status: 401, message: "Invite code not found" }; | ||
const exists = await knex("usuario").where({email}).first(); | ||
if (exists) throw {status: 422, message: "Usuario já existe"}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.