diff --git a/.gitignore b/.gitignore index 7d0383c..7fb1ece 100644 --- a/.gitignore +++ b/.gitignore @@ -77,4 +77,5 @@ expo-env.d.ts # erm *google-services.json -keys.txt \ No newline at end of file +keys.txt +android/ \ No newline at end of file diff --git a/app.json b/app.json index 59a1baf..575d0c5 100644 --- a/app.json +++ b/app.json @@ -27,7 +27,8 @@ "foregroundImage": "./assets/images/adaptive-icon.png", "backgroundColor": "#ffffff" }, - "package": "dev.srizan.featheroom" + "package": "dev.srizan.featheroom", + "googleServicesFile": "./lib/google-services.json" }, "web": { "bundler": "metro", diff --git a/app/(auth)/_layout.tsx b/app/(auth)/_layout.tsx index a7f5560..24be3e1 100644 --- a/app/(auth)/_layout.tsx +++ b/app/(auth)/_layout.tsx @@ -1,17 +1,28 @@ -import { Stack } from 'expo-router' +import { View } from 'react-native' +import { Button, Text } from 'react-native-paper' -import { StackHeader } from '@/lib/ui' +import { useAuth } from '@/lib/providers/auth' -const Layout = () => ( - , - }} - > - - - -) +function Layout() { + const { signIn, user } = useAuth() + + return ( + + + this is {JSON.stringify(user)}. + + /* ( + + ), + }} + > + + + */ + ) +} export default Layout diff --git a/app/_layout.tsx b/app/_layout.tsx index fe335f5..4047f8f 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -12,6 +12,7 @@ import { PaperProvider } from 'react-native-paper' import { Setting } from '@/lib/types' import { StackHeader, Themes } from '@/lib/ui' +import { AuthProvider } from '@/lib/providers/auth' export { // Catch any errors thrown by the Layout component. @@ -85,23 +86,25 @@ const RootLayoutNav = () => { ][settings.color] } > - ( - - ), - }} - > - - - - - - + + ( + + ), + }} + > + + + + + + + ) } diff --git a/bun.lockb b/bun.lockb index 356344a..10525b0 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/lib/providers/auth.tsx b/lib/providers/auth.tsx new file mode 100644 index 0000000..5e2c665 --- /dev/null +++ b/lib/providers/auth.tsx @@ -0,0 +1,67 @@ +import { + GoogleSignin, + type User, +} from '@react-native-google-signin/google-signin' +import { createContext, useContext, useEffect, useState, type ReactNode } from 'react' + +interface AuthProviderProps { + children: ReactNode +} + +export function AuthProvider({ children }: AuthProviderProps) { + const [user, setUser] = useState(null) + + GoogleSignin.configure({ + scopes: [ + 'https://www.googleapis.com/auth/classroom.courses.readonly', + 'https://www.googleapis.com/auth/classroom.coursework.me', + 'https://www.googleapis.com/auth/classroom.coursework.students', + 'https://www.googleapis.com/auth/classroom.coursework.students.readonly', + 'https://www.googleapis.com/auth/classroom.coursework.me.readonly', + ], + }) + + useEffect(() => { + setUser(GoogleSignin.getCurrentUser()) + }, []) + + const signIn = async () => { + try { + await GoogleSignin.hasPlayServices() + const userInfo = await GoogleSignin.signIn() + setUser(userInfo.data) + return userInfo + } catch (error) { + console.error(error) + } + } + + const signOut = async () => { + try { + await GoogleSignin.signOut() + setUser(null) + } catch (error) { + console.error(error) + } + } + + return ( + + {children} + + ) +} + +interface AuthContextType { + user: User | null + signIn: () => Promise + signOut: () => Promise +} + +const AuthContext = createContext({ + user: null, + signIn: async () => null, + signOut: async () => {}, +}) + +export const useAuth = (): AuthContextType => useContext(AuthContext) diff --git a/package.json b/package.json index 0ac1cdb..8314e89 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@expo-google-fonts/jetbrains-mono": "^0.2.3", "@expo-google-fonts/noto-sans": "^0.2.3", "@expo/vector-icons": "^14.0.0", + "@react-native-google-signin/google-signin": "^13.1.0", "@react-navigation/drawer": "^6.6.15", "@react-navigation/native": "^6.0.2", "@shopify/flash-list": "1.6.4",