Skip to content

Commit

Permalink
feat(mobile): clerk oauth apple and google
Browse files Browse the repository at this point in the history
  • Loading branch information
Quốc Khánh committed Jun 9, 2024
1 parent 4efd2e2 commit a0b34e3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
5 changes: 5 additions & 0 deletions apps/mobile/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
} from '@expo-google-fonts/be-vietnam-pro'
import { Stack } from 'expo-router'
import * as SplashScreen from 'expo-splash-screen'
import * as WebBrowser from "expo-web-browser";

import 'react-native-reanimated'
import { useWarmUpBrowser } from '@/hooks/use-warm-up-browser'
import { useColorScheme } from '@/hooks/useColorScheme'
import { queryClient } from '@/lib/client'
import {
Expand All @@ -36,12 +38,15 @@ cssInterop(Svg, {
// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync()

WebBrowser.maybeCompleteAuthSession();

// biome-ignore lint/style/useNamingConvention: <explanation>
export const unstable_settings = {
initialRouteName: '(app)',
}

export default function RootLayout() {
useWarmUpBrowser();
const colorScheme = useColorScheme()
const [fontsLoaded] = useFonts({
BeVietnamPro_300Light,
Expand Down
47 changes: 38 additions & 9 deletions apps/mobile/components/auth/auth-social.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useOAuth } from '@clerk/clerk-expo'
import type { SvgProps } from 'react-native-svg'
import { Button } from '../Button'
import { AppleLogo } from '../svg-assets/apple-logo'
Expand All @@ -6,35 +7,63 @@ import { GoogleLogo } from '../svg-assets/google-logo'
type AuthSocialProps = {
label: string
icon: React.ComponentType<SvgProps>
onPress?: () => void
}

export function AuthSocial({
label,
icon: Icon,
}: AuthSocialProps) {
export function AuthSocial({ label, icon: Icon, onPress }: AuthSocialProps) {
return (
<Button
label={label}
leftIcon={Icon}
variant="outline"
/>
<Button label={label} leftIcon={Icon} variant="outline" onPress={onPress} />
)
}

export function GoogleAuthButton() {
const { startOAuthFlow } = useOAuth({ strategy: 'oauth_google' })

const onPress = async () => {
try {
const { createdSessionId, setActive } = await startOAuthFlow()

if (createdSessionId) {
setActive?.({ session: createdSessionId })
} else {
// Use signIn or signUp for next steps such as MFA
}
} catch (err) {
console.error('OAuth error', err)
}
}

return (
<AuthSocial
label="Sign in with Google"
icon={GoogleLogo}
onPress={onPress}
/>
)
}

export function AppleAuthButton() {
const { startOAuthFlow } = useOAuth({ strategy: 'oauth_apple' })

const onPress = async () => {
try {
const { createdSessionId, setActive } = await startOAuthFlow()

if (createdSessionId) {
setActive?.({ session: createdSessionId })
} else {
// Use signIn or signUp for next steps such as MFA
}
} catch (err) {
console.error('OAuth error', err)
}
}

return (
<AuthSocial
label="Sign in with Apple"
icon={AppleLogo}
onPress={onPress}
/>
)
}
13 changes: 13 additions & 0 deletions apps/mobile/hooks/use-warm-up-browser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as WebBrowser from "expo-web-browser";
import React from "react";

export const useWarmUpBrowser = () => {
React.useEffect(() => {
// biome-ignore lint/complexity/noVoid: <explanation>
void WebBrowser.warmUpAsync();
return () => {
// biome-ignore lint/complexity/noVoid: <explanation>
void WebBrowser.coolDownAsync();
};
}, []);
};

0 comments on commit a0b34e3

Please sign in to comment.