Browse Source

User autentication first implementation

master
GabrielTrettel 3 years ago
parent
commit
b65101c78d
  1. 6
      src/app/api/auth.js
  2. 31
      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", baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users",
}); });
localClient.setHeader("Authorization", `Bearer ${authToken}`); localClient.setHeader("Authorization", `Bearer ${authToken}`);
return localClient.get(`/me`); return localClient.get(`/me`);
} }
@ -58,13 +59,16 @@ async function userPersonalData() {
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("USER NAME: " + username);
const localClient = create({ const localClient = create({
baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users", baseURL: "http://wpd.brazilsouth.cloudapp.azure.com:8080/users",
}); });
localClient.setHeader("Authorization", `Bearer ${authToken}`); 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 }; export { login, signup, userPersonalData, userActivation };

31
src/app/screens/AccountScreen.js

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

4
src/app/screens/ActivateInstitutionCode.js

@ -126,6 +126,7 @@ function ValidateCode({ user }) {
async function processOnSubmit(navigation, form, instRole, setShowLog) { async function processOnSubmit(navigation, form, instRole, setShowLog) {
const response = await userActivation(form.code); const response = await userActivation(form.code);
console.log("==============================================================================")
console.log(response) console.log(response)
@ -136,7 +137,7 @@ async function processOnSubmit(navigation, form, instRole, setShowLog) {
setShowLog({ show: true, message: false }); setShowLog({ show: true, message: false });
} }
} else if (response.ok) { } else if (response.ok) {
navigation.navigate("ActivateInstitutionShowCode", { code: response.data });
navigation.navigate("ActivateInstitutionShowCode");
} else { } else {
setShowLog({ show: true, message: false }); setShowLog({ show: true, message: false });
} }
@ -149,6 +150,7 @@ export default function ActivateInstitutionCode({ navigation }) {
const { user, _ } = useContext(AuthContext); const { user, _ } = useContext(AuthContext);
// FIXME: wpdAuth has errors while giving /me data for ROLE_INSTITUTION // FIXME: wpdAuth has errors while giving /me data for ROLE_INSTITUTION
user.role = "ROLE_CLIENT" user.role = "ROLE_CLIENT"
// console.log(user)
const initialState = { show: false, message: true }; const initialState = { show: false, message: true };
const [showLog, setShowLog] = useState(initialState); 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 { View, Text } from "react-native";
import { dimensions } from "../config/dimensions"; import { dimensions } from "../config/dimensions";
import colors from "../config/colors"; import colors from "../config/colors";
import authStorage from "../auth/storage";
import { AuthContext } from "../auth/context";
import {userPersonalData} from "../api/auth";
function Header() { function Header() {
return ( 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 ( return (
<View> <View>
@ -52,9 +67,11 @@ export default function ActivateInstitutionShowCode({ route }) {
marginTop: 24, marginTop: 24,
fontWeight: "bold", fontWeight: "bold",
textAlign: "center", textAlign: "center",
fontSize: dimensions.text.header
fontSize: dimensions.text.header,
}} }}
>{actCode}</Text>
>
{actCode}
</Text>
</View> </View>
); );
} }

1
src/app/screens/LoginScreen.js

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

Loading…
Cancel
Save