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

feat: implement dev bypass #730

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 5 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ const fetch = require('node-fetch');
const checkValidatorAvailability = async () => {
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_VALIDATOR}/health`);
if (response.ok) {
return true;
} else {
return false;
}
return response.ok;
} catch (error) {
return false;
}
Expand Down
112 changes: 98 additions & 14 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@vercel/analytics": "^0.1.11",
"async-mutex": "^0.4.0",
"axios": "^1.3.4",
"cookies": "^0.8.0",
"eslint": "^8.8.0",
"eslint-plugin-unused-imports": "^2.0.0",
"framer-motion": "^6.2.3",
Expand All @@ -72,6 +73,7 @@
"devDependencies": {
"@babel/core": "^7.20.12",
"@jest/globals": "^29.6.1",
"@types/cookies": "^0.7.9",
"@types/node": "^17.0.14",
"@types/nprogress": "^0.2.0",
"@types/react": "^17.0.39",
Expand Down
38 changes: 29 additions & 9 deletions prisma/seedTestUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ export interface SeededUserData {
}

export const seedTestUser = async (prisma: PrismaClient): Promise<SeededUserData> => {
const uuid = '00000000-0000-0000-0000-000000000000';
// Create user
const user = await prisma.user.create({
data: {
const user = await prisma.user.upsert({
where: {
email: '[email protected]',
},
update: {
id: uuid,
},
create: {
id: uuid,
email: '[email protected]',
onboardingComplete: true,
seenHomeOnboardingModal: true,
Expand All @@ -23,8 +31,15 @@ export const seedTestUser = async (prisma: PrismaClient): Promise<SeededUserData
// Create profile
const [profile, account, session] = await Promise.all([
// Create profile
prisma.profile.create({
data: {
prisma.profile.upsert({
where: {
userId: user.id,
},
update: {
id: uuid,
},
create: {
id: uuid,
name: 'Test User',
startYear: 2021,
startSemester: 's',
Expand All @@ -34,11 +49,16 @@ export const seedTestUser = async (prisma: PrismaClient): Promise<SeededUserData
},
}),
// Create account
prisma.account.create({
data: {
type: 'test',
provider: 'test',
providerAccountId: 'test123',
prisma.account.upsert({
where: {
id: uuid,
},
update: {},
create: {
id: uuid,
type: 'test_bypass',
provider: 'test_bypass',
providerAccountId: 'test123_bypass',
userId: user.id,
},
}),
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export const authOptions: NextAuthOptions = {
},
from: env.EMAIL_FROM,
}),
// ...add more providers here
],
pages: {
signIn: '/auth/login',
signOut: '/',
newUser: '/app/onboarding',
},
};

export default NextAuth(authOptions);
29 changes: 27 additions & 2 deletions src/pages/app/home.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
import { createServerSideHelpers } from '@trpc/react-query/server';
import Cookies from 'cookies';
import { GetServerSidePropsContext } from 'next';
import { getServerSession } from 'next-auth';
import superjson from 'superjson';

import { env } from '@/env/client.mjs';
import { appRouter } from '@/server/trpc/router/_app';
import { createContextInner } from '@server/trpc/context';
import { seedTestUser } from 'prisma/seedTestUser';

import Home from '../../components/home/Home';
import { prisma } from '../../server/db/client';
import { authOptions } from '../api/auth/[...nextauth]';

export async function getServerSideProps(context: GetServerSidePropsContext) {
const session = await getServerSession(context.req, context.res, authOptions);
let serverSession = await getServerSession(context.req, context.res, authOptions);
if (!serverSession && env.NEXT_PUBLIC_NODE_ENV === 'development') {
// bypass login using test user for convenience
const { user, session } = await seedTestUser(prisma);
serverSession = {
user: {
id: session.userId,
email: user.email,
},
expires: new Date(session.expires).toISOString(),
};
const cookies = new Cookies(context.req, context.res);
cookies.set('next-auth.session-token', session.sessionToken, {
domain: 'localhost',
// session is not actually a 'Date' object here, it's a string
expires: new Date(session.expires),
httpOnly: true,
path: '/',
secure: false,
});
}
const ssg = createServerSideHelpers({
router: appRouter,
ctx: await createContextInner({ session }),
ctx: await createContextInner({ session: serverSession }),
transformer: superjson,
});

Expand Down
Loading
Loading