From 7a4485b3a4f49162902f764378d65b297cd714cb Mon Sep 17 00:00:00 2001 From: GabrielTrettel Date: Wed, 23 Feb 2022 20:17:45 -0300 Subject: [PATCH] Starting to conncect change password to api calls --- src/app/api/auth.js | 39 ++++++++-- src/app/auth/authClient.js | 6 +- src/app/screens/PasswordRecoveryScreen.js | 89 +++++++++++------------ 3 files changed, 80 insertions(+), 54 deletions(-) diff --git a/src/app/api/auth.js b/src/app/api/auth.js index 5aaef88..392dcd3 100644 --- a/src/app/api/auth.js +++ b/src/app/api/auth.js @@ -1,5 +1,5 @@ import { create } from "apisauce"; -import authClient from "../auth/authClient"; +import { authClient, authChangePswdClient } from "../auth/authClient"; import authStorage from "../auth/storage"; function login(name, password) { @@ -44,9 +44,8 @@ function signup({ return authClient.post(`/signup`, body); } - async function userPersonalData() { - const authToken = await authStorage.getToken() + const authToken = await authStorage.getToken(); const localClient = create({ baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users", }); @@ -55,10 +54,9 @@ async function userPersonalData() { return localClient.get(`/me`); } - async function userActivation(code) { const { username } = await authStorage.getUser(); - const authToken = await authStorage.getToken() + const authToken = await authStorage.getToken(); // console.log("TOKEN ACTIVATE: "+authToken); // console.log("USER NAME: " + username); @@ -67,8 +65,10 @@ async function userActivation(code) { }); localClient.setHeader("Authorization", `Bearer ${authToken}`); - - return localClient.post(`/activate?username=${username}&activationkey=${code}`); + + return localClient.post( + `/activate?username=${username}&activationkey=${code}` + ); } async function existUsername(username) { @@ -79,5 +79,28 @@ async function existNickname(nickname) { return authClient.post(`/existsByNickname?nickname=${nickname}`); } +function loginByUsernamAnswers(username, secQuestionId, secQuestionAnswer) { + const body = [ + { + id: 1, + forgotpasswordquestionsid: secQuestionId, + usersid: 1, + answer: secQuestionAnswer + } + ]; + + return authChangePswdClient.post( + `/loginbyusernameandanswers?username=${username}`, + body + ); +} -export { login, signup, userPersonalData, userActivation, existNickname, existUsername }; +export { + login, + signup, + userPersonalData, + userActivation, + existNickname, + existUsername, + loginByUsernamAnswers, +}; diff --git a/src/app/auth/authClient.js b/src/app/auth/authClient.js index 38370f3..5075788 100644 --- a/src/app/auth/authClient.js +++ b/src/app/auth/authClient.js @@ -4,4 +4,8 @@ const authClient = create({ baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users", }); -export default authClient; +const authChangePswdClient = create({ + baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/forgotpasswords" +}) + +export { authClient, authChangePswdClient}; diff --git a/src/app/screens/PasswordRecoveryScreen.js b/src/app/screens/PasswordRecoveryScreen.js index 084bba8..f508a15 100644 --- a/src/app/screens/PasswordRecoveryScreen.js +++ b/src/app/screens/PasswordRecoveryScreen.js @@ -1,46 +1,40 @@ -import React, { useState, useContext } from "react"; +import React, { useState } from "react"; import { StyleSheet, View, Text } from "react-native"; import * as Yup from "yup"; -import { - Form, - SubmitButton, - FormField, - ErrorMessage, -} from "../components/forms"; -import Screen from "../components/Screen"; +import { Form, SubmitButton, FormField } from "../components/forms"; import colors from "../config/colors"; import { dimensions } from "../config/dimensions"; -import assets from "../config/assets"; import PhoneNumberFormField from "../components/forms/PhoneNumberFormField"; import SearchablePicker from "../components/SearchablePicker"; import ConfirmationModal from "../components/ConfirmationModal"; +import {loginByUsernamAnswers} from "../api/auth"; function SecQuestionPicker({ name }) { const [items, setItems] = useState([ - { value: "Qual a sua cor predileta?", label: "Qual a sua cor predileta?" }, + { value: "1", label: "Qual a sua cor predileta?" }, { - value: "Qual é seu livro predileto?", + value: "2", label: "Qual é seu livro predileto?", }, { - value: "Qual o nome da rua em que você cresceu?", + value: "3", label: "Qual o nome da rua em que você cresceu?", }, { - value: "Qual o nome do seu bicho de estimação predileto?", + value: "4", label: "Qual o nome do seu bicho de estimação predileto?", }, { - value: "Qual a sua comida predileta?", + value: "5", label: "Qual a sua comida predileta?", }, { - value: "Qual é o seu país preferido?", + value: "7", label: "Qual é o seu país preferido?", }, { - value: "Qual é a sua marca de carro predileto?", + value: "8", label: "Qual é a sua marca de carro predileto?", }, ]); @@ -77,6 +71,37 @@ export default function PasswordRecovery({ navigation }) { message: "", }); + const handleSubmit = async (number, answer, secQuestion) => { + const apiResponse = await loginByUsernamAnswers(number, secQuestion, answer); + + switch (apiResponse.status) { + case 200: + console.log("ir pra proxima tela...."); + navigation.navigate("PasswordRecoveryChangePswd", { + authToken: apiResponse.data, + }); + break; + case 404: + setConfirmatioModalData({ + message: "Número de telefone inválido", + show: true, + }); + break; + case 422: + setConfirmatioModalData({ + message: "Pergunta de segurança ou senha incorretos", + show: true, + }); + break; + default: + setConfirmatioModalData({ + message: "Algo deu errado, tente novamente mais tarde", + show: true, + }); + break; + } + }; + return ( { - // FIXME: This should be replaced by the proper api call in future - const apiReturnTest = 200; - const apiReturnTokenTest = "API EXAMPLE TOKEN API"; - - switch (apiReturnTest) { - case 200: - console.log("ir pra proxima tela...."); - navigation.navigate("PasswordRecoveryChangePswd", { authToken : apiReturnTokenTest }) - break; - case 404: - setConfirmatioModalData({ - message: "Número de telefone inválido", - show: true, - }); - break; - case 422: - setConfirmatioModalData({ - message: "Pergunta de segurança ou senha incorretos", - show: true, - }); - break; - default: - setConfirmatioModalData({ - message: "Algo deu errado, tente novamente mais tarde", - show: true, - }); - break; - } + onSubmit={({number, answer, secQuestion}) => { + + handleSubmit(number, answer, secQuestion); }} >