Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
157 commits
Select commit Hold shift + click to select a range
0d55e99
[npm]: Bump resend from 4.0.0 to 4.0.1
dependabot[bot] Dec 30, 2024
d66f380
1.1.2
gearbox4026 Dec 30, 2024
14ae345
Merge branch 'main' into dependabot/npm_and_yarn/resend-4.0.1
renatodellosso Dec 31, 2024
d630055
Merge branch 'main' into dependabot/npm_and_yarn/resend-4.0.1
renatodellosso Dec 31, 2024
38c7727
Merge branch 'main' into dependabot/npm_and_yarn/resend-4.0.1
renatodellosso Jan 7, 2025
654c414
Install again
renatodellosso Jan 7, 2025
cd9ca56
Merge branch 'main' into dependabot/npm_and_yarn/resend-4.0.1
renatodellosso Jan 9, 2025
0887fe0
Merge pull request #389 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 9, 2025
eeffc48
Use HTTP if no ssl certs
renatodellosso Jan 9, 2025
dd46350
Merge branch 'main' of github.com:Decatur-Robotics/Gearbox
renatodellosso Jan 9, 2025
89829ff
Expose 80 instead of 443
renatodellosso Jan 9, 2025
85d2e04
Fix formatting in index
renatodellosso Jan 13, 2025
7f22a8d
[npm]: Bump typescript from 5.6.2 to 5.7.3
dependabot[bot] Jan 13, 2025
1411bd5
1.1.5
gearbox4026 Jan 13, 2025
4f8dd26
[npm]: Bump next from 15.1.2 to 15.1.4
dependabot[bot] Jan 13, 2025
7c6df78
[npm]: Bump eslint from 9.17.0 to 9.18.0
dependabot[bot] Jan 13, 2025
0e575f6
1.1.5
gearbox4026 Jan 13, 2025
aaf2de5
1.1.5
gearbox4026 Jan 13, 2025
1e48fa4
[npm]: Bump eslint-config-next from 15.0.3 to 15.1.4
dependabot[bot] Jan 13, 2025
67ae1aa
1.1.5
gearbox4026 Jan 13, 2025
9873423
[npm]: Bump tailwindcss from 3.3.6 to 3.4.17
dependabot[bot] Jan 13, 2025
bbac738
1.1.5
gearbox4026 Jan 13, 2025
ad75304
Merge pull request #402 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 13, 2025
fcc7ee3
Merge branch 'main' into dependabot/npm_and_yarn/next-15.1.4
renatodellosso Jan 13, 2025
8cd3fcd
Merge branch 'main' into dependabot/npm_and_yarn/eslint-9.18.0
renatodellosso Jan 13, 2025
d02bd63
Merge branch 'main' into dependabot/npm_and_yarn/eslint-config-next-1…
renatodellosso Jan 13, 2025
d4bd891
Merge branch 'main' into dependabot/npm_and_yarn/tailwindcss-3.4.17
renatodellosso Jan 13, 2025
f647ab6
Merge pull request #406 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 13, 2025
d029741
Merge pull request #405 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 13, 2025
302fd5d
Merge pull request #403 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 13, 2025
4d4f4a0
Merge branch 'main' into dependabot/npm_and_yarn/eslint-9.18.0
renatodellosso Jan 13, 2025
24fb93a
Merge pull request #404 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 13, 2025
c2eb198
Add log for incoming requests
renatodellosso Jan 13, 2025
c778321
Merge branch 'main' of github.com:Decatur-Robotics/Gearbox
renatodellosso Jan 13, 2025
ca0f6e4
Fix formatting
renatodellosso Jan 13, 2025
e67f495
Increment package version
renatodellosso Jan 13, 2025
8efe6f2
actually use http
renatodellosso Jan 13, 2025
943f121
Infer types for DbInterface
renatodellosso Jan 14, 2025
7b3ea49
1.1.7
gearbox4026 Jan 14, 2025
a66720b
Fix formatting
renatodellosso Jan 14, 2025
226e887
Merge branch 'use-mongo-anywhere' of github.com:Decatur-Robotics/Gear…
renatodellosso Jan 14, 2025
508a3cf
Use port 80 in docker-start
renatodellosso Jan 14, 2025
8486d91
Merge branch 'main' into use-mongo-anywhere
renatodellosso Jan 14, 2025
aaadad6
More formatting fixes
renatodellosso Jan 14, 2025
98d83b8
Merge branch 'use-mongo-anywhere' of github.com:Decatur-Robotics/Gear…
renatodellosso Jan 14, 2025
d7d0cfa
Converted DbInterfaces to mongo-anywhere
renatodellosso Jan 14, 2025
9a805a3
Remove unused tests and function for in memory db
renatodellosso Jan 14, 2025
402bfd0
Merge pull request #409 from Decatur-Robotics/use-mongo-anywhere
BanEvading Jan 16, 2025
c88eb56
Log inbound requests
renatodellosso Jan 16, 2025
e6cf3a8
Use https bool
renatodellosso Jan 16, 2025
350eaf5
Merge branch 'main' of github.com:Decatur-Robotics/Gearbox
renatodellosso Jan 16, 2025
54504f7
Fix formatting
renatodellosso Jan 16, 2025
c422d20
Add dockerignore, rename enviroment, and remove gitpod
renatodellosso Jan 17, 2025
67bbd46
Bump package versions in package-lock.json to 1.1.8 and update depend…
renatodellosso Jan 17, 2025
53e597e
Bump version to 1.1.9
renatodellosso Jan 17, 2025
601b572
Save build from ci to reuse in cd
renatodellosso Jan 17, 2025
d4a0498
Bump unified-api
renatodellosso Jan 17, 2025
0674ccc
Bump version to 1.1.11 and update unified-api dependencies
renatodellosso Jan 17, 2025
6ee86d4
Bump version to 1.1.12 and update unified-api-nextjs and unified-api …
renatodellosso Jan 17, 2025
7968244
Bump unified-api
renatodellosso Jan 18, 2025
4b27054
Add .env.production
renatodellosso Jan 18, 2025
901f7ca
Null check in initial event data
renatodellosso Jan 18, 2025
1e48a83
[npm]: Bump next from 15.1.4 to 15.1.5
dependabot[bot] Jan 20, 2025
e9c9354
[npm]: Bump eslint-config-next from 15.1.4 to 15.1.5
dependabot[bot] Jan 20, 2025
737395f
1.1.13
gearbox4026 Jan 20, 2025
87428f8
[npm]: Bump next-auth from 4.24.10 to 4.24.11
dependabot[bot] Jan 20, 2025
65bf1df
1.1.13
gearbox4026 Jan 20, 2025
3348b07
[npm]: Bump @types/node from 22.10.2 to 22.10.7
dependabot[bot] Jan 20, 2025
9593555
1.1.13
gearbox4026 Jan 20, 2025
ab71f2a
[npm]: Bump resend from 4.0.1 to 4.1.1
dependabot[bot] Jan 20, 2025
bd5408e
1.1.13
gearbox4026 Jan 20, 2025
846fc32
1.1.13
gearbox4026 Jan 20, 2025
c3c8a3d
Merge pull request #412 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 20, 2025
23d56e4
Merge pull request #413 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 20, 2025
77e7f5d
Merge pull request #415 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 20, 2025
26c706c
Merge pull request #416 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 20, 2025
7239248
Merge branch 'main' into dependabot/npm_and_yarn/next-auth-4.24.11
renatodellosso Jan 20, 2025
a5aa2b5
Merge pull request #414 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 20, 2025
c54460a
Remove allow transparency
renatodellosso Jan 20, 2025
5f656c3
Refactor Homepage component: simplify number formatting and update JS…
renatodellosso Jan 20, 2025
3539a84
Add Gearbox version to footer
renatodellosso Jan 24, 2025
9a41e1c
Rename package variable
renatodellosso Jan 24, 2025
506afe4
Get started links to sign in if user isn't signed in. Closes #301
renatodellosso Jan 24, 2025
7099840
Add /dev/ subroute
renatodellosso Jan 24, 2025
ec2c9c3
Fix pamphlet import
renatodellosso Jan 24, 2025
947f754
Added /dev/leveling page
renatodellosso Jan 24, 2025
1e0baec
Format leveling page
renatodellosso Jan 24, 2025
d780719
Changed API failure text
renatodellosso Jan 24, 2025
d1fd433
Update README.md
renatodellosso Jan 25, 2025
766821b
Added tests for Utils.ts
renatodellosso Jan 27, 2025
d820202
Fix formatting for Utils.ts tests
renatodellosso Jan 27, 2025
c50bfa2
Added tests for TOA
renatodellosso Jan 27, 2025
a3bfc7e
[npm]: Bump eslint-config-next from 15.1.5 to 15.1.6
dependabot[bot] Jan 27, 2025
1cc44da
[npm]: Bump postcss from 8.4.49 to 8.5.1
dependabot[bot] Jan 27, 2025
137e03e
1.1.15
gearbox4026 Jan 27, 2025
63e8a11
1.1.15
gearbox4026 Jan 27, 2025
e73798f
[npm]: Bump next from 15.1.5 to 15.1.6
dependabot[bot] Jan 27, 2025
cc3f077
[npm]: Bump react-hot-toast from 2.4.1 to 2.5.1
dependabot[bot] Jan 27, 2025
81bed9d
1.1.15
gearbox4026 Jan 27, 2025
7a9a28c
1.1.15
gearbox4026 Jan 27, 2025
61eb27f
Add tests for FakeData.ts
renatodellosso Jan 27, 2025
e0ae141
Format FakeData.test.ts
renatodellosso Jan 27, 2025
f3e48c8
Add tests for Xp.ts
renatodellosso Jan 27, 2025
1121244
Add tests for competition handling
renatodellosso Jan 27, 2025
7856814
Formatting fixes
renatodellosso Jan 27, 2025
b4a5325
Add more tests for CompetitionHandling.ts
renatodellosso Jan 28, 2025
3ce59f3
Fix formatting
renatodellosso Jan 28, 2025
48c36bb
Fix team creation page
renatodellosso Jan 29, 2025
aef2963
Refactor team name update logic in CreateTeam component
renatodellosso Jan 29, 2025
acfa336
Merge branch 'main' into dependabot/npm_and_yarn/eslint-config-next-1…
renatodellosso Jan 29, 2025
dc2c918
Merge pull request #419 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 31, 2025
fed2639
Merge pull request #420 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 31, 2025
d67446e
Merge branch 'main' into dependabot/npm_and_yarn/next-15.1.6
renatodellosso Jan 31, 2025
21bf97b
Merge pull request #423 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 31, 2025
84a7d61
Merge branch 'main' into dependabot/npm_and_yarn/next-15.1.6
renatodellosso Jan 31, 2025
4ef75de
Merge pull request #422 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Jan 31, 2025
5fea226
Fixed bug with sign in with email
renatodellosso Feb 1, 2025
b9087a1
Merge branch 'main' of github.com:Decatur-Robotics/Gearbox
renatodellosso Feb 1, 2025
753e064
Add repairUser function
renatodellosso Feb 1, 2025
5d9346c
Add user repair scripts and update README with usage instructions
renatodellosso Feb 1, 2025
6e66364
Add getCommandLineInput function for user prompts in repairUsers script
renatodellosso Feb 1, 2025
fc2bf67
Fix formatting
renatodellosso Feb 1, 2025
fecac39
Fix build
renatodellosso Feb 1, 2025
c659a2e
Fix formatting in Utils
renatodellosso Feb 1, 2025
a4a6e80
More logging for repairUsers script
renatodellosso Feb 1, 2025
3eb8cf1
Fix formatting in repairUsers script
renatodellosso Feb 1, 2025
f36896a
Even more logging repairUsers script
renatodellosso Feb 1, 2025
5efb0d7
JSON output in repair users script
renatodellosso Feb 1, 2025
9dbe1d2
Yet more logging repairUsers script
renatodellosso Feb 1, 2025
afe52f1
Added conversion of ID to ObjectId in repairUser
renatodellosso Feb 1, 2025
3f77be6
Fixed repairUser call in Auth.ts not working on new email sign in use…
renatodellosso Feb 1, 2025
5251198
Formatting
renatodellosso Feb 1, 2025
141c386
Email sign in session stopped working, then started working again. I …
renatodellosso Feb 1, 2025
1dbbee4
Cleaned up my code from last night
renatodellosso Feb 1, 2025
ad344f4
Fix formatting
renatodellosso Feb 1, 2025
a3db7b7
Add duration for request logs
renatodellosso Feb 1, 2025
58bc14f
Formatting index.ts
renatodellosso Feb 1, 2025
c6bd8a5
1.1.16
renatodellosso Feb 1, 2025
2ccd9fe
[npm]: Bump resend from 4.1.1 to 4.1.2
dependabot[bot] Feb 3, 2025
56961cc
1.1.17
gearbox4026 Feb 3, 2025
26f9a2b
[npm]: Bump @types/node from 22.10.7 to 22.13.0
dependabot[bot] Feb 3, 2025
aa1acea
1.1.17
gearbox4026 Feb 3, 2025
690f50b
Merge pull request #430 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Feb 3, 2025
b744896
Merge pull request #427 from Decatur-Robotics/dependabot/npm_and_yarn…
renatodellosso Feb 3, 2025
e3a099c
CD for AWS
renatodellosso Feb 3, 2025
22276c8
Merge branch 'main' of github.com:Decatur-Robotics/Gearbox
renatodellosso Feb 3, 2025
c9344be
Changed environment to Production for CD
renatodellosso Feb 3, 2025
e525179
Formatting tweaks
renatodellosso Feb 3, 2025
4d49b89
Bumped version, decreased wait accuracy, removed repairUser console.log
renatodellosso Feb 4, 2025
2db79c7
Remove Session User log
renatodellosso Feb 4, 2025
210c001
Update version to 1.1.18 and add LocalStorageDbInterface for managing…
renatodellosso Feb 5, 2025
3c67d5c
Refactor LocalStorageDb component for improved readability and format…
renatodellosso Feb 5, 2025
f5706e9
Added demo page for LocalStorageDb
renatodellosso Feb 6, 2025
df8d0c3
1.1.19
gearbox4026 Feb 6, 2025
eb30661
Format db demo page
renatodellosso Feb 6, 2025
850cb4f
Merge branch 'local-storage-db-interface' of github.com:Decatur-Robot…
renatodellosso Feb 6, 2025
31e988f
Merge pull request #435 from Decatur-Robotics/local-storage-db-interface
renatodellosso Feb 6, 2025
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
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules
tests
.git
.github
.next
.swc
.vscode
coverage
certs
.env
.env.test
tsconfig.tsbuildinfo
5 changes: 5 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Only put NEXT_PUBLIC variables here!
# We have to include these at build time, so this file is used to inject them into the build process.
NEXT_PUBLIC_API_URL=/api/
NEXT_PUBLIC_SLACK_CLIENT_ID=10831824934.7404945710466
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID=G-1BFJYBDC76
6 changes: 5 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
NEXT_PUBLIC_API_URL=http://localhost:3000/api

