Skip to content

Commit

Permalink
Fix small issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ProchaLu committed Nov 3, 2024
1 parent 8793408 commit 5e63643
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 69 deletions.
15 changes: 9 additions & 6 deletions app/api/login+api.ts → app/(auth)/api/login+api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import crypto from 'node:crypto';
import bcryptJs from 'bcryptjs';
import { createSessionInsecure } from '../../database/sessions';
import { getUserWithPasswordHashInsecure } from '../../database/users';
import { ExpoApiResponse } from '../../ExpoApiResponse';
import { type User, userSchema } from '../../migrations/00001-createTableUsers';
import { createSerializedRegisterSessionTokenCookie } from '../../util/cookies';
import { createSessionInsecure } from '../../../database/sessions';
import { getUserWithPasswordHashInsecure } from '../../../database/users';
import { ExpoApiResponse } from '../../../ExpoApiResponse';
import {
type User,
userSchema,
} from '../../../migrations/00001-createTableUsers';
import { createSerializedRegisterSessionTokenCookie } from '../../../util/cookies';

export type LoginResponseBodyPost =
| {
Expand Down Expand Up @@ -35,7 +38,7 @@ export async function POST(
);
}

// 3. verify the user credentials
// 3. Verify the user credentials
const userWithPasswordHash = await getUserWithPasswordHashInsecure(
result.data.username,
);
Expand Down
6 changes: 3 additions & 3 deletions app/api/logout+api.ts → app/(auth)/api/logout+api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parse } from 'cookie';
import { deleteSession } from '../../database/sessions';
import { ExpoApiResponse } from '../../ExpoApiResponse';
import { deleteSerializedRegisterSessionTokenCookie } from '../../util/cookies';
import { deleteSession } from '../../../database/sessions';
import { ExpoApiResponse } from '../../../ExpoApiResponse';
import { deleteSerializedRegisterSessionTokenCookie } from '../../../util/cookies';

