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

Authentication Flow #92

Merged
merged 4 commits into from
Jan 30, 2025
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
6 changes: 4 additions & 2 deletions frontend/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"userInterfaceStyle": "automatic",
"newArchEnabled": true,
"ios": {
"supportsTablet": true
"supportsTablet": true,
"bundleIdentifier": "com.anonymous.frontend"
},
"android": {
"adaptiveIcon": {
Expand All @@ -32,7 +33,8 @@
"resizeMode": "contain",
"backgroundColor": "#ffffff"
}
]
],
"expo-secure-store"
],
"experiments": {
"typedRoutes": true
Expand Down
24 changes: 24 additions & 0 deletions frontend/app/main/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getCurrentSession } from '@/services/auth/me';
import { User } from '@/types';
import { useEffect, useState } from 'react';
import { SafeAreaView, Text } from 'react-native';

const Index = () => {
const [user, setUser] = useState<User>();

useEffect(() => {
const call = async () => {
const [currentUser, error] = await getCurrentSession();

Check warning on line 11 in frontend/app/main/index.tsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'error' is assigned a value but never used
setUser(currentUser!);
};
call();
}, []);

return (
<SafeAreaView>
<Text>{user?.username}</Text>
</SafeAreaView>
);
};

export default Index;
7 changes: 6 additions & 1 deletion frontend/app/register/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@
import { register } from '@/services/auth/register';
import { Button } from '@/components/Button';
import { Spinner } from '@/components/Spinner';
import { useRouter } from 'expo-router';

const Index = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState<string>();
const [isLoading, setIsLoading] = useState(false);


const router = useRouter();
const registerUser = async () => {
setError(undefined);
setIsLoading(true);
const [_, error] = await register(username, password);

Check warning on line 22 in frontend/app/register/index.tsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'_' is assigned a value but never used

setIsLoading(false);
if (error) {
setError(error.error);
return;
}

setIsLoading(false);
router.navigate('/main');
};

return (
Expand Down
10 changes: 10 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"scripts": {
"start": "expo start",
"reset-project": "node ./scripts/reset-project.js",
"android": "expo start --android",
"ios": "expo start --ios",
"android": "expo run:android",
"ios": "expo run:ios",
"web": "expo start --web",
"test": "jest --watchAll",
"lint": "expo lint"
Expand Down Expand Up @@ -39,7 +39,8 @@
"react-native-safe-area-context": "4.12.0",
"react-native-screens": "~4.4.0",
"react-native-web": "~0.19.13",
"react-native-webview": "13.12.5"
"react-native-webview": "13.12.5",
"expo-secure-store": "~14.0.1"
},
"devDependencies": {
"@babel/core": "^7.25.2",
Expand Down
29 changes: 29 additions & 0 deletions frontend/services/auth/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { LOCAL_API_ENDPOINT } from '@/constants';
import { ErrorResponse, User } from '@/types';
import { saveCookie } from '@/utils/cookies';

export const login = async (
username: string,
password: string
): Promise<[User | null, ErrorResponse | null]> => {
const response = await fetch(`${LOCAL_API_ENDPOINT}/user/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
credentials: 'include',
},
body: JSON.stringify({ username, password }),
});

saveCookie(response.headers.get('set-cookie')!);

const data = await response.json();

console.log(data);

if (data.error) {
return [null, { error: data.error }];
}

return [data.user as User, null];
};
22 changes: 22 additions & 0 deletions frontend/services/auth/me.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { LOCAL_API_ENDPOINT } from '@/constants';
import { ErrorResponse, User } from '@/types';
import { getCookie } from '@/utils/cookies';

export const getCurrentSession = async (): Promise<
[User | null, ErrorResponse | null]
> => {
const response = await fetch(`${LOCAL_API_ENDPOINT}/user`, {
headers: {
Cookie: (await getCookie())!,
},
});
const data = await response.json();

console.log(data);

if (data.error) {
return [null, { error: data.error }];
}

return [data.user as User, null];
};
3 changes: 3 additions & 0 deletions frontend/services/auth/register.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LOCAL_API_ENDPOINT } from '@/constants';
import { ErrorResponse, User } from '@/types';
import { saveCookie } from '@/utils/cookies';

export const register = async (
username: string,
Expand All @@ -13,6 +14,8 @@ export const register = async (
body: JSON.stringify({ username, password }),
});

saveCookie(response.headers.get('set-cookie')!);

const data = await response.json();

if (data.error) {
Expand Down
11 changes: 11 additions & 0 deletions frontend/utils/cookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as SecureStore from 'expo-secure-store';

const COOKIE_KEY = 'auth-cookie';

export const saveCookie = async (cookieValue: string) => {
await SecureStore.setItemAsync(COOKIE_KEY, cookieValue);
};

export const getCookie = async () => {
return await SecureStore.getItemAsync(COOKIE_KEY);
};
Loading