From c401b61a144011ba28302359ccee94d14162f2f2 Mon Sep 17 00:00:00 2001 From: TI JONNY Date: Fri, 13 Sep 2024 15:21:08 -0300 Subject: [PATCH] create user api --- functions/src/index.ts | 2 - src/controller/classroom/createClassroom.js | 7 +- src/controller/user/createUser.js | 92 ++++++++++++++------- src/services/api.js | 6 +- yarn.lock | 8 +- 5 files changed, 72 insertions(+), 43 deletions(-) diff --git a/functions/src/index.ts b/functions/src/index.ts index e97910d..7ba3ebd 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -37,8 +37,6 @@ export const userUpdate = updateUserData(corsMiddleware); export const addPointsStud = addPointsStudent(corsMiddleware); -export const addPointsStud = addPointsStudent(corsMiddleware) - export const helloWorld = functions.https.onRequest((request, response) => { corsMiddleware(request, response, () => { response.send("Hello from Firebase!"); diff --git a/src/controller/classroom/createClassroom.js b/src/controller/classroom/createClassroom.js index 96a427b..9ec200a 100644 --- a/src/controller/classroom/createClassroom.js +++ b/src/controller/classroom/createClassroom.js @@ -1,11 +1,9 @@ -import { firestore } from "../../config/firebase" -import { collection } from "@firebase/firestore" -import { addDoc } from "@firebase/firestore" +import { addDoc, collection } from "@firebase/firestore"; +import { firestore } from "../../config/firebase"; const handleSubmitClassroom = (body, history) => { const ref = collection(firestore, "classroom"); - let data = body try { @@ -16,4 +14,5 @@ const handleSubmitClassroom = (body, history) => { } } + export default handleSubmitClassroom \ No newline at end of file diff --git a/src/controller/user/createUser.js b/src/controller/user/createUser.js index 273ebe7..e7affcb 100644 --- a/src/controller/user/createUser.js +++ b/src/controller/user/createUser.js @@ -4,46 +4,74 @@ // import { addDoc } from "@firebase/firestore"; // import Swal from "sweetalert2"; -// ==================================================================================== -import axios from 'axios'; +import Swal from "sweetalert2"; +import api from "../../services/api"; + +export const CreateUserRequest = async (body) => { + return await api.post("/userCreate", body).then((data) => { + Swal.fire({ + title: "Sucesso!", + text: "Usuário criado com sucesso!", + icon: "success", + confirmButtonText: "Ok", + }); + }).catch((error) => { + console.error("Erro ao criar usuário", error); + Swal.fire({ + title: "Erro!", + text: "Erro", + icon: "error", + confirmButtonText: "Ok", + }); + throw error; + }) +} + export async function createUser(data) { try { - await axios.post(`https://us-central1-br-ipti-visao.cloudfunctions.net/userCreate`); + const userData = { + name: data.name, + role: data.role, + email: data.email, + }; + + CreateUserRequest(userData) + + console.log("Usuário criado com sucesso"); } catch (err) { - console.error("Erro ao criar usuário", err); - throw err; + console.error("Erro ao criar usuário", err); + throw err; } } // ==================================================================================== // export function createUser(data) { -// // Create the user with email and password -// createUserWithEmailAndPassword(auth, data.email, data.password) -// .then((userCredential) => { -// // User created successfully -// const user = userCredential.user; - -// const userData = { -// name: data.name, -// role: data.role, -// email: user.email, -// uid: user.uid, -// }; - -// const ref = collection(firestore, "userData"); -// addDoc(ref, userData); -// Swal.fire({ -// title: "Sucesso!", -// text: "Usuário criado com sucesso!", -// icon: "success", -// confirmButtonText: "Ok", -// }); - -// }) -// .catch((error) => { -// // Handle errors -// console.error("Error creating user:", error.message); +// Create the user with email and password +// createUserWithEmailAndPassword(auth, data.email, data.password) +// .then((userCredential) => { +// // User created successfully +// const user = userCredential.user; + +// const userData = { +// name: data.name, +// role: data.role, +// email: user.email, +// uid: user.uid, +// }; + +// const ref = collection(firestore, "userData"); +// addDoc(ref, userData); +// Swal.fire({ +// title: "Sucesso!", +// text: "Usuário criado com sucesso!", +// icon: "success", +// confirmButtonText: "Ok", // }); -// } \ No newline at end of file + +// }) +// .catch((error) => { +// // Handle errors +// console.error("Error creating user:", error.message); +// }); diff --git a/src/services/api.js b/src/services/api.js index 1320166..4b1fba0 100644 --- a/src/services/api.js +++ b/src/services/api.js @@ -1,8 +1,12 @@ import axios from "axios"; import { getToken } from "./auth"; + + + + const api = axios.create({ -   baseURL: "https://apitag.azurewebsites.net/" +   baseURL: "https://us-central1-br-ipti-visao.cloudfunctions.net/" }); api.interceptors.request.use(async config => { diff --git a/yarn.lock b/yarn.lock index 2a09645..4eb9474 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4246,10 +4246,10 @@ caniuse-api@^3.0.0: lodash.memoize "^4.1.2" lodash.uniq "^4.5.0" -caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001010, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001517: - version "1.0.30001524" - resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001524.tgz" - integrity sha512-Jj917pJtYg9HSJBF95HVX3Cdr89JUyLT4IZ8SvM5aDRni95swKgYi3TgYLH5hnGfPE/U1dg6IfZ50UsIlLkwSA== +caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001010, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001517, caniuse-lite@^1.0.30001651: + version "1.0.30001651" + resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001651.tgz" + integrity sha512-9Cf+Xv1jJNe1xPZLGuUXLNkE1BoDkqRqYyFJ9TDYSqhduqA4hu4oR9HluGoWYQC/aj8WHjsGVV+bwkh0+tegRg== canvg@^3.0.6: version "3.0.10"