export type LogoutResponseBodyGet =
| {
Expand Down
25 changes: 17 additions & 8 deletions app/api/register+api.ts → app/(auth)/api/register+api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import crypto from 'node:crypto';
import bcryptJs from 'bcryptjs';
import { createSessionInsecure } from '../../database/sessions';
import { createUserInsecure, getUserInsecure } from '../../database/users';
import { ExpoApiResponse } from '../../ExpoApiResponse';
import { type User, userSchema } from '../../migrations/00001-createTableUsers';
import { createSerializedRegisterSessionTokenCookie } from '../../util/cookies';
import { createSessionInsecure } from '../../../database/sessions';
import { createUserInsecure, getUserInsecure } from '../../../database/users';
import { ExpoApiResponse } from '../../../ExpoApiResponse';
import {
type User,
userSchema,
} from '../../../migrations/00001-createTableUsers';
import { createSerializedRegisterSessionTokenCookie } from '../../../util/cookies';

export type RegisterResponseBodyPost =
| {
Expand Down Expand Up @@ -40,7 +43,9 @@ export async function POST(

if (user) {
return ExpoApiResponse.json(
{ error: 'Username already taken' },
{
error: 'Username already taken',
},
{
status: 401,
},
Expand All @@ -55,7 +60,9 @@ export async function POST(

if (!newUser) {
return ExpoApiResponse.json(
{ error: 'Registration failed' },
{
error: 'Registration failed',
},
{
status: 500,
},
Expand All @@ -70,7 +77,9 @@ export async function POST(

if (!session) {
return ExpoApiResponse.json(
{ error: 'Sessions creation failed' },
{
error: 'Sessions creation failed',
},
{
status: 401,
},
Expand Down
7 changes: 4 additions & 3 deletions app/(auth)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
View,
} from 'react-native';
import { colors } from '../../constants/colors';
import type { LoginResponseBodyPost } from '../api/login+api';
import type { UserResponseBodyGet } from '../api/user+api';
import type { LoginResponseBodyPost } from './api/login+api';

const styles = StyleSheet.create({
container: {
Expand Down Expand Up @@ -82,9 +83,9 @@ export default function Login() {
async function getUser() {
const response = await fetch('/api/user');

const data = await response.json();
const responseBody: UserResponseBodyGet = await response.json();

if ('username' in data) {
if ('username' in responseBody) {
router.push('/(tabs)/guests');
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/(auth)/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
View,
} from 'react-native';
import { colors } from '../../constants/colors';
import type { RegisterResponseBodyPost } from '../api/register+api';
import type { RegisterResponseBodyPost } from './api/register+api';

const styles = StyleSheet.create({
container: {
Expand Down
14 changes: 4 additions & 10 deletions app/(tabs)/guests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FlatList, SafeAreaView, StyleSheet } from 'react-native';
import GuestItem from '../../components/GuestItem';
import { colors } from '../../constants/colors';
import type { Guest } from '../../migrations/00000-createTableGuests';
import type { GuestsResponseBodyGet } from '../api/guests+api';
import type { GuestsResponseBodyGet } from '../api/guests/guests+api';
import type { UserResponseBodyGet } from '../api/user+api';

const styles = StyleSheet.create({
Expand All @@ -26,18 +26,13 @@ export default function App() {
const [fontsLoaded] = useFonts({
Poppins_400Regular,
});
const [isStale, setIsStale] = useState(true);

// const renderItem = (item: { item: User }) => <UserItem user={item.item} />;

const renderItem = (item: { item: Guest }) => (
<GuestItem guest={item.item} setIsStale={setIsStale} />
);
const renderItem = (item: { item: Guest }) => <GuestItem guest={item.item} />;

useFocusEffect(
useCallback(() => {
if (!isStale) return;

async function getUser() {
const response = await fetch('/api/user');

Expand All @@ -49,11 +44,10 @@ export default function App() {
}

async function getGuests() {
const response = await fetch('/api/guests');
const response = await fetch('/api/guests/guests');
const body: GuestsResponseBodyGet = await response.json();

setGuests(body.guests);
setIsStale(false);
}

getUser().catch((error) => {
Expand All @@ -63,7 +57,7 @@ export default function App() {
getGuests().catch((error) => {
console.error(error);
});
}, [isStale]),
}, []),
);

if (!fontsLoaded) {
Expand Down
4 changes: 2 additions & 2 deletions app/(tabs)/notes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FlatList, SafeAreaView, StyleSheet } from 'react-native';
import NoteItem from '../../components/NotesItem';
import { colors } from '../../constants/colors';
import type { Note } from '../../migrations/00003-createTableNotes';
import type { NotesResponseBodyGet } from '../api/notes+api';
import type { NotesResponseBodyGet } from '../api/notes/notes+api';
import type { UserResponseBodyGet } from '../api/user+api';

const styles = StyleSheet.create({
Expand Down Expand Up @@ -45,7 +45,7 @@ export default function App() {
}

async function getNotes() {
const response = await fetch('/api/notes');
const response = await fetch('/api/notes/notes');
const body: NotesResponseBodyGet = await response.json();

if ('error' in body) {
Expand Down
2 changes: 1 addition & 1 deletion app/(tabs)/profile.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { router, useFocusEffect } from 'expo-router';
import React, { useCallback } from 'react';
import { Alert, Pressable, StyleSheet, Text, View } from 'react-native';
import type { LogoutResponseBodyGet } from '../(auth)/api/logout+api';
import { colors } from '../../constants/colors';
import type { LogoutResponseBodyGet } from '../api/logout+api';
import type { UserResponseBodyGet } from '../api/user+api';

const styles = StyleSheet.create({
Expand Down
6 changes: 3 additions & 3 deletions app/api/[guestId]+api.ts → app/api/guests/[guestId]+api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {
deleteGuestInsecure,
getGuestInsecure,
updateGuestInsecure,
} from '../../database/guests';
import { ExpoApiResponse } from '../../ExpoApiResponse';
} from '../../../database/guests';
import { ExpoApiResponse } from '../../../ExpoApiResponse';
import {
type Guest,
guestsSchema,
} from '../../migrations/00000-createTableGuests';
} from '../../../migrations/00000-createTableGuests';

export type GuestResponseBodyGet =
| {
Expand Down
9 changes: 6 additions & 3 deletions app/api/guests+api.ts → app/api/guests/guests+api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { createGuestInsecure, getGuestsInsecure } from '../../database/guests';
import { ExpoApiResponse } from '../../ExpoApiResponse';
import {
createGuestInsecure,
getGuestsInsecure,
} from '../../../database/guests';
import { ExpoApiResponse } from '../../../ExpoApiResponse';
import {
type Guest,
guestsSchema,
} from '../../migrations/00000-createTableGuests';
} from '../../../migrations/00000-createTableGuests';

export type GuestsResponseBodyGet = {
guests: Guest[];
Expand Down
6 changes: 3 additions & 3 deletions app/api/[noteId]+api.ts → app/api/notes/[noteId]+api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parse } from 'cookie';
import { getNote } from '../../database/notes';
import { ExpoApiResponse } from '../../ExpoApiResponse';
import type { Note } from '../../migrations/00003-createTableNotes';
import { getNote } from '../../../database/notes';
import { ExpoApiResponse } from '../../../ExpoApiResponse';
import type { Note } from '../../../migrations/00003-createTableNotes';

export type NoteResponseBodyGet =
| {
Expand Down
9 changes: 6 additions & 3 deletions app/api/notes+api.ts → app/api/notes/notes+api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { parse } from 'cookie';
import { createNote, getNotes } from '../../database/notes';
import { ExpoApiResponse } from '../../ExpoApiResponse';
import { type Note, noteSchema } from '../../migrations/00003-createTableNotes';
import { createNote, getNotes } from '../../../database/notes';
import { ExpoApiResponse } from '../../../ExpoApiResponse';
import {
type Note,
noteSchema,
} from '../../../migrations/00003-createTableNotes';

export type NotesResponseBodyGet =
| {
Expand Down
1 change: 0 additions & 1 deletion app/api/user+api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export async function GET(
request: Request,
): Promise<ExpoApiResponse<UserResponseBodyGet>> {
// 1. get the session token from the cookie

const cookies = parse(request.headers.get('cookie') || '');
const token = cookies.sessionToken;

Expand Down
20 changes: 10 additions & 10 deletions app/guests/[guestId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'react-native';
import placeholder from '../../assets/candidate-default.avif';
import { colors } from '../../constants/colors';
import type { GuestResponseBodyGet } from '../api/[guestId]+api';
import type { GuestResponseBodyGet } from '../api/guests/[guestId]+api';

const styles = StyleSheet.create({
container: {
Expand Down Expand Up @@ -125,13 +125,13 @@ export default function GuestPage() {
return;
}

const response = await fetch(`/api/${guestId}`);
const body: GuestResponseBodyGet = await response.json();
const response = await fetch(`/api/guests/${guestId}`);
const responseBody: GuestResponseBodyGet = await response.json();

if ('guest' in body) {
setFirstName(body.guest.firstName);
setLastName(body.guest.lastName);
setAttending(body.guest.attending);
if ('guest' in responseBody) {
setFirstName(responseBody.guest.firstName);
setLastName(responseBody.guest.lastName);
setAttending(responseBody.guest.attending);
}
}

Expand Down Expand Up @@ -195,7 +195,7 @@ export default function GuestPage() {
{ opacity: pressed ? 0.5 : 1 },
]}
onPress={async () => {
await fetch(`/api/${guestId}`, {
await fetch(`/api/guests/${guestId}`, {
method: 'PUT',
body: JSON.stringify({
firstName,
Expand Down Expand Up @@ -224,7 +224,7 @@ export default function GuestPage() {
<Switch
value={attending}
onValueChange={async () => {
await fetch(`/api/${guestId}`, {
await fetch(`/api/guests/${guestId}`, {
method: 'PUT',
body: JSON.stringify({
firstName: firstName,
Expand All @@ -249,7 +249,7 @@ export default function GuestPage() {
<Pressable
style={styles.icon}
onPress={async () => {
await fetch(`/api/${guestId}`, {
await fetch(`/api/guests/${guestId}`, {
method: 'DELETE',
});
setIsEditing(false);
Expand Down
4 changes: 2 additions & 2 deletions app/guests/newGuest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
View,
} from 'react-native';
import { colors } from '../../constants/colors';
import type { GuestsResponseBodyPost } from '../api/guests+api';
import type { GuestsResponseBodyPost } from '../api/guests/guests+api';

const styles = StyleSheet.create({
container: {
Expand Down Expand Up @@ -100,7 +100,7 @@ export default function NewGuest() {
<Pressable
style={({ pressed }) => [styles.button, { opacity: pressed ? 0.5 : 1 }]}
onPress={async () => {
const response = await fetch('/api/guests', {
const response = await fetch('/api/guests/guests', {
method: 'POST',
body: JSON.stringify({ firstName, lastName, attending: false }),
});
Expand Down
4 changes: 2 additions & 2 deletions app/notes/[noteId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useFocusEffect, useLocalSearchParams } from 'expo-router';
import { useCallback, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { colors } from '../../constants/colors';
import type { NoteResponseBodyGet } from '../api/[noteId]+api';
import type { NoteResponseBodyGet } from '../api/notes/[noteId]+api';

const styles = StyleSheet.create({
container: {
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function NotePage() {
return;
}

const response = await fetch(`/api/${noteId}`);
const response = await fetch(`/api/notes/${noteId}`);
const body: NoteResponseBodyGet = await response.json();

if ('note' in body) {
Expand Down
4 changes: 2 additions & 2 deletions app/notes/newNote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
View,
} from 'react-native';
import { colors } from '../../constants/colors';
import type { GuestsResponseBodyPost } from '../api/guests+api';
import type { GuestsResponseBodyPost } from '../api/guests/guests+api';

const styles = StyleSheet.create({
container: {
Expand Down Expand Up @@ -102,7 +102,7 @@ export default function NewNote() {
<Pressable
style={({ pressed }) => [styles.button, { opacity: pressed ? 0.5 : 1 }]}
onPress={async () => {
const response = await fetch('/api/notes', {
const response = await fetch('/api/notes/notes', {
method: 'POST',
body: JSON.stringify({ title, textContent }),
});
Expand Down
Loading

0 comments on commit 5e63643

Please sign in to comment.