Skip to content

Commit e6c282d

Browse files
committed
[optimize] auto load Environment variables
[fix] 4 Database detail bugs
1 parent 5bda9ff commit e6c282d

File tree

11 files changed

+376
-34
lines changed

11 files changed

+376
-34
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ node_modules/
22
package-lock.json
33
dist/
44
type/*.d.ts
5-
.env
5+
.env*
66
.data/
77
.vscode/settings.json

ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
| Name | Usage |
4848
| :------------: | :--------------------------: |
49-
| `APP_SECRET` | encrypt Password & Token |
49+
| `JWT_SECRET` | encrypt Password & Token |
5050
| `DATABASE_URL` | PostgreSQL connection string |
5151

5252
## Development

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ services:
99
postgres:
1010
image: postgres
1111
environment:
12-
- POSTGRES_PASSWORD=${APP_SECRET}
12+
- POSTGRES_PASSWORD=${JWT_SECRET}
1313
volumes:
1414
- database-data:/var/lib/postgresql/data/
1515
networks:
@@ -21,7 +21,7 @@ services:
2121
image: kaiyuanshe/openhackathon-service
2222
environment:
2323
- NODE_ENV=production
24-
- DATABASE_URL=postgres://postgres:${APP_SECRET}@postgres:5432/postgres
24+
- DATABASE_URL=postgres://postgres:${JWT_SECRET}@postgres:5432/postgres
2525
- PORT=8080
2626
networks:
2727
- kaiyuanshe

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"dependencies": {
2121
"@koa/cors": "^5.0.0",
2222
"@koa/multer": "^3.0.2",
23-
"@koa/router": "^12.0.1",
23+
"@koa/router": "^13.0.0",
2424
"@octokit/openapi-types": "^22.2.0",
2525
"class-transformer": "^0.5.1",
2626
"class-validator": "^0.14.1",
@@ -54,6 +54,7 @@
5454
"@typescript-eslint/parser": "^8.1.0",
5555
"eslint": "^8.57.0",
5656
"eslint-plugin-simple-import-sort": "^12.1.1",
57+
"get-git-folder": "^0.1.2",
5758
"husky": "^9.1.4",
5859
"lint-staged": "^15.2.9",
5960
"prettier": "^3.3.3",
@@ -72,6 +73,7 @@
7273
},
7374
"scripts": {
7475
"prepare": "husky || true",
76+
"install": "get-git-folder https://github.com/kaiyuanshe/service-configuration main OpenHackathon-Web || true",
7577
"dev": "ts-node-dev source/",
7678
"test": "lint-staged",
7779
"build": "rm -rf dist/ type/*.d.ts && tsc && mv dist/model/*.d.ts type/",

pnpm-lock.yaml

Lines changed: 351 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/controller/OAuth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export class OauthController {
1616
const response = await fetch('https://api.github.com/user', {
1717
headers: { Authorization: `Bearer ${accessToken}` }
1818
});
19-
const { email, name, avatar_url } =
19+
const { email, login, avatar_url } =
2020
(await response.json()) as GitHubUser;
2121
const user =
2222
(await store.findOneBy({ email })) ||
2323
(await UserController.signUp({ email, password: accessToken }));
24-
const newProfile = { name, avatar_url },
24+
const newProfile = { name: login, avatar: avatar_url },
2525
oldPofile = { name: user.name, avatar: user.avatar };
2626

2727
if (!isDeepStrictEqual(oldPofile, newProfile)) {

source/controller/User.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
UserFilter,
2828
UserListChunk
2929
} from '../model';
30-
import { APP_SECRET, searchConditionOf } from '../utility';
30+
import { JWT_SECRET, searchConditionOf } from '../utility';
3131
import { ActivityLogController } from './ActivityLog';
3232

3333
const store = dataSource.getRepository(User);
@@ -36,23 +36,23 @@ const store = dataSource.getRepository(User);
3636
export class UserController {
3737
static encrypt = (raw: string) =>
3838
createHash('sha1')
39-
.update(APP_SECRET + raw)
39+
.update(JWT_SECRET + raw)
4040
.digest('hex');
4141

4242
static sign = (user: User): User => ({
4343
...user,
44-
token: sign({ ...user }, APP_SECRET)
44+
token: sign({ ...user }, JWT_SECRET)
4545
});
4646

47-
static async signUp(data: SignInData) {
47+
static async signUp({ email, password }: SignInData) {
4848
const sum = await store.count();
4949

50-
const { password, ...user } = await store.save(
51-
Object.assign(new User(), data, {
52-
password: UserController.encrypt(data.password),
53-
roles: [sum ? Role.Client : Role.Administrator]
54-
})
55-
);
50+
const { password: _, ...user } = await store.save({
51+
name: email,
52+
email,
53+
password: UserController.encrypt(password),
54+
roles: [sum ? Role.Client : Role.Administrator]
55+
});
5656
await ActivityLogController.logCreate(user, 'User', user.id);
5757

5858
return user;

source/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import {
1414
UserController
1515
} from './controller';
1616
import { dataSource } from './model';
17-
import { APP_SECRET, isProduct, PORT } from './utility';
17+
import { JWT_SECRET, isProduct, PORT } from './utility';
1818

1919
const HOST = `localhost:${PORT}`,
2020
app = new Koa()
2121
.use(KoaLogger())
2222
.use(swagger({ exposeSpec: true }))
23-
.use(jwt({ secret: APP_SECRET, passthrough: true }));
23+
.use(jwt({ secret: JWT_SECRET, passthrough: true }));
2424

2525
if (!isProduct) app.use(mocker());
2626

source/model/Hackathon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class Hackathon extends UserBase {
8686
@Min(0)
8787
@VirtualColumn({
8888
query: alias =>
89-
`SELECT COUNT(*) FROM "Enrollment" WHERE hackathonId = ${alias}.id`
89+
`SELECT COUNT(*) FROM "enrollment" WHERE enrollment.hackathonId = ${alias}.id`
9090
})
9191
enrollment: number;
9292

source/model/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionO
55
import { DATABASE_URL, isProduct } from '../utility';
66
import { User } from './User';
77
import { PlatformAdmin } from './PlatformAdmin';
8-
import { ActivityLog } from './ActivityLog';
8+
import { ActivityLog, UserRank } from './ActivityLog';
99
import { Hackathon } from './Hackathon';
1010
import { Staff } from './Staff';
1111
import { Organizer } from './Organizer';
@@ -35,6 +35,7 @@ const commonOptions: Pick<
3535
User,
3636
PlatformAdmin,
3737
ActivityLog,
38+
UserRank,
3839
Hackathon,
3940
Staff,
4041
Organizer,

0 commit comments

Comments
 (0)