Repository navigation
Add login and profile #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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { Router } from 'express'; | ||
| import type { Request, Response } from 'express'; | ||
|
|
||
| import { prisma } from '../prisma.js'; | ||
| import { getSession } from '../users/userService.js'; | ||
| import { meSelect } from '../utils/selects.js'; | ||
| import { bearerToken, verifyIdToken } from './tokens.js'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| /** | ||
| * POST /auth/login | ||
| * Header: Authorization: Bearer <Firebase ID token from Google sign-in> | ||
| * | ||
| * Creates the Roomie account on first sign-in and returns the session the app | ||
| * needs to decide where to go next: | ||
| * - no house + pendingInvites → show "join <house>?" | ||
| * - no house → onboarding (create a house or enter an invite code) | ||
| * - house → Home | ||
| */ | ||
| router.post('/login', async (req: Request, res: Response) => { | ||
| const identity = await verifyIdToken(bearerToken(req.headers.authorization)); | ||
|
|
||
| const existing = await prisma.user.findUnique({ | ||
| where: { firebaseUid: identity.firebaseUid }, | ||
| select: { id: true }, | ||
| }); | ||
|
|
||
| const user = existing | ||
| ? existing | ||
| : await prisma.user.create({ | ||
| data: { | ||
| firebaseUid: identity.firebaseUid, | ||
| email: identity.email, | ||
| netid: identity.netid, | ||
| name: identity.name, | ||
| avatarUrl: identity.avatarUrl, | ||
| }, | ||
| select: meSelect, | ||
| }); | ||
|
|
||
| const session = await getSession(user.id); | ||
| res.status(existing ? 200 : 201).json({ isNewUser: !existing, ...session }); | ||
| }); | ||
|
|
||
| export default router; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { getFirebaseAuth } from '../firebase.js'; | ||
| import { ForbiddenError, UnauthorizedError } from '../utils/AppError.js'; | ||
|
|
||
| export interface VerifiedIdentity { | ||
| firebaseUid: string; | ||
| email: string; | ||
| netid: string; | ||
| name: string; | ||
| avatarUrl?: string; | ||
| } | ||
|
|
||
| const CORNELL_SUFFIX = '@cornell.edu'; | ||
|
|
||
| /** Comma-separated full emails allowed besides @cornell.edu (e.g. a reviewer account). */ | ||
| const emailAllowlist = () => | ||
| new Set( | ||
| (process.env.EMAIL_ALLOWLIST ?? '') | ||
| .split(',') | ||
| .map((e) => e.trim().toLowerCase()) | ||
| .filter(Boolean), | ||
| ); | ||
|
|
||
| export const isDevBypassEnabled = () => | ||
| process.env.AUTH_DEV_BYPASS === 'true' && | ||
| process.env.NODE_ENV !== 'production'; | ||
|
|
||
| const netidFromEmail = (email: string) => email.split('@')[0]!.toLowerCase(); | ||
|
|
||
| /** | ||
| * Verifies a Firebase ID token (from Google sign-in on iOS/Android) and returns | ||
| * who it belongs to. Only Cornell accounts get through. | ||
| * | ||
| * Local development: with AUTH_DEV_BYPASS=true (never in production) you can send | ||
| * `Bearer dev:<netid>` instead of a real token. | ||
| */ | ||
| export const verifyIdToken = async ( | ||
| token: string, | ||
| ): Promise<VerifiedIdentity> => { | ||
| if (token.startsWith('dev:')) { | ||
| if (!isDevBypassEnabled()) { | ||
| throw new UnauthorizedError('Invalid token.'); | ||
| } | ||
| const netid = token.slice(4).toLowerCase(); | ||
| if (!/^[a-z]{1,4}\d{1,6}$/.test(netid)) { | ||
| throw new UnauthorizedError('Dev tokens look like dev:<netid>.'); | ||
| } | ||
| return { | ||
| firebaseUid: `dev-${netid}`, | ||
| email: `${netid}${CORNELL_SUFFIX}`, | ||
| netid, | ||
| name: netid, | ||
| }; | ||
| } | ||
|
|
||
| let decoded; | ||
| try { | ||
| decoded = await getFirebaseAuth().verifyIdToken(token); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject tokens for disabled users and revoked sessions.
|
||
| } catch { | ||
| throw new UnauthorizedError('Invalid or expired token.'); | ||
|
Comment on lines
+57
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Distinguish Firebase setup failures from invalid tokens. If the service-account file is missing or malformed,
|
||
| } | ||
|
|
||
| const email = decoded.email?.trim().toLowerCase(); | ||
| if (!email || !decoded.email_verified) { | ||
| throw new ForbiddenError('A verified email is required.'); | ||
| } | ||
| if (!email.endsWith(CORNELL_SUFFIX) && !emailAllowlist().has(email)) { | ||
| throw new ForbiddenError('Only Cornell accounts can use Roomie.'); | ||
| } | ||
|
|
||
| return { | ||
| firebaseUid: decoded.uid, | ||
| email, | ||
| netid: netidFromEmail(email), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| name: (decoded.name as string | undefined) ?? netidFromEmail(email), | ||
| avatarUrl: decoded.picture, | ||
| }; | ||
| }; | ||
|
|
||
| export const bearerToken = (header: string | undefined) => { | ||
| if (!header?.startsWith('Bearer ')) { | ||
| throw new UnauthorizedError('No token provided or wrong format.'); | ||
| } | ||
| const token = header.slice(7).trim(); | ||
| if (!token) { | ||
| throw new UnauthorizedError('No token provided.'); | ||
| } | ||
| return token; | ||
| }; | ||
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.
Script executed:
Repository: cuappdev/roomie-backend
Length of output: 4469
Script executed:
Repository: cuappdev/roomie-backend
Length of output: 7925
Allow the development-token bypass only in development and test.
The guard disables the bypass only for
NODE_ENV=production. Every other value enables it, includingdevelopmentfrom.env.example. WithAUTH_DEV_BYPASS=true, adev:<netid>token skips Firebase verification and returns an identity for any NetID that matches^[a-z]{1,4}\d{1,6}$(src/auth/tokens.ts:40-52). The image setsNODE_ENV=production, butdocker-compose.ymlloads.env, which overrides it at runtime. A server whose.envis copied from.env.exampleruns with the bypass active.Keep
testin the allowance. The Vitest config and CI setNODE_ENV=testwith the bypass enabled, and the auth tests depend on that.Suggested fix
Committable suggestion