diff --git a/next/pages/api/user/getSimpleToken.js b/next/pages/api/user/getSimpleToken.js new file mode 100644 index 00000000..90b8be6c --- /dev/null +++ b/next/pages/api/user/getSimpleToken.js @@ -0,0 +1,26 @@ +import axios from "axios"; + +const API_URL= `${process.env.NEXT_PUBLIC_API_URL}/api/v1/graphql` + +export default async function getSimpleToken({ email, password }) { + try { + const res = await axios({ + url: API_URL, + method: "POST", + data: { + query: ` + mutation { + tokenAuth ( email: "${email}", password: "${password}" ) { + payload, + refreshExpiresIn, + token + } + }` + } + }) + const data = res.data?.data + return data + } catch (error) { + console.error(error) + } +} diff --git a/next/pages/api/user/index.js b/next/pages/api/user/index.js index 1849ca92..782a4c20 100644 --- a/next/pages/api/user/index.js +++ b/next/pages/api/user/index.js @@ -1,4 +1,5 @@ import getToken from "./getToken"; +import getSimpleToken from "./getSimpleToken"; import refreshToken from "./refreshToken"; import validateToken from "./validateToken"; import registerAccount from "./registerAccount"; @@ -7,9 +8,11 @@ import getAllUsers from "./getAllUsers"; import getFullUser from "./getFullUser"; import updateProfile from "./updateProfile"; import updateUser from "./updateUser"; +import updatePassword from "./updatePassword"; export { getToken, + getSimpleToken, refreshToken, validateToken, registerAccount, @@ -17,5 +20,6 @@ export { getAllUsers, getFullUser, updateProfile, - updateUser + updateUser, + updatePassword } diff --git a/next/pages/api/user/refreshToken.js b/next/pages/api/user/refreshToken.js index b0035347..19b84332 100644 --- a/next/pages/api/user/refreshToken.js +++ b/next/pages/api/user/refreshToken.js @@ -20,13 +20,7 @@ export default async function refreshToken() { } }) try { - const data = res.data.data - if(res.data.errors.length > 0) { - cookies.remove('userBD', { path: '/' }) - cookies.remove('token', { path: '/' }) - return res.data - } - cookies.set('token', data?.refreshToken?.token) + const data = res.data return data } catch (error) { console.error(error) diff --git a/next/pages/api/user/updatePassword.js b/next/pages/api/user/updatePassword.js new file mode 100644 index 00000000..0918021a --- /dev/null +++ b/next/pages/api/user/updatePassword.js @@ -0,0 +1,40 @@ +import axios from "axios"; + +const API_URL= `${process.env.NEXT_PUBLIC_API_URL}/api/v1/graphql` + +export default async function updatePassword({ + id, + password, + token +}) { + try { + const res = await axios({ + url: API_URL, + method: "POST", + headers: { + Authorization: `Bearer ${token}` + }, + data: { + query: ` + mutation { + CreateUpdateAccount (input: + { + id: "${id}" + password: "${password}" + } + ) + { + errors { + field, + messages + } + } + }` + } + }) + const data = res.data.data.CreateUpdateAccount + return data + } catch (error) { + console.error(error) + } +} diff --git a/next/pages/user/[username].js b/next/pages/user/[username].js index 7974d9e5..ca4f7811 100644 --- a/next/pages/user/[username].js +++ b/next/pages/user/[username].js @@ -29,6 +29,7 @@ import { } from "@chakra-ui/react"; import { useState, useEffect } from "react"; import { useRouter } from "next/router"; +import cookies from 'js-cookie'; import { MainPageTemplate } from "../../components/templates/main"; import { isMobileMod } from "../../hooks/useCheckMobile.hook"; import { removeSubscription } from "../api/stripe"; @@ -44,8 +45,11 @@ import PaymentSystem from "../../components/organisms/PaymentSystem"; import { getUserDataJson, checkUserInfo, cleanUserInfo } from "../../utils"; import { + getSimpleToken, getFullUser, updateProfile, + refreshToken, + updatePassword } from "../api/user"; import Exclamation from "../../public/img/icons/exclamationIcon"; @@ -852,12 +856,8 @@ const NewPassword = ({ userInfo }) => { newPassword: "", confirmPassword: "" }) - const [errors, setErrors] = useState({ - password: "", - newPassword: "", - regexPassword: {}, - confirmPassword: "" - }) + const [errors, setErrors] = useState({}) + const [showPassword, setShowPassword] = useState(true) const [showNewPassword, setShowNewPassword] = useState(true) const [showConfirmPassword, setShowConfirmPassword] = useState(true) @@ -869,7 +869,59 @@ const NewPassword = ({ userInfo }) => { })) } - const submitNewPassword = () => { + async function submitNewPassword() { + const regexPassword = {} + const validationErrors = {} + + if(formData.password !== "" && formData.password === formData.newPassword) { + validationErrors.newPassword = "A nova senha tem quer ser diferente da atual" + } + if(!/^.{8,}$/.test(formData.newPassword)) { + regexPassword = {...regexPassword, amount: true} + } + if(!/[A-Z]/.test(formData.newPassword)) { + regexPassword = {...regexPassword, upperCase: true} + } + if(!/[a-z]/.test(formData.newPassword)) { + regexPassword = {...regexPassword, lowerCase: true} + } + if(!/(?=.*?[0-9])/.test(formData.newPassword)) { + regexPassword = {...regexPassword, number: true} + } + if(!/(?=.*?[#?!@$%^&*-])/.test(formData.newPassword)) { + regexPassword = {...regexPassword, special: true} + } + if (!formData.confirmPassword) { + validationErrors.confirmPassword = "Confirmar a senha é necessário" + } + if(formData.confirmPassword !== formData.newPassword) { + validationErrors.confirmPassword = "A senha inserida não coincide com a senha criada no campo acima. Por favor, verifique se não há erros de digitação e tente novamente." + } + + if(Object.keys(regexPassword).length > 0) validationErrors.regexPassword = regexPassword + + if(formData.password === "") { + validationErrors.password = "Confirmar a senha atual é necessário" + } + + let getTokenPassword + if(formData.password !== "") { + getTokenPassword = await getSimpleToken({email: userInfo.email, password: formData.password}) + if(getTokenPassword?.tokenAuth === null || result?.errors?.length > 0) { + validationErrors.password = "Senha incorreta" + } + } + setErrors(validationErrors) + + if (Object.keys(validationErrors).length > 0) return + + const reg = new RegExp("(?<=:).*") + const [ id ] = reg.exec(userInfo?.id) + const form = {id: id, password: formData.newPassword, token: getTokenPassword?.tokenAuth?.token} + + const result = await updatePassword(form) + setFormData({}) + newPasswordModal.onOpen() } @@ -999,7 +1051,7 @@ const NewPassword = ({ userInfo }) => { - + { /> 0 ? "#D93B3B" : "#7D7D7D"} + color= { errors?.regexPassword ? Object.keys(errors?.regexPassword).length > 0 ? "#D93B3B" : "#7D7D7D" : "#7D7D7D" } fontFamily= "Ubuntu" fontSize= "12px" fontWeight= "400" @@ -1047,7 +1099,7 @@ const NewPassword = ({ userInfo }) => { flexDirection="row" gap="4px" alignItems="flex-start" - > 0 ? "flex" : "none"}/> Certifique-se que a senha tenha no mínimo: + > 0 ? "flex" : "none" : "none"}/> Certifique-se que a senha tenha no mínimo: 8 caracteres Uma letra maiúscula @@ -1055,6 +1107,11 @@ const NewPassword = ({ userInfo }) => { Um dígito Um caractere especial + {errors.newPassword && + + {errors.newPassword} + + } @@ -1692,6 +1749,21 @@ export default function UserPage({ fullUser }) { const [userInfo, setUserInfo] = useState({}) const [sectionSelected, setSectionSelected] = useState(0) + async function refreshTokenValidate() { + const result = await refreshToken() + + if(result?.data?.refreshToken?.token) return cookies.set('token', result.data.refreshToken.token) + if(result?.errors?.length > 0) { + cookies.remove('userBD', { path: '/' }) + cookies.remove('token', { path: '/' }) + window.open("/user/login", "_self") + } + } + + useEffect(() => { + refreshTokenValidate() + }, []) + useEffect(() => { setUserInfo(fullUser) }, [fullUser])