Browse Source

Adjusting login/register to API

master
GabrielTrettel 3 years ago
parent
commit
411606e710
  1. 11
      src/app/api/auth.js
  2. 13
      src/app/screens/LoginScreen.js
  3. 34
      src/app/screens/RegisterScreen.js

11
src/app/api/auth.js

@ -35,11 +35,18 @@ function signup({
}; };
Object.entries(body).forEach(([key, value]) => { Object.entries(body).forEach(([key, value]) => {
value == "" && delete body[key]
value == "" && delete body[key];
}); });
return authClient.post(`/signup`, body); return authClient.post(`/signup`, body);
}
function userPersonalData(token) {
const localClient = create({
baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users",
});
localClient.setHeader("Authorization", `Bearer ${token}`);
return localClient.get(`/me`);
} }
export { login, signup };
export { login, signup, userPersonalData };

13
src/app/screens/LoginScreen.js

@ -11,14 +11,12 @@ import Screen from "../components/Screen";
import colors from "../config/colors"; import colors from "../config/colors";
import { dimensions } from "../config/dimensions"; import { dimensions } from "../config/dimensions";
import jwdDecode from "jwt-decode";
import { AuthContext } from "../auth/context"; import { AuthContext } from "../auth/context";
import authStorage from "../auth/storage"; import authStorage from "../auth/storage";
import assets from "../config/assets"; import assets from "../config/assets";
import Button from "../components/Button"; import Button from "../components/Button";
import { TouchableNativeFeedback } from "react-native-gesture-handler"; import { TouchableNativeFeedback } from "react-native-gesture-handler";
// import authApi from '../api/auth';
import login from "../api/auth";
import {login, userPersonalData} from "../api/auth";
const phoneRegex = RegExp( const phoneRegex = RegExp(
/^\(?[\(]?([0-9]{2})?\)?[)\b]?([0-9]{4,5})[-. ]?([0-9]{4})$/ /^\(?[\(]?([0-9]{2})?\)?[)\b]?([0-9]{4,5})[-. ]?([0-9]{4})$/
@ -79,15 +77,14 @@ export default function LoginScreen(props) {
const handleSubmit = async (name, password, setLoginFailed) => { const handleSubmit = async (name, password, setLoginFailed) => {
const result = await login(name, password); const result = await login(name, password);
console.log(JSON.stringify(result));
if (!result.ok) return setLoginFailed(true); if (!result.ok) return setLoginFailed(true);
authStorage.setToken(result.data);
setLoginFailed(false); setLoginFailed(false);
const user = jwdDecode(result.data);
authContext.setUser(user);
console.log("usuário: " + JSON.sstringify(user));
authStorage.setToken(result.data);
const user = await userPersonalData(result.data);
user.ok && authContext.setUser(user.data);
}; };
const [loginFailed, setLoginFailed] = useState(false); const [loginFailed, setLoginFailed] = useState(false);

34
src/app/screens/RegisterScreen.js

@ -4,7 +4,7 @@ import {
FormField, FormField,
ErrorMessage, ErrorMessage,
} from "../components/forms"; } from "../components/forms";
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useContext } from "react";
import { StyleSheet, View, Text, TouchableNativeFeedback } from "react-native"; import { StyleSheet, View, Text, TouchableNativeFeedback } from "react-native";
import Screen from "../components/Screen"; import Screen from "../components/Screen";
import { dimensions } from "../config/dimensions"; import { dimensions } from "../config/dimensions";
@ -19,7 +19,10 @@ import SearchablePicker from "../components/SearchablePicker";
import { states, statesToCities } from "../assets/cities_states"; import { states, statesToCities } from "../assets/cities_states";
import { useFormikContext } from "formik"; import { useFormikContext } from "formik";
import Checkbox from "../components/forms/CheckBox"; import Checkbox from "../components/forms/CheckBox";
import {signup} from "../api/auth";
import {signup, login, userPersonalData} from "../api/auth";
import { AuthContext } from "../auth/context";
import authStorage from "../auth/storage";
import jwdDecode from "jwt-decode";
const phoneRegex = RegExp( const phoneRegex = RegExp(
/^\(?[\(]?([0-9]{2})?\)?[)\b]?([0-9]{4,5})[-. ]?([0-9]{4})$/ /^\(?[\(]?([0-9]{2})?\)?[)\b]?([0-9]{4,5})[-. ]?([0-9]{4})$/
@ -227,6 +230,7 @@ function MaterialCommunityIconsCustom({
); );
} }
export default function RegisterScreen(props) { export default function RegisterScreen(props) {
const authContext = useContext(AuthContext);
const _moment = moment(); const _moment = moment();
const [date, setDate] = useState(_moment); const [date, setDate] = useState(_moment);
const [singUpFailed, setSingUpFailed] = useState(false); const [singUpFailed, setSingUpFailed] = useState(false);
@ -239,9 +243,31 @@ export default function RegisterScreen(props) {
} }
}; };
const automaticLogin = async (form) => {
const result = await login(form.number, form.password);
if (!result.ok) return;
authStorage.setToken(result.data);
const user = await userPersonalData(result.data);
user.ok && authContext.setUser(user.data);
}
const handleSubmit = async (form) => { const handleSubmit = async (form) => {
const result = await signup({...form, dateofborn: date.format("DD/MM/yyyy")});
console.log(result);
const formDate = date.format("DD/MM/yyyy") === moment().format("DD/MM/yyyy") ? "" : date;
const result = await signup({...form, dateofborn: formDate})
switch (result.status) {
case 200:
automaticLogin(form)
break;
case 422:
// setError()
default:
}
}; };
return ( return (

Loading…
Cancel
Save