Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { useGenerateTokens } from '@/hooks/queries/useAuth';
- import { env } from '@/lib/env';
- import {
- useIdentityToken,
- useLinkWithFarcaster,
- useLoginWithFarcaster,
- usePrivy,
- } from '@privy-io/expo';
- import { usePathname } from 'expo-router';
- import React, { useState } from 'react';
- import { Linking, Text, TouchableOpacity, Platform } from 'react-native';
- import { openBrowserAsync } from 'expo-web-browser';
- import { PlatformIcon } from '../ui/PlatformIcon';
- export function SignInWithFarcaster() {
- const { linkWithFarcaster } = useLinkWithFarcaster();
- const { user } = usePrivy();
- const { getAccessToken } = usePrivy();
- const { getIdentityToken } = useIdentityToken();
- const { mutate: generateTokens } = useGenerateTokens();
- const pathname = usePathname();
- const [isLoading, setIsLoading] = useState(false);
- const handleSuccess = async () => {
- const [privyAccessToken, privyIdentityToken] = await Promise.all([
- getAccessToken(),
- getIdentityToken(),
- ]);
- if (!privyAccessToken || !privyIdentityToken) {
- return null;
- }
- generateTokens({
- accessToken: privyAccessToken,
- identityToken: privyIdentityToken,
- });
- };
- const { loginWithFarcaster } = useLoginWithFarcaster({
- onSuccess: handleSuccess,
- onError: (error) => {
- console.log('loginWithFarcaster error', error);
- setIsLoading(false);
- },
- });
- const handleFarcasterSignIn = async () => {
- setIsLoading(true);
- try {
- // For iOS, check if Farcaster is installed
- if (Platform.OS === 'ios') {
- const canOpen = await Linking.canOpenURL('farcaster://');
- if (canOpen) {
- // Farcaster is installed - proceed with normal flow
- if (user) {
- linkWithFarcaster({
- relyingParty: env.PRIVY_RELYING_PARTY,
- redirectUrl: pathname,
- });
- } else {
- loginWithFarcaster({
- relyingParty: env.PRIVY_RELYING_PARTY,
- redirectUrl: pathname,
- });
- }
- } else {
- // Farcaster not installed - redirect to App Store
- try {
- await openBrowserAsync('https://farcaster.xyz');
- } catch (error) {
- console.log('error', error);
- }
- // console.log('result', result);
- setIsLoading(false);
- }
- } else {
- // For Android, proceed directly with login/link
- if (user) {
- linkWithFarcaster({
- relyingParty: env.PRIVY_RELYING_PARTY,
- redirectUrl: pathname,
- });
- } else {
- loginWithFarcaster({
- relyingParty: env.PRIVY_RELYING_PARTY,
- redirectUrl: pathname,
- });
- }
- }
- } catch (error) {
- console.log('Error with Farcaster sign in:', error);
- setIsLoading(false);
- }
- };
- return (
- <TouchableOpacity
- className='flex-row items-center justify-center w-full bg-[#222224] hover:bg-[#222224]/70 transition-all mx-0.5 text-gray-300 hover:text-white border border-[#2c2c2e] py-2 px-5 rounded-lg gap-2'
- onPress={handleFarcasterSignIn}
- activeOpacity={0.7}
- accessibilityRole='button'
- disabled={isLoading}
- >
- <Text className='text-white text-2xl font-semibold font-superstar'>
- {isLoading ? 'Checking...' : 'Sign in with Farcaster'}
- </Text>
- <PlatformIcon platform='farcaster' size={20} className='mr-2' />
- </TouchableOpacity>
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment