Skip to content

Commit 11331fa

Browse files
authored
Merge pull request #1 from bagusok/auth-test
Add: Google auth (beta)
2 parents 74c7d59 + 59af1fb commit 11331fa

File tree

13 files changed

+362
-56
lines changed

13 files changed

+362
-56
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ expo-env.d.ts
2121

2222
.env
2323
android
24-
android/*
24+
android/*
25+
keykey/*

app.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
{
3737
"initialOrientation": "DEFAULT"
3838
}
39+
],
40+
[
41+
"@react-native-google-signin/google-signin",
42+
{
43+
"iosUrlScheme": "com.googleusercontent.apps._some_id_here_"
44+
}
3945
]
4046
],
4147
"experiments": {

app/(home)/_layout.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Tabs } from "expo-router";
22
import React from "react";
33

4-
import { TabBarIcon } from "@/components/navigation/TabBarIcon";
54
import { Colors } from "@/constants/Colors";
65
import { useColors } from "@/hooks/useColors";
76
import { AntDesign, Feather } from "@expo/vector-icons";

app/(home)/all-anime.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { API_URL } from "@/constants/Strings";
77
import { useColors } from "@/hooks/useColors";
88
import { axiosIn } from "@/utils/axios";
99

10-
import { useInfiniteQuery, useQuery } from "@tanstack/react-query";
10+
import { useInfiniteQuery } from "@tanstack/react-query";
1111
import { useMemo } from "react";
12-
import { ActivityIndicator, Pressable, StyleSheet, View } from "react-native";
12+
import { ActivityIndicator, StyleSheet, View } from "react-native";
1313
import { FlatList } from "react-native-gesture-handler";
1414
import { SafeAreaView } from "react-native-safe-area-context";
1515

app/(home)/user.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
import GoogleLogin from "@/components/auth/GoogleLogin";
12
import SafeAreaWrapper from "@/components/SafeAreaWrapper";
23
import { CustomText } from "@/components/ui";
4+
import { tokenAtom } from "@/store/auth";
5+
import { GoogleSignin } from "@react-native-google-signin/google-signin";
6+
import { useAtom, useAtomValue } from "jotai";
7+
import { Alert } from "react-native";
8+
import { TouchableOpacity } from "react-native-gesture-handler";
39

410
export default function AllAnime() {
11+
const [token, setToken] = useAtom(tokenAtom);
12+
513
return (
614
<SafeAreaWrapper>
715
<CustomText>All Anime</CustomText>
16+
17+
{!token ? <GoogleLogin /> : <CustomText>Logged in</CustomText>}
18+
<TouchableOpacity
19+
onPress={async () => {
20+
await GoogleSignin.signOut();
21+
22+
setToken(null);
23+
Alert.alert("Sign out success!");
24+
}}
25+
>
26+
<CustomText>Sign Out</CustomText>
27+
</TouchableOpacity>
828
</SafeAreaWrapper>
929
);
1030
}

app/_layout.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
1515

1616
import { GestureHandlerRootView } from "react-native-gesture-handler";
1717
import { BottomSheetModalProvider } from "@gorhom/bottom-sheet";
18+
import { GoogleSignin } from "@react-native-google-signin/google-signin";
1819

19-
// Prevent the splash screen from auto-hiding before asset loading is complete.
2020
SplashScreen.preventAutoHideAsync();
2121
ScreenOrientation.unlockAsync();
2222

2323
const queryClient = new QueryClient();
2424

2525
export default function RootLayout() {
26+
GoogleSignin.configure({
27+
// scopes: ["https://www.googleapis.com/auth/drive.readonly"],
28+
webClientId: process.env.EXPO_PUBLIC_GOOGLE_WEB_CLIENT_ID,
29+
});
30+
2631
const [loaded, error] = useFonts({
2732
Poppins_400Regular,
2833
Poppins_600SemiBold,

components/GlobalLoading.tsx

Lines changed: 0 additions & 34 deletions
This file was deleted.

components/auth/GoogleLogin.tsx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { View } from "react-native";
2+
import { API_URL } from "@/constants/Strings";
3+
import { axiosIn } from "@/utils/axios";
4+
import { supabase } from "@/utils/supabase";
5+
import {
6+
GoogleSignin,
7+
GoogleSigninButton,
8+
statusCodes,
9+
} from "@react-native-google-signin/google-signin";
10+
import { Alert } from "react-native";
11+
import { useSetAtom } from "jotai";
12+
import { tokenAtom } from "@/store/auth";
13+
14+
export default function GoogleLogin() {
15+
const setToken = useSetAtom(tokenAtom);
16+
17+
return (
18+
<View>
19+
<GoogleSigninButton
20+
size={GoogleSigninButton.Size.Wide}
21+
color={GoogleSigninButton.Color.Dark}
22+
onPress={async () => {
23+
try {
24+
await GoogleSignin.hasPlayServices();
25+
const userInfo = await GoogleSignin.signIn();
26+
// console.log(userInfo);
27+
28+
if (userInfo.idToken) {
29+
const { data, error } = await supabase.auth.signInWithIdToken({
30+
provider: "google",
31+
token: userInfo.idToken,
32+
});
33+
34+
if (error) {
35+
console.log("ERROR SUPABASE LOGIN: ", error);
36+
Alert.alert("Error logging in with Supabase!");
37+
} else {
38+
const getToken = await axiosIn.post(
39+
`${API_URL}/anime/auth/login`,
40+
{
41+
email: data.user.email,
42+
},
43+
{
44+
headers: {
45+
Authorization: `Bearer ${data.session.access_token}`,
46+
},
47+
}
48+
);
49+
50+
if (getToken.data && getToken.data.status == true) {
51+
console.log("TOKEN: ", getToken.data);
52+
setToken(getToken.data.data.token);
53+
Alert.alert("Token found!");
54+
} else {
55+
console.log("NO TOKEN: ", getToken.data);
56+
Alert.alert("No token found!");
57+
}
58+
}
59+
} else {
60+
throw new Error("no ID token present!");
61+
}
62+
} catch (error: any) {
63+
Alert.alert(error.toString());
64+
console.log(error);
65+
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
66+
// user cancelled the login flow
67+
} else if (error.code === statusCodes.IN_PROGRESS) {
68+
// operation (e.g. sign in) is in progress already
69+
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
70+
// play services not available or outdated
71+
} else {
72+
// some other error happened
73+
}
74+
}
75+
}}
76+
/>
77+
</View>
78+
);
79+
}

0 commit comments

Comments
 (0)