From c7fe504666fdf5fe522133431129a0618644bb5e Mon Sep 17 00:00:00 2001 From: Jakub Satora Date: Tue, 29 Oct 2024 18:08:10 +0100 Subject: [PATCH] final steps --- app/(root)/(tabs)/rides.tsx | 63 ++++++++++++++++++++---- app/_layout.tsx | 3 +- components/OAuth.tsx | 25 ++++++++-- lib/auth.ts | 96 +++++++++++++++++++++++++++---------- 4 files changed, 145 insertions(+), 42 deletions(-) diff --git a/app/(root)/(tabs)/rides.tsx b/app/(root)/(tabs)/rides.tsx index 773eec6..50b4968 100644 --- a/app/(root)/(tabs)/rides.tsx +++ b/app/(root)/(tabs)/rides.tsx @@ -1,14 +1,57 @@ -import { View, Text } from "react-native" -import { SafeAreaView } from "react-native-safe-area-context" +import { useUser } from "@clerk/clerk-expo"; +import { ActivityIndicator, FlatList, Image, Text, View } from "react-native"; +import { SafeAreaView } from "react-native-safe-area-context"; + +import RideCard from "@/components/RideCard"; +import { images } from "@/constants"; +import { useFetch } from "@/lib/fetch"; +import { Ride } from "@/types/type"; const Rides = () => { - return( - - - Rides - - - ) -} + const { user } = useUser(); + + const { + data: recentRides, + loading, + error, + } = useFetch(`/(api)/ride/${user?.id}`); + + return ( + + } + keyExtractor={(item, index) => index.toString()} + className="px-5" + keyboardShouldPersistTaps="handled" + contentContainerStyle={{ + paddingBottom: 100, + }} + ListEmptyComponent={() => ( + + {!loading ? ( + <> + No recent rides found + No recent rides found + + ) : ( + + )} + + )} + ListHeaderComponent={ + <> + All Rides + + } + /> + + ); +}; export default Rides; \ No newline at end of file diff --git a/app/_layout.tsx b/app/_layout.tsx index 1536099..b5993a2 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -8,7 +8,8 @@ import { ClerkProvider, ClerkLoaded } from '@clerk/clerk-expo' import { Slot } from 'expo-router' import { tokenCache } from '@/lib/auth'; import 'react-native-get-random-values'; - +import{LogBox}from"react-native" +LogBox.ignoreLogs(["Warning:..."]) const publishableKey = process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY! diff --git a/components/OAuth.tsx b/components/OAuth.tsx index 173e5e1..ce97abb 100644 --- a/components/OAuth.tsx +++ b/components/OAuth.tsx @@ -1,12 +1,27 @@ -import { View, Text, Image } from "react-native" +import { View, Text, Image, Alert } from "react-native" import CustomButton from "./CustomButton" import { icons } from "@/constants" +import { useOAuth } from "@clerk/clerk-expo" +import { useCallback } from "react" +import { googleOAuth } from "@/lib/auth" +import { router } from "expo-router" const OAuth = () => { - const handleGoogleSignIn = async () => { - - } - return( + const { startOAuthFlow } = useOAuth({ strategy: "oauth_google" }) + const handleGoogleSignIn = useCallback(async () => { + try { + const result = await googleOAuth(startOAuthFlow) + if (result.code === "session_exists"||result.code==="success") { + Alert.alert("Success", "Session Exist. Redirect to home page") + router.push("/(root)/(tabs)/home") + } + Alert.alert(result.success ? "Sucess" : "Error", result.message) + } + catch (err) { + console.error("OAuth error", err) + } + }, []) + return ( diff --git a/lib/auth.ts b/lib/auth.ts index 246ecd2..6e63c5d 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -1,33 +1,77 @@ import * as SecureStore from 'expo-secure-store' +import * as Linking from "expo-linking" +import { fetchAPI } from './fetch' + export interface TokenCache { - getToken: (key: string) => Promise - saveToken: (key: string, token: string) => Promise - clearToken?: (key: string) => void - } + getToken: (key: string) => Promise + saveToken: (key: string, token: string) => Promise + clearToken?: (key: string) => void +} - export const tokenCache = { - async getToken(key: string) { - try { - const item = await SecureStore.getItemAsync(key) - if (item) { - console.log(`${key} was used 🔐 \n`) - } else { - console.log('No values stored under key: ' + key) - } - return item - } catch (error) { - console.error('SecureStore get item error: ', error) - await SecureStore.deleteItemAsync(key) - return null +export const tokenCache = { + async getToken(key: string) { + try { + const item = await SecureStore.getItemAsync(key) + if (item) { + console.log(`${key} was used 🔐 \n`) + } else { + console.log('No values stored under key: ' + key) } - }, - async saveToken(key: string, value: string) { - try { - return SecureStore.setItemAsync(key, value) - } catch (err) { - return + return item + } catch (error) { + console.error('SecureStore get item error: ', error) + await SecureStore.deleteItemAsync(key) + return null + } + }, + async saveToken(key: string, value: string) { + try { + return SecureStore.setItemAsync(key, value) + } catch (err) { + return + } + }, +} +export const googleOAuth = async (startOAuthFlow: any) => { + try { + const { createdSessionId, signIn, signUp, setActive } = await startOAuthFlow({ + redirectUrl: Linking.createURL("/(root)/(tabs)/home") + }) + if (createdSessionId) { + if (setActive) { + await setActive!({ + session: createdSessionId + }) + if (signUp.createdSessionId) { + await fetchAPI("/(api)/user", + { + method: "POST", + body: JSON.stringify({ + name: `${signUp.firstName} ${signUp.lastName}`, + email: signUp.email, + clerkId: signUp.createdUserId + }) + }) + } + return { + success: true, + code: "success", + message: "You have successfully authenticated" + } } - }, - } \ No newline at end of file + + } + return { + success: false, + message: "An error occurred" + } + + } catch (error: any) { + return { + success: false, + message: error?.errors[0]?.longMessage + } + } +} \ No newline at end of file