0x0x230x

Untitled

Jul 23rd, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. import { useGenerateTokens } from '@/hooks/queries/useAuth';
  2. import { env } from '@/lib/env';
  3. import {
  4. useIdentityToken,
  5. useLinkWithFarcaster,
  6. useLoginWithFarcaster,
  7. usePrivy,
  8. } from '@privy-io/expo';
  9. import { usePathname } from 'expo-router';
  10. import React, { useState } from 'react';
  11. import { Linking, Text, TouchableOpacity, Platform } from 'react-native';
  12. import { openBrowserAsync } from 'expo-web-browser';
  13. import { PlatformIcon } from '../ui/PlatformIcon';
  14.  
  15. export function SignInWithFarcaster() {
  16. const { linkWithFarcaster } = useLinkWithFarcaster();
  17. const { user } = usePrivy();
  18. const { getAccessToken } = usePrivy();
  19. const { getIdentityToken } = useIdentityToken();
  20. const { mutate: generateTokens } = useGenerateTokens();
  21. const pathname = usePathname();
  22. const [isLoading, setIsLoading] = useState(false);
  23.  
  24. const handleSuccess = async () => {
  25. const [privyAccessToken, privyIdentityToken] = await Promise.all([
  26. getAccessToken(),
  27. getIdentityToken(),
  28. ]);
  29.  
  30. if (!privyAccessToken || !privyIdentityToken) {
  31. return null;
  32. }
  33.  
  34. generateTokens({
  35. accessToken: privyAccessToken,
  36. identityToken: privyIdentityToken,
  37. });
  38. };
  39.  
  40. const { loginWithFarcaster } = useLoginWithFarcaster({
  41. onSuccess: handleSuccess,
  42. onError: (error) => {
  43. console.log('loginWithFarcaster error', error);
  44. setIsLoading(false);
  45. },
  46. });
  47.  
  48. const handleFarcasterSignIn = async () => {
  49. setIsLoading(true);
  50.  
  51. try {
  52. // For iOS, check if Farcaster is installed
  53.  
  54. if (Platform.OS === 'ios') {
  55. const canOpen = await Linking.canOpenURL('farcaster://');
  56.  
  57. if (canOpen) {
  58. // Farcaster is installed - proceed with normal flow
  59. if (user) {
  60. linkWithFarcaster({
  61. relyingParty: env.PRIVY_RELYING_PARTY,
  62. redirectUrl: pathname,
  63. });
  64. } else {
  65. loginWithFarcaster({
  66. relyingParty: env.PRIVY_RELYING_PARTY,
  67. redirectUrl: pathname,
  68. });
  69. }
  70. } else {
  71. // Farcaster not installed - redirect to App Store
  72.  
  73. try {
  74. await openBrowserAsync('https://farcaster.xyz');
  75. } catch (error) {
  76. console.log('error', error);
  77. }
  78.  
  79. // console.log('result', result);
  80. setIsLoading(false);
  81. }
  82. } else {
  83. // For Android, proceed directly with login/link
  84. if (user) {
  85. linkWithFarcaster({
  86. relyingParty: env.PRIVY_RELYING_PARTY,
  87. redirectUrl: pathname,
  88. });
  89. } else {
  90. loginWithFarcaster({
  91. relyingParty: env.PRIVY_RELYING_PARTY,
  92. redirectUrl: pathname,
  93. });
  94. }
  95. }
  96. } catch (error) {
  97. console.log('Error with Farcaster sign in:', error);
  98. setIsLoading(false);
  99. }
  100. };
  101.  
  102. return (
  103. <TouchableOpacity
  104. 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'
  105. onPress={handleFarcasterSignIn}
  106. activeOpacity={0.7}
  107. accessibilityRole='button'
  108. disabled={isLoading}
  109. >
  110. <Text className='text-white text-2xl font-semibold font-superstar'>
  111. {isLoading ? 'Checking...' : 'Sign in with Farcaster'}
  112. </Text>
  113. <PlatformIcon platform='farcaster' size={20} className='mr-2' />
  114. </TouchableOpacity>
  115. );
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment