Skip to content

Commit

Permalink
Merge pull request #15 from jaydenchee97/feature
Browse files Browse the repository at this point in the history
Feature
  • Loading branch information
jaydenchee97 authored Aug 11, 2024
2 parents ce37de4 + 41a1c33 commit cceff70
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
30 changes: 25 additions & 5 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import { Authenticator } from "@aws-amplify/ui-react-native";
import { NavigationContainer } from "@react-navigation/native";
import { Amplify } from "aws-amplify";
import { Amplify, Auth } from "aws-amplify";
import { PaperProvider } from "react-native-paper";
import { SafeAreaProvider } from "react-native-safe-area-context";

import awsExports from "./src/aws-exports";
import HomeStack from "./src/navigation/HomeStack";
import { useEffect, useState } from "react";
import AppStack from "./src/navigation/AppStack";

Amplify.configure(awsExports);

// Amplify configuration
Amplify.configure({
...awsExports,
Auth: {
region: process.env.EXPO_PUBLIC_AMPLIFY_REGION,
userPoolId: process.env.EXPO_PUBLIC_COGNITO_USER_POOL_ID,
userPoolWebClientId: process.env.EXPO_PUBLIC_COGNITO_USER_POOL_WEBCLIENT_ID,
authenticationFlowType: process.env.EXPO_PUBLIC_AUTHENTICATION_FLOW_TYPE,
oauth: {
domain: process.env.EXPO_PUBLIC_OAUTH_DOMAIN,
redirectSignIn: process.env.EXPO_PUBLIC_OAUTH_REDIRECT_SIGN_IN,
redirectSignOut: process.env.EXPO_PUBLIC_OAUTH_REDIRECT_SIGN_OUT,
responseType: process.env.EXPO_PUBLIC_OAUTH_RESPONSE_TYPE
}
},

});
export default function App() {

return (
<PaperProvider>
<SafeAreaProvider>
<NavigationContainer>
<Authenticator.Provider>
<Authenticator>
<HomeStack />
</Authenticator>
{/* <HomeStack /> */}
<AppStack />
</Authenticator.Provider>
{/* <AppStack /> */}

</NavigationContainer>
</SafeAreaProvider>
</PaperProvider>
Expand Down
39 changes: 39 additions & 0 deletions src/navigation/AppStack.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createStackNavigator } from '@react-navigation/stack';
import { useEffect, useState } from 'react';
import { Auth } from 'aws-amplify';
import HomeStack from './HomeStack';


const Stack = createStackNavigator();

export default function AppStack() {
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);
const handleAuth = async () => {
try {

const user = await Auth.currentAuthenticatedUser();
console.log('User is already authenticated:', user);
setIsAuthenticated(true);
} catch {
console.log('User is not authenticated, redirecting to login');
await Auth.federatedSignIn();
}
};
useEffect(() => {
handleAuth();
}, []);

if (isAuthenticated === null) {
return null; // Show loading spinner or splash screen
}

return (
<Stack.Navigator>
<Stack.Screen
name="HomeStack"
component={HomeStack}
options={{ headerShown: false }}
/>
</Stack.Navigator>
);
}
3 changes: 3 additions & 0 deletions src/navigation/HomeStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default function HomeStack() {
bypassCache: true,
});

console.log("authUser");
console.log(authUser);

const userData = await API.graphql(
graphqlOperation(getUser, { id: authUser.attributes.sub }),
);
Expand Down
12 changes: 10 additions & 2 deletions src/screens/Account.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { useAuthenticator } from "@aws-amplify/ui-react-native";
import { Auth } from "aws-amplify";
import { View, StyleSheet } from "react-native";
import { Divider, List, Text } from "react-native-paper";

export default function Account({ navigation }) {
const { signOut } = useAuthenticator((context) => [context.user]);

const handleSignOut = async () => {
try {
await Auth.signOut({ global: true });
} catch (error) {
console.log('Error signing out: ', error);
}
};

return (
<View style={styles.view}>
Expand Down Expand Up @@ -44,7 +52,7 @@ export default function Account({ navigation }) {
<List.Item
title={<Text> Sign out </Text>}
left={() => <List.Icon icon="logout" />}
onPress={signOut}
onPress={handleSignOut}
style={{ alignSelf: "center" }}
/>
<Divider />
Expand Down
1 change: 0 additions & 1 deletion src/screens/Welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ export default function Welcome({ props }) {
// fetch all Listings
useEffect(() => {
if (isFocused) {
console.log("call again");
getSavedAccommodations();
fetch();
}
Expand Down

0 comments on commit cceff70

Please sign in to comment.