-
Notifications
You must be signed in to change notification settings - Fork 1
Add Expo authentication and notes authorization workflow #1
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
Open
karlhorky
wants to merge
3
commits into
main
Choose a base branch
from
add-expo-auth-and-notes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Stack } from 'expo-router'; | ||
| import { colors } from '../../constants/theme'; | ||
|
|
||
| export default function AuthLayout() { | ||
| return ( | ||
| <Stack | ||
| screenOptions={{ | ||
| headerTintColor: colors.text, | ||
| headerStyle: { backgroundColor: colors.background }, | ||
| headerShadowVisible: false, | ||
| contentStyle: { backgroundColor: colors.background }, | ||
| }} | ||
| > | ||
| <Stack.Screen name="login" options={{ title: 'Login' }} /> | ||
| <Stack.Screen name="register" options={{ title: 'Register' }} /> | ||
| </Stack> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import crypto from 'node:crypto'; | ||
| import bcrypt from 'bcryptjs'; | ||
| import { createSessionInsecure } from '../../../database/sessions'; | ||
| import { getUserWithPasswordHashInsecure } from '../../../database/users'; | ||
| import { ExpoApiResponse } from '../../../ExpoApiResponse'; | ||
| import { | ||
| type User, | ||
| userSchemaLogin, | ||
| } from '../../../migrations/00002-createTableUsers'; | ||
| import { createSerializedSessionTokenCookie } from '../../../util/cookies'; | ||
| import { getCombinedErrorMessage } from '../../../util/validation'; | ||
|
|
||
| export type LoginResponseBodyPost = | ||
| | { | ||
| user: User; | ||
| } | ||
| | { | ||
| error: string; | ||
| }; | ||
|
|
||
| export async function POST( | ||
| request: Request, | ||
| ): Promise<ExpoApiResponse<LoginResponseBodyPost>> { | ||
| const requestBody = await request.json(); | ||
| const result = userSchemaLogin.safeParse(requestBody); | ||
|
|
||
| if (!result.success) { | ||
| return ExpoApiResponse.json( | ||
| { error: getCombinedErrorMessage(result.error.issues) }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| const userWithPasswordHash = await getUserWithPasswordHashInsecure( | ||
| result.data.user.username, | ||
| ); | ||
|
|
||
| if (!userWithPasswordHash) { | ||
| return ExpoApiResponse.json( | ||
| { error: 'Username or password invalid' }, | ||
| { status: 401 }, | ||
| ); | ||
| } | ||
|
|
||
| const isPasswordValid = await bcrypt.compare( | ||
| result.data.user.password, | ||
| userWithPasswordHash.passwordHash, | ||
| ); | ||
|
|
||
| userWithPasswordHash.passwordHash = ''; | ||
|
|
||
| if (!isPasswordValid) { | ||
| return ExpoApiResponse.json( | ||
| { error: 'Username or password invalid' }, | ||
| { status: 401 }, | ||
| ); | ||
| } | ||
|
|
||
| const sessionToken = crypto.randomBytes(100).toString('base64'); | ||
| const session = await createSessionInsecure( | ||
| sessionToken, | ||
| userWithPasswordHash.id, | ||
| ); | ||
|
|
||
| if (!session) { | ||
| return ExpoApiResponse.json( | ||
| { error: 'Session creation failed' }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
|
|
||
| return ExpoApiResponse.json( | ||
| { | ||
| user: { | ||
| id: userWithPasswordHash.id, | ||
| username: userWithPasswordHash.username, | ||
| }, | ||
| }, | ||
| { | ||
| headers: { | ||
| 'Set-Cookie': createSerializedSessionTokenCookie(session.token), | ||
| }, | ||
| }, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { parse } from 'cookie'; | ||
| import { deleteSession } from '../../../database/sessions'; | ||
| import { ExpoApiResponse } from '../../../ExpoApiResponse'; | ||
| import { deleteSerializedSessionTokenCookie } from '../../../util/cookies'; | ||
|
|
||
| export type LogoutResponseBodyPost = | ||
| | { | ||
| success: true; | ||
| } | ||
| | { | ||
| error: string; | ||
| }; | ||
|
|
||
| export async function POST( | ||
| request: Request, | ||
| ): Promise<ExpoApiResponse<LogoutResponseBodyPost>> { | ||
| const cookies = parse(request.headers.get('cookie') || ''); | ||
| const sessionToken = cookies.sessionToken; | ||
|
|
||
| if (!sessionToken) { | ||
| return ExpoApiResponse.json( | ||
| { error: 'Authentication required' }, | ||
| { status: 401 }, | ||
| ); | ||
| } | ||
|
|
||
| await deleteSession(sessionToken); | ||
|
|
||
| return ExpoApiResponse.json( | ||
| { success: true }, | ||
| { | ||
| headers: { | ||
| 'Set-Cookie': deleteSerializedSessionTokenCookie(), | ||
| }, | ||
| }, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import crypto from 'node:crypto'; | ||
| import bcrypt from 'bcryptjs'; | ||
| import { createSessionInsecure } from '../../../database/sessions'; | ||
| import { | ||
| createUserInsecure, | ||
| getUserInsecure, | ||
| } from '../../../database/users'; | ||
| import { ExpoApiResponse } from '../../../ExpoApiResponse'; | ||
| import { | ||
| type User, | ||
| userSchemaRegister, | ||
| } from '../../../migrations/00002-createTableUsers'; | ||
| import { createSerializedSessionTokenCookie } from '../../../util/cookies'; | ||
| import { getCombinedErrorMessage } from '../../../util/validation'; | ||
|
|
||
| export type RegisterResponseBodyPost = | ||
| | { | ||
| user: User; | ||
| } | ||
| | { | ||
| error: string; | ||
| }; | ||
|
|
||
| export async function POST( | ||
| request: Request, | ||
| ): Promise<ExpoApiResponse<RegisterResponseBodyPost>> { | ||
| const requestBody = await request.json(); | ||
| const result = userSchemaRegister.safeParse(requestBody); | ||
|
|
||
| if (!result.success) { | ||
| return ExpoApiResponse.json( | ||
| { error: getCombinedErrorMessage(result.error.issues) }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| if (await getUserInsecure(result.data.user.username)) { | ||
| return ExpoApiResponse.json( | ||
| { error: 'Username already exists' }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| const passwordHash = await bcrypt.hash(result.data.user.password, 12); | ||
| const user = await createUserInsecure(result.data.user.username, passwordHash); | ||
|
|
||
| if (!user) { | ||
| return ExpoApiResponse.json( | ||
| { error: 'Creating user failed' }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
|
|
||
| const sessionToken = crypto.randomBytes(100).toString('base64'); | ||
| const session = await createSessionInsecure(sessionToken, user.id); | ||
|
|
||
| if (!session) { | ||
| return ExpoApiResponse.json( | ||
| { error: 'Session creation failed' }, | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
|
|
||
| return ExpoApiResponse.json( | ||
| { user }, | ||
| { | ||
| headers: { | ||
| 'Set-Cookie': createSerializedSessionTokenCookie(session.token), | ||
| }, | ||
| }, | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because getUserWithPasswordHashInsecure is wrapped in react cache(), mutating the returned object (userWithPasswordHash.passwordHash = '') can have surprising effects if the cached value is reused within the same request. Prefer not mutating cached DB results; instead, avoid returning the password hash from this function (eg map/alias in SQL) or create a separate object for the response without modifying the fetched record.