Skip to content
Open
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
14,166 changes: 14,166 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/context/AuthenticationContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext } from 'react';
import { User } from '../types/User';
import { User } from '../types/Users';

export type AuthenticationContextObject = {
value: User;
Expand Down
37 changes: 25 additions & 12 deletions src/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AuthenticationContext } from '../context/AuthenticationContext';
import logoImg from '../images/logo.png';
import * as api from '../services/api';
import { getFromCache, setInCache } from '../services/caching';
import { User } from '../types/User';
import { Users } from '../types/Users';
import { isTokenExpired, sanitizeEmail, validateEmail } from '../utils';

export default function Login({ navigation }: StackScreenProps<any>) {
Expand All @@ -28,20 +28,34 @@ export default function Login({ navigation }: StackScreenProps<any>) {
const isFocused = useIsFocused();

useEffect(() => {
getFromCache('userInfo').then(
(cachedUserInfo) => authenticationContext?.setValue(cachedUserInfo as User),
(error: any) => console.log(error)
);
getFromCache('accessToken').then(
(accessToken) => accessToken && !isTokenExpired(accessToken as string) && setAccessTokenIsValid(true),
(error: any) => console.log(error)
);
if (authError)
const checkCache = async () => {
try {
const cachedUserInfo = await getFromCache<Users>('userInfo');
const cachedAccessToken = await getFromCache<string>('accessToken');

if (cachedUserInfo) {
authenticationContext?.setValue(cachedUserInfo);
}

if (cachedAccessToken && !isTokenExpired(cachedAccessToken)) {
setAccessTokenIsValid(true);
}
} catch (error) {
console.log('Error fetching cache:', error);
}
};

checkCache();

if (authError) {
Alert.alert('Authentication Error', authError, [{ text: 'Ok', onPress: () => setAuthError(undefined) }]);
}
}, [authError]);

useEffect(() => {
if (accessTokenIsValid && authenticationContext?.value) navigation.navigate('EventsMap');
if (accessTokenIsValid && authenticationContext?.value) {
navigation.navigate('EventsMap');
}
}, [accessTokenIsValid]);

const handleAuthentication = () => {
Expand All @@ -53,7 +67,6 @@ export default function Login({ navigation }: StackScreenProps<any>) {
setInCache('accessToken', response.data.accessToken);
authenticationContext?.setValue(response.data.user);
setIsAuthenticating(false);
123;
navigation.navigate('EventsMap');
})
.catch((error) => {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/AppStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const { Navigator, Screen } = createStackNavigator();
import Login from '../pages/Login';
import EventsMap from '../pages/EventsMap';
import { AuthenticationContext, AuthenticationContextObject } from '../context/AuthenticationContext';
import { User } from '../types/User';
import { Users } from '../types/Users';

export default function Routes() {
const [authenticatedUser, setAuthenticatedUser] = useState<User>();
const [authenticatedUser, setAuthenticatedUser] = useState<Users>();

const authenticationContextObj: AuthenticationContextObject = {
value: authenticatedUser as User,
value: authenticatedUser as Users,
setValue: setAuthenticatedUser,
};

Expand Down
4 changes: 2 additions & 2 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import axios, { AxiosResponse } from 'axios';
const api = axios.create({
// Before running your 'json-server', get your computer's IP address and
// update your baseURL to `http://your_ip_address_here:3333` and then run:
// `npx json-server --watch db.json --port 3333 --host your_ip_address_here`
// `npx json-server --watch db.json --port 3333 --host 192.168.137.68`
//
// To access your server online without running json-server locally,
// you can set your baseURL to:
// `https://my-json-server.typicode.com/<your-github-username>/<your-github-repo>`
//
// To use `my-json-server`, make sure your `db.json` is located at the repo root.

baseURL: 'http://0.0.0.0:3333',
baseURL: 'http://192.168.56.1:3333', // i changed the ipv4 address
});

export const authenticateUser = (email: string, password: string): Promise<AxiosResponse> => {
Expand Down
21 changes: 15 additions & 6 deletions src/services/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
export const getFromNetworkFirst = async <T>(key: string, request: Promise<T>): Promise<T> => {
try {
const response = await request;
setInCache(key, response);
await setInCache(key, response);
return response;
} catch (e) {
return getFromCache<T>(key);
}
};

export const setInCache = (key: string, value: any) => {
const jsonValue = JSON.stringify(value);
return AsyncStorage.setItem(key, jsonValue);
export const setInCache = async (key: string, value: any) => {
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem(key, jsonValue);
} catch (e) {
console.error('Failed to save data to cache', e);
}
};

export const getFromCache = async <T>(key: string): Promise<T> => {
const json = await AsyncStorage.getItem(key);
return await (json != null ? Promise.resolve(JSON.parse(json)) : Promise.reject(`Key "${key}" not in cache`));
try {
const json = await AsyncStorage.getItem(key);
return json != null ? JSON.parse(json) : Promise.reject(`Key "${key}" not in cache`);
} catch (e) {
console.error('Failed to retrieve data from cache', e);
return Promise.reject(e);
}
};
2 changes: 1 addition & 1 deletion src/types/User.ts → src/types/Users.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface User {
export interface Users {
name: {
first: string;
last: string;
Expand Down
Loading