Browse Source

User autentication first implementation

master
GabrielTrettel 3 years ago
parent
commit
b65101c78d
  1. 6
      src/app/api/auth.js
  2. 49
      src/app/screens/AccountScreen.js
  3. 4
      src/app/screens/ActivateInstitutionCode.js
  4. 29
      src/app/screens/ActivateInstitutionShowCode.js
  5. 1
      src/app/screens/LoginScreen.js

6
src/app/api/auth.js

@ -51,6 +51,7 @@ async function userPersonalData() {
baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users",
});
localClient.setHeader("Authorization", `Bearer ${authToken}`);
return localClient.get(`/me`);
}
@ -58,13 +59,16 @@ async function userPersonalData() {
async function userActivation(code) {
const { username } = await authStorage.getUser();
const authToken = await authStorage.getToken()
console.log("TOKEN ACTIVATE: "+authToken);
console.log("USER NAME: " + username);
const localClient = create({
baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users",
});
localClient.setHeader("Authorization", `Bearer ${authToken}`);
return localClient.post(`/activate?activationkey=${code}&username=${username}`);
return localClient.post(`/activate?username=${username}&activationkey=0b7b37bb-0f6d-4c65-b1b4-d7cc816f52bd`);
}
export { login, signup, userPersonalData, userActivation };

49
src/app/screens/AccountScreen.js

@ -9,7 +9,6 @@ import { MaterialCommunityIcons, FontAwesome } from "@expo/vector-icons";
import colors from "../config/colors";
import ConfirmationModal from "../components/ConfirmationModal";
function UserHeader({ name, fone }) {
return (
<View style={{ flexDirection: "row", alignItems: "center" }}>
@ -74,16 +73,32 @@ function ProfileItensList({ icon, IconProvider, title, onPress }) {
function AccountScreen(props) {
const { user, setUser } = useContext(AuthContext);
const [ showLog, setShowLog ] = useState(false);
const [showLog, setShowLog] = useState(false);
console.log(user);
const logout = () => {
setUser(null);
authStorage.removeToken();
};
const activationActions = () => {
if (user?.providerActivationKey)
props.navigation.navigate("ActivateInstitutionShowCode");
else
props.navigation.navigate("ActivateInstitutionCode");
};
const showActivation = () => {
if (user.role === "ROLE_CLIENT")
return !user.active
else
return true
}
const profileItems = [
{
icon: "account",
show: true,
IconProvider: MaterialCommunityIcons,
title: "Cadastrar pluviômetro",
onPress: () => {
@ -92,14 +107,16 @@ function AccountScreen(props) {
},
{
icon: "institution",
show: showActivation(),
IconProvider: FontAwesome,
title: "ATIVAR INSTITUIÇÃO",
onPress: () => {
props.navigation.navigate("ActivateInstitutionCode");
activationActions();
},
},
{
icon: "information-outline",
show: true,
IconProvider: MaterialCommunityIcons,
title: "SOBRE O PROJETO",
onPress: () => {
@ -108,10 +125,11 @@ function AccountScreen(props) {
},
{
icon: "logout",
show: true,
IconProvider: MaterialCommunityIcons,
title: "sair",
onPress: () => {
setShowLog(true)
setShowLog(true);
},
},
// {
@ -135,16 +153,19 @@ function AccountScreen(props) {
<UserHeader name={user.nickname} fone={user.username} image={"1"} />
<View style={{ marginTop: 24 }}>
{profileItems.map(({ icon, IconProvider, title, onPress }) => (
<View key={title}>
<ProfileItensList
icon={icon}
IconProvider={IconProvider}
title={title}
onPress={onPress}
/>
</View>
))}
{profileItems.map(
({ icon, IconProvider, title, onPress, show }) =>
show && (
<View key={title}>
<ProfileItensList
icon={icon}
IconProvider={IconProvider}
title={title}
onPress={onPress}
/>
</View>
)
)}
</View>
<ConfirmationModal
show={showLog}

4
src/app/screens/ActivateInstitutionCode.js

@ -126,6 +126,7 @@ function ValidateCode({ user }) {
async function processOnSubmit(navigation, form, instRole, setShowLog) {
const response = await userActivation(form.code);
console.log("==============================================================================")
console.log(response)
@ -136,7 +137,7 @@ async function processOnSubmit(navigation, form, instRole, setShowLog) {
setShowLog({ show: true, message: false });
}
} else if (response.ok) {
navigation.navigate("ActivateInstitutionShowCode", { code: response.data });
navigation.navigate("ActivateInstitutionShowCode");
} else {
setShowLog({ show: true, message: false });
}
@ -149,6 +150,7 @@ export default function ActivateInstitutionCode({ navigation }) {
const { user, _ } = useContext(AuthContext);
// FIXME: wpdAuth has errors while giving /me data for ROLE_INSTITUTION
user.role = "ROLE_CLIENT"
// console.log(user)
const initialState = { show: false, message: true };
const [showLog, setShowLog] = useState(initialState);

29
src/app/screens/ActivateInstitutionShowCode.js

@ -1,7 +1,10 @@
import React, { useState, useContext } from "react";
import React, { useState, useContext, useEffect } from "react";
import { View, Text } from "react-native";
import { dimensions } from "../config/dimensions";
import colors from "../config/colors";
import authStorage from "../auth/storage";
import { AuthContext } from "../auth/context";
import {userPersonalData} from "../api/auth";
function Header() {
return (
@ -26,10 +29,22 @@ function Header() {
);
}
export default function ActivateInstitutionShowCode() {
const [actCode, setActCode] = useState("");
const authContext = useContext(AuthContext);
useEffect(() => {
if (authContext.user?.providerActivationKey?.activationkey)
setActCode(authContext.user.providerActivationKey.activationkey);
else
userPersonalData().then((userData) => {
userData.ok && authContext.setUser(userData.data);
setActCode(userData.data.providerActivationKey.activationkey);
});
}, []);
export default function ActivateInstitutionShowCode({ route }) {
const { code } = route.params;
const [actCode, setActCode] = useState("123456789012");
return (
<View>
@ -52,9 +67,11 @@ export default function ActivateInstitutionShowCode({ route }) {
marginTop: 24,
fontWeight: "bold",
textAlign: "center",
fontSize: dimensions.text.header
fontSize: dimensions.text.header,
}}
>{actCode}</Text>
>
{actCode}
</Text>
</View>
);
}

1
src/app/screens/LoginScreen.js

@ -80,6 +80,7 @@ export default function LoginScreen(props) {
if (!result.ok) return setLoginFailed(true);
await authStorage.setToken(result.data);
// console.log("TOKEN: " + result.data);
setLoginFailed(false);
const user = await userPersonalData();

Loading…
Cancel
Save