Browse Source

Starting to conncect change password to api calls

master
GabrielTrettel 3 years ago
parent
commit
7a4485b3a4
  1. 37
      src/app/api/auth.js
  2. 6
      src/app/auth/authClient.js
  3. 79
      src/app/screens/PasswordRecoveryScreen.js

37
src/app/api/auth.js

@ -1,5 +1,5 @@
import { create } from "apisauce"; import { create } from "apisauce";
import authClient from "../auth/authClient";
import { authClient, authChangePswdClient } from "../auth/authClient";
import authStorage from "../auth/storage"; import authStorage from "../auth/storage";
function login(name, password) { function login(name, password) {
@ -44,9 +44,8 @@ function signup({
return authClient.post(`/signup`, body); return authClient.post(`/signup`, body);
} }
async function userPersonalData() { async function userPersonalData() {
const authToken = await authStorage.getToken()
const authToken = await authStorage.getToken();
const localClient = create({ const localClient = create({
baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users", baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users",
}); });
@ -55,10 +54,9 @@ async function userPersonalData() {
return localClient.get(`/me`); return localClient.get(`/me`);
} }
async function userActivation(code) { async function userActivation(code) {
const { username } = await authStorage.getUser(); const { username } = await authStorage.getUser();
const authToken = await authStorage.getToken()
const authToken = await authStorage.getToken();
// console.log("TOKEN ACTIVATE: "+authToken); // console.log("TOKEN ACTIVATE: "+authToken);
// console.log("USER NAME: " + username); // console.log("USER NAME: " + username);
@ -68,7 +66,9 @@ async function userActivation(code) {
localClient.setHeader("Authorization", `Bearer ${authToken}`); 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) { async function existUsername(username) {
@ -79,5 +79,28 @@ async function existNickname(nickname) {
return authClient.post(`/existsByNickname?nickname=${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,
};

6
src/app/auth/authClient.js

@ -4,4 +4,8 @@ const authClient = create({
baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users", 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};

79
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 { StyleSheet, View, Text } from "react-native";
import * as Yup from "yup"; 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 colors from "../config/colors";
import { dimensions } from "../config/dimensions"; import { dimensions } from "../config/dimensions";
import assets from "../config/assets";
import PhoneNumberFormField from "../components/forms/PhoneNumberFormField"; import PhoneNumberFormField from "../components/forms/PhoneNumberFormField";
import SearchablePicker from "../components/SearchablePicker"; import SearchablePicker from "../components/SearchablePicker";
import ConfirmationModal from "../components/ConfirmationModal"; import ConfirmationModal from "../components/ConfirmationModal";
import {loginByUsernamAnswers} from "../api/auth";
function SecQuestionPicker({ name }) { function SecQuestionPicker({ name }) {
const [items, setItems] = useState([ 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?", 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?", 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?", 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?", label: "Qual a sua comida predileta?",
}, },
{ {
value: "Qual é o seu país preferido?",
value: "7",
label: "Qual é o seu país preferido?", 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?", label: "Qual é a sua marca de carro predileto?",
}, },
]); ]);
@ -77,33 +71,15 @@ export default function PasswordRecovery({ navigation }) {
message: "", message: "",
}); });
return (
<View>
<ConfirmationModal
show={confirmatioModalData.show}
description={confirmatioModalData.message}
confirmationLabel="OK"
onConfirm={() =>
setConfirmatioModalData({ ...confirmatioModalData, show: false })
}
/>
const handleSubmit = async (number, answer, secQuestion) => {
const apiResponse = await loginByUsernamAnswers(number, secQuestion, answer);
<Form
validationSchema={validationSchema}
initialValues={{
number: "",
answer: "",
secQuestion: "",
}}
onSubmit={() => {
// FIXME: This should be replaced by the proper api call in future
const apiReturnTest = 200;
const apiReturnTokenTest = "API EXAMPLE TOKEN API";
switch (apiReturnTest) {
switch (apiResponse.status) {
case 200: case 200:
console.log("ir pra proxima tela...."); console.log("ir pra proxima tela....");
navigation.navigate("PasswordRecoveryChangePswd", { authToken : apiReturnTokenTest })
navigation.navigate("PasswordRecoveryChangePswd", {
authToken: apiResponse.data,
});
break; break;
case 404: case 404:
setConfirmatioModalData({ setConfirmatioModalData({
@ -124,6 +100,29 @@ export default function PasswordRecovery({ navigation }) {
}); });
break; break;
} }
};
return (
<View>
<ConfirmationModal
show={confirmatioModalData.show}
description={confirmatioModalData.message}
confirmationLabel="OK"
onConfirm={() =>
setConfirmatioModalData({ ...confirmatioModalData, show: false })
}
/>
<Form
validationSchema={validationSchema}
initialValues={{
number: "",
answer: "",
secQuestion: "",
}}
onSubmit={({number, answer, secQuestion}) => {
handleSubmit(number, answer, secQuestion);
}} }}
> >
<View style={{ padding: 16 }}> <View style={{ padding: 16 }}>

Loading…
Cancel
Save