Skip to content
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: 3 additions & 2 deletions app/(app)/(authorized)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Redirect, Stack } from 'expo-router'
import { useAtomValue } from 'jotai'

import { useAuth } from '~hooks'
import { isSignedInAtom } from '~store/auth'

export const unstable_settings = {
initialRouteName: '(tabs)',
}

export default function AuthorizedLayout() {
const { isSignedIn } = useAuth()
const isSignedIn = useAtomValue(isSignedInAtom)

if (isSignedIn === false) {
return <Redirect href="/sign-in" />
Expand Down
6 changes: 3 additions & 3 deletions app/(app)/(not-authorized)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Redirect, Stack } from 'expo-router'
import { useAtomValue } from 'jotai'

import { useAuth } from '~hooks'

import { isSignedInAtom } from '~store/auth'
export const unstable_settings = {
initialRouteName: 'sign-in',
}

export default function NotAuthorizedLayout() {
const { isSignedIn } = useAuth()
const isSignedIn = useAtomValue(isSignedInAtom)

if (isSignedIn === true) {
return <Redirect href="/home" />
Expand Down
6 changes: 4 additions & 2 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { ThemeProvider } from '@react-navigation/native'
import { Slot } from 'expo-router'
import { useAtomValue } from 'jotai'

import { AbsoluteFullFill, Loader, StatusBar } from '~components'
import { useAuth, useNavigationTheme, useRouterNotifications } from '~hooks'
import { useNavigationTheme, useRouterNotifications } from '~hooks'
import { Providers } from '~providers'
import { isSignedInAtom } from '~store/auth'

export const unstable_settings = {
initialRouteName: 'index',
}

const Layout = () => {
const { isSignedIn } = useAuth()
const isSignedIn = useAtomValue(isSignedInAtom)
const { navigationTheme } = useNavigationTheme()

useRouterNotifications() // TODO: check if handling notification deeplinks works correctly
Expand Down
5 changes: 3 additions & 2 deletions app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Redirect } from 'expo-router'
import { useAtomValue } from 'jotai'
import { Platform } from 'react-native'

import { useAuth } from '~hooks'
import { LandingScreen } from '~screens/LandingScreen'
import { isSignedInAtom } from '~store/auth'

export default function Root() {
const { isSignedIn } = useAuth()
const isSignedIn = useAtomValue(isSignedInAtom)

if (isSignedIn === true) {
return <Redirect href="/home" />
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"expo-updates": "~0.24.8",
"expo-web-browser": "~12.8.2",
"i18next": "^23.7.20",
"jotai": "^2.4.3",
"moti": "^0.25.3",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
7 changes: 5 additions & 2 deletions src/components/AppLoading.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as SplashScreen from 'expo-splash-screen'
import { useAtomValue } from 'jotai'
import { FC, PropsWithChildren, useCallback, useEffect } from 'react'
import { View, StyleSheet } from 'react-native'

import { AbsoluteFullFill, Loader, Center } from './atoms'

import { useAuth, useBoolean, useCachedResources, useFonts } from '~hooks'
import { useBoolean, useCachedResources, useFonts } from '~hooks'
import { isSignedInAtom } from '~store/auth'

SplashScreen.preventAutoHideAsync()

Expand All @@ -21,7 +23,8 @@ export const AppLoading: FC<PropsWithChildren> = ({ children }) => {

// Delay loading logic was made to prevent displaying empty screen after splash screen will hide
const [isDelayLoading, setIsDelayLoading] = useBoolean(true)
const { isSignedIn } = useAuth()

const isSignedIn = useAtomValue(isSignedInAtom)

useEffect(() => {
async function prepare() {
Expand Down
14 changes: 2 additions & 12 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import { NativeStackHeaderProps } from '@react-navigation/native-stack'
import { useRouter } from 'expo-router'

import { Touchable } from './atoms'

import { Box, Column, Row, Icon, Text } from '~components/atoms'
import { Box, Column, Row, Icon, Text, Touchable } from '~components/atoms'

const logoHeight = 24

export const Header = ({ options, ...rest }: NativeStackHeaderProps) => {
export const Header = ({ options }: NativeStackHeaderProps) => {
const router = useRouter()

console.log('options', {
options,
rest,
canGoBack: router.canGoBack(),
restxd: rest.navigation.getState(),
canGoBack2: rest.navigation.canGoBack(),
})

return (
<Column bg="fg.white">
<Row bg="fg.white" zIndex={3} justifyContent="space-between" alignItems="center">
Expand Down
7 changes: 4 additions & 3 deletions src/components/LandingHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { useRouter } from 'expo-router'
import { useAtomValue } from 'jotai'
import { Image, StyleSheet, Platform, View } from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'

import { Box, Button, Icon, Pressable } from '~components'
import { darkLogoFull, lightLogoFull } from '~constants'
import { useColorScheme } from '~contexts'
import { useAuth, useTranslation } from '~hooks'
import { useTranslation } from '~hooks'
import { TabColorsStrings } from '~navigation/tabNavigator/config'

import { isSignedInAtom } from '~store/auth'
export function LandingHeader() {
const { colorScheme } = useColorScheme()
const { top } = useSafeAreaInsets()
const { t } = useTranslation()
const { push, canGoBack, back } = useRouter()

const { isSignedIn } = useAuth()
const isSignedIn = useAtomValue(isSignedInAtom)

const navigateToLogin = () => push('/sign-in')

Expand Down
8 changes: 4 additions & 4 deletions src/constants/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ export const theme = {
export const lightNavigationTheme: Theme = {
colors: {
background: themeColors.lightMode.bg.primary,
border: themeColors.lightMode.border.primary,
card: themeColors.lightMode.button.primary.bg,
border: 'transparent',
card: themeColors.lightMode.bg.primary,
text: themeColors.lightMode.alpha.black[70],
notification: themeColors.lightMode.avatar.bg,
primary: themeColors.lightMode.utility.purple[500],
Expand All @@ -368,8 +368,8 @@ export const lightNavigationTheme: Theme = {
export const darkNavigationTheme: Theme = {
colors: {
background: themeColors.darkMode.bg.primary,
border: themeColors.darkMode.border.primary,
card: themeColors.darkMode.button.primary.bg,
border: 'transparent',
card: themeColors.darkMode.bg.primary,
text: themeColors.darkMode.alpha.black[70],
notification: themeColors.darkMode.avatar.bg,
primary: themeColors.darkMode.utility.purple[500],
Expand Down
17 changes: 0 additions & 17 deletions src/contexts/AuthContext.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './AuthContext'
export * from './NotificationContext'
export * from './ColorSchemeContext'
24 changes: 19 additions & 5 deletions src/hooks/forms/useSignInForm.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { isError } from '@tanstack/react-query'
import { useSetAtom } from 'jotai'
import { useState } from 'react'
import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'

import { useAuth } from '../useAuth'

import { setToken } from '~services'
import { isSignedInAtom } from '~store/auth'
import { SignInFormValues } from '~types/authForms'
import { hapticImpact } from '~utils'
import { hapticImpact, wait } from '~utils'

const defaultValues: SignInFormValues = {
// TODO: Reset this values when building production app
Expand All @@ -16,11 +17,13 @@ const defaultValues: SignInFormValues = {
}

export const useSignInForm = () => {
const { signIn } = useAuth()
const [error, setError] = useState('')
const [isSubmitting, setIsSubmitting] = useState(false)

const { t } = useTranslation()

const setIsSignedIn = useSetAtom(isSignedInAtom)

const {
control,
formState: { errors },
Expand All @@ -35,7 +38,18 @@ export const useSignInForm = () => {
try {
setIsSubmitting(true)
setError('')
await signIn(data)
// Errors are handled on UI side
// if you want to stop this function with error just throw new Error.
// Remember to pass readable error message for user, because this error will be displayed for him

// TODO: Add some backend call here, you can use react query for this
await wait(500)

if (data.email !== 'test@example.com' || data.password !== '123456') {
throw new Error('Incorrect email or password')
}
await setToken('token_here')
setIsSignedIn(true)
} catch (e) {
if (isError(e)) {
setError(e.message)
Expand Down
14 changes: 9 additions & 5 deletions src/hooks/forms/useSignUpForm.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isError } from '@tanstack/react-query'
import { useSetAtom } from 'jotai'
import { useState } from 'react'
import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'

import { useAuth } from '../useAuth'

import { isSignedInAtom } from '~store/auth'
import { SignUpFormValues } from '~types/authForms'
import { hapticImpact } from '~utils'
import { hapticImpact, wait } from '~utils'

const defaultValues: SignUpFormValues = {
user: '',
Expand All @@ -17,11 +17,13 @@ const defaultValues: SignUpFormValues = {
}

export const useSignUpForm = () => {
const { signUp } = useAuth()
const [error, setError] = useState('')
const [isSubmitting, setIsSubmitting] = useState(false)

const { t } = useTranslation()

const setIsSignedIn = useSetAtom(isSignedInAtom)

const {
control,
formState: { errors },
Expand All @@ -36,7 +38,9 @@ export const useSignUpForm = () => {
try {
setIsSubmitting(true)
setError('')
await signUp(data)
await wait(500)
// TODO: Add some backend call here, you can use react query for this
setIsSignedIn(true)
} catch (e) {
if (isError(e)) {
setError(e.message)
Expand Down
1 change: 0 additions & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export * from './navigation'

// Custom hooks implemented in app
export * from './useAppStateActive'
export * from './useAuth'
export * from './useBoolean'
export * from './useCachedResources'
export * from './useKeyboardHeight'
Expand Down
2 changes: 0 additions & 2 deletions src/hooks/useAuth.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/logic/AuthLogic.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useSetAtom } from 'jotai'
import { FC } from 'react'

import { useEffect } from '~hooks'
import { getToken } from '~services'
import { isSignedInAtom } from '~store/auth'

export const AuthLogic: FC = () => {
const setIsSignedIn = useSetAtom(isSignedInAtom)

useEffect(() => {
const bootstrap = async () => {
// TODO: This should be moved to backend calls, in this bootstrap function we should fetch user info and not token
const token = await getToken()
setIsSignedIn(!!token)
}

bootstrap()
}, [setIsSignedIn])

return null
}
Empty file added src/logic/index.ts
Empty file.
3 changes: 1 addition & 2 deletions src/navigation/tabNavigator/components/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import { TabColorsStrings, upperSideTabs } from '../config'
import { useWidth } from '../hooks'
import { cns } from '../utils'

import { useAuth } from '~hooks'
import { signOut } from '~store/auth'
import cssStyles from '~styles'

const NAV_MEDIUM_WIDTH = 244

export function SideBar({ visible }: { visible: boolean }) {
const isLarge = useWidth(1264)
const { signOut } = useAuth()

return (
<View
Expand Down
Loading