DEVELOPER_EMAILS=["[email protected]"]
DEVELOPER_EMAILS=["[email protected]"]

TOA_URL=https://example.com
TOA_APP_ID=123
TOA_KEY=456
37 changes: 25 additions & 12 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
name: CD

on: [workflow_call, workflow_dispatch]
on:
workflow_call:
secrets:
AWS_ACCESS_KEY_ID:
required: true
AWS_SECRET_ACCESS_KEY:
required: true
workflow_dispatch:

permissions:
packages: write

jobs:
build_and_push:
push:
runs-on: ubuntu-latest
environment: Test
environment: Production
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and export
uses: docker/build-push-action@v6
env:
DOCKER_BUILD_SUMMARY: false
- name: Download artifact
uses: actions/download-artifact@v4
with:
tags: ghcr.io/decatur-robotics/gearbox:latest
outputs: type=docker,dest=/tmp/gearbox.tar
context: . # Needed for Docker to find files made during the workflow

name: gearbox
path: /tmp

- name: Load image
run: |
docker load --input /tmp/gearbox.tar
Expand All @@ -34,4 +38,13 @@
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u decatur-robotics --password-stdin

- name: Push to GHCR
run: docker push ghcr.io/decatur-robotics/gearbox:latest
run: docker push ghcr.io/decatur-robotics/gearbox:latest

