Table of Contents
CodeHive aims to be a tool to help aspiring developers get fast, reliable, and contextual feedback on their questions. This is achieved by enabling seamless project integration and providing holistic context to related questions.
- npm
npm install npm@latest -g
- Clone the repo
git clone https://github.com/Spark-Project-Pulse/frontend.git
- Install NPM packages
npm install
See the following article for a detailed explanation of how to manage environment variables in a Next.js project: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
- TLDR:
- Environment variables are NOT to be used as secrets. To add a secret to the project, use the get_secret function in your code and follow these directions to add the secret to Google Secret Manager.
- To add an environment variable to the project (for example, anything with the name
NEXT_PUBLIC_*), add the development value of the variable to.env.developmentin the formatVARIABLE_NAME=valueand the production value to.env.productionin the formatVARIABLE_NAME=value. The variable will be available in the project asprocess.env.VARIABLE_NAME.
- Go to the Google Cloud Console, navigate to
IAM & Admin>Service Accounts, and and download the already existing json key from the service acount called Secret Accessor Service Account. Go to theKeystab and clickAdd KeythenCreate New KeythenCreate(use json format). Your key will download aspulse-random-letters-and-numbers.jsonin the root of the project. - Add a
.env.localfile to the root of the project with the following contents:
GOOGLE_CLOUD_PROJECT=google_cloud_project_id
GOOGLE_APPLICATION_CREDENTIALS=pulse-random-letters-and-numbers.json- server-side code: Use the
getSecretfunction insrc/lib/getSecret.tsto access secrets stored in Google Secret Manager locally. The function takes the secret name as an argument and returns the secret value.- (you should ONLY use this function in server-side functions labelled with
'user-server')
- (you should ONLY use this function in server-side functions labelled with
- client-side code: Use the
getSecretsfunction insrc/api/getSecrets.tswhich is a Next.js endpoint that is called on the server (since thegetSecretfunction requires server-side dependencies)- (you should ONLY use this function in client-side functions labelled with
'use-client'or no label)
- (you should ONLY use this function in client-side functions labelled with
To ensure they will be available in production and consistent across all environments and between developers, secrets should be stored in Google Secret Manager. To update a secret:
- Navigate to the Google Cloud Console
- Select the project
Pulse - Navigate to
Secret Manager - Select the secret you want to update
- Click
Deleteto delete the secret - Now recreate the secret with the new value (I am aware this is kind of a pain, but I haven't researched how versions work yet)
For the frontend, there are only two kinds of secrets: local and production. When creating a new secret, make sure to add the production version by appending _PRODUCTION to the secret name and the local version by appending _LOCAL. Even if the secret is the same for both environments, it should be added twice to ensure that the secret is available in both environments. The getSecret function handles the logic of which secret to return based on the environment, so after adding the secret to Google Secret Manager, it should be accessible via the function.
- Navigate to the Google Cloud Console
- Select the project
Pulse - Navigate to
Secret Manager - Select the secret you want to update
- Click
Create Secret - Enter the secret's name and value
- Click
Create Secret
- Make sure the Docker daemon is running (open Docker Desktop)
- Navigate to the frontend repository
- Use this command to build and run the backend container:
docker compose up --build
- Navigate to
http://localhost:3000to see an example of an API response
- Make sure the Docker daemon is running (open Docker Desktop)
- Navigate to the frontend repository
- Make sure the backend repository is in the same directory as the frontend repository
- Use this command to run the project locally:
docker compose --profile frontend-and-backend up --build
- This will run the frontend and backend in separate containers
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun devOpen http://localhost:3000 with your browser to see the result.
Since we are using Supabase Auth, there are two types of user-schemas that we have access to (see backend models.py for more details on AuthUser and our custom User).
In general, we will want to use the custom User schema to get important information about the user that needs to be rendered by the client. HOWEVER, in order to do so, we first need to use Supabase's getUser function to get the user's id. You can simply use the useUser context to auto-"magically" fetch the user:
import { useUser } from '@/app/contexts/UserContext'
export default function YourComponent () {
const { user, loading } = useUser()
...
}So far, it seems that the main use for getting the AuthUser in server-side code (i.e. functions/files in src/api) would be to pass in the active user_id to the backend API. This is useful for creating objects associated with that user. To do so, you should use the getSupaUser function from utils/supabase/server. Here is an example for uploading projects:
import { getSupaUser } from '@/utils/supabase/server';
export const createProject = async (values: {
public: boolean;
title: string;
description: string;
}): Promise<ApiResponse<{ project_id: string }>> => {
try {
const user = await getSupaUser()
const vals = { owner: user?.id, ...values }
...See the open issues for a full list of proposed features (and known issues).
Distributed under the MIT License. See LICENSE.txt for more information.
CodeHive - [email protected]
Project Link: https://github.com/Spark-Project-Pulse/backend
- CodeHive Team
