Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mobile): clerk oauth apple and google #59

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
};
}, []);
};