- name: Install AWS CLI
uses: unfor19/install-aws-cli-action@v1

Check warning

Code scanning / CodeQL

Unpinned tag for a non-immutable Action in workflow Medium

Unpinned 3rd party Action 'CD' step
Uses Step
uses 'unfor19/install-aws-cli-action' with ref 'v1', not a pinned commit hash

- name: Configure AWS
run: aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} && aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} && aws configure set region us-east-1

- name: Deploy to ECS
run: aws ecs update-service --cluster gearbox --service gearbox --force-new-deployment
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ jobs:
with:
tags: ghcr.io/decatur-robotics/gearbox:latest
outputs: type=docker,dest=/tmp/gearbox.tar

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: gearbox
path: /tmp/gearbox.tar

unit_test:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/onpush.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ jobs:
needs:
- ci
uses: ./.github/workflows/cd.yml
secrets:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
11 changes: 0 additions & 11 deletions .gitpod.yml

This file was deleted.

4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ LABEL org.opencontainers.image.licenses=CC-BY-NC-SA-4.0
WORKDIR /app

COPY package*.json ./
RUN npm i
RUN npm ci

COPY . .

RUN npm run build

EXPOSE 443
EXPOSE 80

# ENTRYPOINT [ "bash" ] # Uncomment to operate the terminal in the container
CMD ["/usr/local/bin/npm", "run", "start"]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ Features full feature parity with SJ2, whilst remaining simpler, faster and cool
1. Run `npm run build`
1. Run `npm run start`

