Login and logout

This commit is contained in:
aeroreyna
2022-03-12 22:44:47 -08:00
parent 1abf26ce60
commit 082abd8fbe
7 changed files with 67 additions and 23 deletions

1
App.js
View File

@@ -84,6 +84,7 @@ export default function App() {
}} }}
/> />
<Stack.Screen name="SinglePost" component={SinglePost} /> <Stack.Screen name="SinglePost" component={SinglePost} />
<Stack.Screen name="Login" component={Login} options={{ headerShown: false }} />
</Stack.Navigator> </Stack.Navigator>
</NavigationContainer> </NavigationContainer>
</PaperProvider> </PaperProvider>

View File

@@ -5,12 +5,35 @@ import API from './../API.js';
import Post from './../components/Post.js'; import Post from './../components/Post.js';
import NewPost from "./../components/NewPost.js"; import NewPost from "./../components/NewPost.js";
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeFeed = async (value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('feed', jsonValue)
} catch (e) {
}
}
const getFeed = async () => {
try {
const value = await AsyncStorage.getItem('feed')
if (value !== null) {
return JSON.parse(value);
}
} catch (e) {
return []
}
}
let Feed = ({ navigation, route }) => { let Feed = ({ navigation, route }) => {
let [Me, setMeProfile] = useState({}); let [Me, setMeProfile] = useState({});
let [Posts, setPosts] = useState([]); let [Posts, setPosts] = useState([]);
useEffect(async () => { useEffect(async () => {
let loggedIn = await API.isLoggedIn();
if(!loggedIn) return navigation.navigate('Login');
let cacheFeed = await getFeed() || [];
setPosts(cacheFeed);
let r = await API.getMe(); let r = await API.getMe();
setMeProfile(r); setMeProfile(r);
if (route.params && route.params.profileid) { if (route.params && route.params.profileid) {
@@ -18,9 +41,8 @@ let Feed = ({ navigation, route }) => {
} else { } else {
let posts = await API.getPosts(); let posts = await API.getPosts();
setPosts(posts); setPosts(posts);
navigation.setOptions({ title: "Feed" }); storeFeed(posts);
} }
//console.log(posts)
}, [route.params]); }, [route.params]);
const renderPost = (({ item }) => { const renderPost = (({ item }) => {
if (item.nonOrganicType === 'PopularUsers' || item.nonOrganicType === 'PopularGroups') if (item.nonOrganicType === 'PopularUsers' || item.nonOrganicType === 'PopularGroups')

View File

@@ -3,35 +3,26 @@ import React, { useEffect, useState } from 'react';
import { StyleSheet, Text, View, TextInput, SafeAreaView } from 'react-native'; import { StyleSheet, Text, View, TextInput, SafeAreaView } from 'react-native';
import API from './../API.js'; import API from './../API.js';
import LoginForm from './../components/Login.js'; import LoginForm from './../components/Login.js';
import { Provider as PaperProvider } from 'react-native-paper';
import AwesomeIcon from 'react-native-vector-icons/FontAwesome';
export default function App({navigation, route}) { export default function App({navigation, route}) {
useEffect(async () => { useEffect(async () => {
let r = await API.isLoggedIn(); let r = await API.isLoggedIn();
if(r) navigation.navigate('Feed') if(r){
await API.logout();
navigation.navigate('Login')
}
}, []); }, []);
return ( return (
<PaperProvider settings={{
icon: props => <AwesomeIcon {...props} />,
}}>
<SafeAreaView style={styles.container}> <SafeAreaView style={styles.container}>
<Text>EMI Social LOGO</Text>
<LoginForm /> <LoginForm />
<StatusBar style="auto" /> <StatusBar style="auto" />
</SafeAreaView> </SafeAreaView>
</PaperProvider>
); );
} }
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
flex: 1, flex: 1,
alignItems: 'center',
justifyContent: 'center',
marginTop: 25,
paddingTop: 10,
backgroundColor: "#edf2f7"
}, },
}); });

View File

@@ -58,7 +58,6 @@ export default Profile;
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
flex: 1, flex: 1,
alignItems: 'center',
backgroundColor: "#edf2f7", backgroundColor: "#edf2f7",
}, },
}); });

View File

@@ -2,11 +2,13 @@ import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { Text, View, TextInput, Button, StyleSheet } from 'react-native'; import { Text, View, TextInput, Button, StyleSheet } from 'react-native';
import API from './../API.js'; import API from './../API.js';
import { useNavigation } from '@react-navigation/native';
let LoginForm = ()=>{ let LoginForm = ()=>{
let [email, setEmail] = useState(''); let [email, setEmail] = useState('');
let [password, setPassword] = useState(''); let [password, setPassword] = useState('');
const navigation = useNavigation();
return ( return (
<View style={styles.mainView}> <View style={styles.mainView}>
@@ -34,7 +36,7 @@ let LoginForm = ()=>{
<Button <Button
onPress={async () => { onPress={async () => {
let r = await API.logIn(email, password); let r = await API.logIn(email, password);
console.log(r); if(r.status === "ok") navigation.navigate('Feed');
}} }}
title={"login"} title={"login"}
/> />
@@ -50,7 +52,6 @@ const styles = StyleSheet.create ({
flexDirection:'column', flexDirection:'column',
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
backgroundColor: 'rgba(255, 0, 255, 1.0)',
}, },
input: { input: {
height: 40, height: 40,

View File

@@ -3,18 +3,47 @@ import { Text, View, ScrollView, Button, StyleSheet } from 'react-native';
import API from './../API.js'; import API from './../API.js';
import { useNavigation } from '@react-navigation/native'; import { useNavigation } from '@react-navigation/native';
import AsyncStorage from '@react-native-async-storage/async-storage';
let UserName = (props) => { const storeName = async (key, value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('Name_'+key, jsonValue)
} catch (e) {
}
}
const getName = async (key) => {
try {
const value = await AsyncStorage.getItem('Name_'+key)
if (value !== null) {
return JSON.parse(value);
}
} catch (e) {
return []
}
}
let UserName = ({profileid}) => {
let [profile, setProfile] = useState({}); let [profile, setProfile] = useState({});
const navigation = useNavigation(); const navigation = useNavigation();
useEffect(async () => { useEffect(async () => {
let p = await API.getUserProfile(props.profileid).catch(()=>{return {}}); let cacheProfile = await getName(profileid);
if(cacheProfile && cacheProfile.profile) setProfile(cacheProfile);
let p = await API.getUserProfile(profileid).catch(() => { return {} });
setProfile(p); setProfile(p);
}, [props.profileid]); storeName(profileid, p)
}, [profileid]);
const onPress = ()=>{
return navigation.navigate('Profile', { profileid })
}
return ( return (
<Text onPress={()=>{navigation.navigate('Profile', {profileid: props.profileid})}} >{profile.profile && profile.profile.firstName} {profile.profile && profile.profile.lastName}</Text> <Text onPress={onPress}>
{profile.profile && profile.profile.firstName} {profile.profile && profile.profile.lastName}
</Text>
); );
} }

View File

@@ -26,7 +26,8 @@
"react-native-screens": "^3.13.1", "react-native-screens": "^3.13.1",
"react-native-vector-icons": "^9.1.0", "react-native-vector-icons": "^9.1.0",
"react-native-web": "0.17.1", "react-native-web": "0.17.1",
"react-native-webview": "^11.17.2" "react-native-webview": "^11.17.2",
"@react-native-async-storage/async-storage": "~1.15.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.12.9" "@babel/core": "^7.12.9"