##### Deployment

See the [Gearbox-Terraform](https://github.com/Decatur-Robotics/Gearbox-Terraform) repo.

#### Tests

1. Run `npm run test`

#### Scripts

There's a few scripts in the /scripts folder that can be run with `npx tsx scripts/<script_name>.ts`.

## Contributing

You've made it past set up and are ready to contibure to the future of scouting - here's how.
Expand Down
3 changes: 2 additions & 1 deletion components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ export default function Footer() {
</nav>
<nav className="max-sm:pl-8">
<h6 className="footer-title">Debug</h6>
<div>Version {process.env.NEXT_PUBLIC_GEARBOX_VERSION}</div>
<div>
Version{" "}
Build Time:{" "}
{(() => {
const timestamp = new Date(+process.env.NEXT_PUBLIC_BUILD_TIME);
return timestamp.toDateString() + " " + timestamp.toTimeString();
Expand Down
1 change: 1 addition & 0 deletions enviroment.d.ts → environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ declare global {
NEXT_PUBLIC_FORCE_OFFLINE_MODE: string;

NEXT_PUBLIC_BUILD_TIME: string;
NEXT_PUBLIC_GEARBOX_VERSION: string;

DEVELOPER_EMAILS: string;

Expand Down
64 changes: 42 additions & 22 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,67 @@
import { join } from "path";
import { createServer } from "https";
import { createServer as createServerHttps } from "https";
import { parse } from "url";
import next from "next";
import fs, { existsSync, readFileSync } from "fs";
import { IncomingMessage, ServerResponse, request } from "http";
import {
IncomingMessage,
ServerResponse,
request,
createServer as createServerHttp,
} from "http";

console.log("Starting server...");

const dev = process.env.NODE_ENV !== "production";
const port = 443;
const app = next({ dev, port });
const handle = app.getRequestHandler();

console.log("Constants set");

const httpsOptions =
existsSync("./certs/key.pem") && existsSync("./certs/cert.pem")
? {
key: readFileSync("./certs/key.pem"),
cert: readFileSync("./certs/cert.pem"),
}
: {};
const useHttps =
existsSync("./certs/key.pem") && existsSync("./certs/cert.pem");

const httpsOptions = useHttps
? {
key: readFileSync("./certs/key.pem"),
cert: readFileSync("./certs/cert.pem"),
}
: {};

console.log("HTTPS options set");
const port = useHttps ? 443 : 80;
console.log(`Using port ${port}`);

const app = next({ dev, port });
const handle = app.getRequestHandler();

console.log("App preparing...");
app.prepare().then(() => {
console.log("App prepared. Creating server...");

async function handleRaw(
req: IncomingMessage,
res: ServerResponse<IncomingMessage>,
) {
const start = Date.now();
console.log(`IN: ${req.method} ${req.url}`);
if (!req.url) return;

const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl).then(() =>
console.log(
`OUT: ${req.method} ${req.url} ${res.statusCode} in ${Date.now() - start}ms`,
),
);
}

try {
const server = createServer(
httpsOptions,
async (req: IncomingMessage, res: ServerResponse<IncomingMessage>) => {
if (!req.url) return;

const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
},
const server = (
useHttps
? createServerHttps(httpsOptions, handleRaw)
: createServerHttp(handleRaw)
)
.listen(port, () => {
console.log(
process.env.NODE_ENV +
" HTTPS Server Running At: https://localhost:" +
` Server Running At: ${useHttps ? "https" : "http"}://localhost:` +
port,
);
})
Expand Down
66 changes: 29 additions & 37 deletions lib/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
import { getDatabase, clientPromise } from "./MongoDB";
import { ObjectId } from "bson";
import { User } from "./Types";
import { GenerateSlug } from "./Utils";
import { GenerateSlug, repairUser } from "./Utils";
import { Analytics } from "@/lib/client/Analytics";
import Email from "next-auth/providers/email";
import ResendUtils from "./ResendUtils";
import CollectionId from "./client/CollectionId";
import { AdapterUser } from "next-auth/adapters";
import { wait } from "./client/ClientUtils";

var db = getDatabase();

Expand Down Expand Up @@ -101,49 +102,40 @@ export const AuthenticationOptions: AuthOptions = {
return baseUrl + "/onboarding";
},

/**
* For email sign in, runs when the "Sign In" button is clicked (before email is sent).
*/
async signIn({ user }) {
Analytics.signIn(user.name ?? "Unknown User");

let typedUser = user as Partial<User>;
if (!typedUser.slug) {
console.log(
"User is incomplete, filling in missing fields. User:",
typedUser,
);
if (!typedUser.slug || typedUser._id?.toString() != typedUser.id) {
const repairUserOnceItIsInDb = async () => {
console.log(
"User is incomplete, waiting for it to be in the database.",
);
let foundUser: User | undefined = undefined;
while (!foundUser) {
foundUser = await (
await db
).findObject(CollectionId.Users, { email: typedUser.email });

const name =
typedUser.name ?? typedUser.email?.split("@")[0] ?? "Unknown User";
if (!foundUser) await wait(50);
}

const id = typedUser._id ?? new ObjectId();
console.log(
"User is incomplete, filling in missing fields. User:",
typedUser,
);

// User is incomplete, fill in the missing fields
typedUser = {
_id: id,
id: id.toString(),
name,
image: typedUser.image ?? "https://4026.org/user.jpg",
slug: await GenerateSlug(
await getDatabase(),
CollectionId.Users,
name,
),
teams: typedUser.teams ?? [],
owner: typedUser.owner ?? [],
slackId: typedUser.slackId ?? "",
onboardingComplete: typedUser.onboardingComplete ?? false,
admin: typedUser.admin ?? false,
xp: typedUser.xp ?? 0,
level: typedUser.level ?? 0,
...typedUser,
} as User;

await (
await db
).updateObjectById(
CollectionId.Users,
new ObjectId(typedUser._id?.toString()),
typedUser,
);
typedUser._id = foundUser._id;

typedUser = await repairUser(await db, typedUser);

console.log("User updated:", typedUser);
};

repairUserOnceItIsInDb();
}

new ResendUtils().createContact(typedUser as User);
Expand Down
Loading
Loading