Skip to content

Commit cd31e9b

Browse files
authored
Merge pull request #94 from ToastedDev/dev
2 parents 15ec20d + 402544b commit cd31e9b

File tree

19 files changed

+1164
-126
lines changed

19 files changed

+1164
-126
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
DISCORD_TOKEN='TOKEN'
22
DISCORD_TOKEN_DEV='DEV_TOKEN'
3+
DISCORD_CLIENT_ID='CLIENT_ID'
4+
DISCORD_CLIENT_SECRET='CLIENT_SECRET'
5+
WEBSITE_URL='http://localhost:3000'
6+
NEXT_PUBLIC_API_URL='http://localhost:18103'
37

48
MYSQL_ADDRESS='YOUR_MYSQL_SERVER_ADDRESS'
59
MYSQL_PORT='YOUR_MYSQL_SERVER_PORT'
610
MYSQL_USER='YOUR_MYSQL_USER'
711
MYSQL_PASSWORD='YOUR_MYSQL_PASSWORD'
812
MYSQL_DATABASE='YOUR_DATABASE_NAME'
913

14+
JWT_SECRET='YOUR_JWT_SECRET'
1015
AUTH="AUTH_KEY_FOR_API"

api/package.json

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
{
2-
"name": "@chatr/api",
3-
"type": "module",
4-
"version": "0.1.0",
5-
"scripts": {
6-
"dev": "bun with-env bun --watch src/index.ts --dev",
7-
"with-env": "dotenv -e ../.env --"
8-
},
9-
"dependencies": {
10-
"cors": "^2.8.5",
11-
"cron": "^3.1.7",
12-
"express": "^4.19.2",
13-
"mysql2": "^3.10.3"
14-
},
15-
"devDependencies": {
16-
"@types/bun": "latest",
17-
"@types/cors": "^2.8.17",
18-
"@types/express": "^4.17.21",
19-
"dotenv-cli": "^7.4.2"
20-
}
21-
}
2+
"name": "@chatr/api",
3+
"type": "module",
4+
"version": "0.1.0",
5+
"scripts": {
6+
"dev": "bun with-env bun --watch src/index.ts --dev",
7+
"with-env": "dotenv -e ../.env --"
8+
},
9+
"dependencies": {
10+
"cors": "^2.8.5",
11+
"cron": "^3.1.7",
12+
"express": "^4.19.2",
13+
"jsonwebtoken": "^9.0.2",
14+
"mysql2": "^3.10.3"
15+
},
16+
"devDependencies": {
17+
"@types/bun": "latest",
18+
"@types/cors": "^2.8.17",
19+
"@types/express": "^4.17.21",
20+
"@types/jsonwebtoken": "^9.0.7",
21+
"dotenv-cli": "^7.4.2"
22+
}
23+
}

api/src/db/init.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ export async function initTables() {
4444
xp INT NOT NULL
4545
)
4646
`;
47+
const createOauthUsersTable = `
48+
CREATE TABLE IF NOT EXISTS oauth_users (
49+
id VARCHAR(255) NOT NULL PRIMARY KEY,
50+
name VARCHAR(255) NOT NULL,
51+
username VARCHAR(255) NOT NULL,
52+
avatar VARCHAR(255) NOT NULL,
53+
access_token VARCHAR(255) NOT NULL,
54+
refresh_token VARCHAR(255) NOT NULL,
55+
expires_at TIMESTAMP NOT NULL
56+
)
57+
`;
4758

4859
pool.query(createGuildsTable, (err) => {
4960
if (err) {
@@ -76,4 +87,12 @@ export async function initTables() {
7687
console.log("Tracking table created");
7788
}
7889
});
90+
91+
pool.query(createOauthUsersTable, (err) => {
92+
if (err) {
93+
console.error("Error creating OAuth users table:", err);
94+
} else {
95+
console.log("OAuth users table created");
96+
}
97+
});
7998
}

api/src/db/queries/guilds.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { pool } from "..";
55
export interface Guild {
66
id: string;
77
name: string;
8-
icon: string;
8+
icon?: string;
99
members: number;
1010
cooldown: number;
1111
updates_enabled: 0 | 1;

api/src/db/queries/oauth-users.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import type { QueryError } from "mysql2";
2+
3+
import { pool } from "..";
4+
5+
export interface OAuthUser {
6+
id: string;
7+
name: string;
8+
username: string;
9+
avatar: string;
10+
access_token: string;
11+
refresh_token: string;
12+
expires_at: Date;
13+
}
14+
15+
export type OAuthUserWithoutTokens = Without<
16+
OAuthUser,
17+
"access_token" | "refresh_token" | "expires_at"
18+
>;
19+
20+
type Without<T, K> = {
21+
[L in keyof T]: L extends K ? undefined : T[L];
22+
};
23+
24+
export function getOAuthUser(
25+
id: string
26+
): Promise<[QueryError, null] | [null, OAuthUser]> {
27+
return new Promise((resolve, reject) => {
28+
pool.query(
29+
"SELECT * FROM oauth_users WHERE id = ?",
30+
[id],
31+
(err, results) => {
32+
if (err) {
33+
reject([err, null]);
34+
} else {
35+
resolve([null, (results as OAuthUser[])[0]]);
36+
}
37+
}
38+
);
39+
});
40+
}
41+
42+
export function updateOAuthUser(
43+
oauthUser: Partial<OAuthUser>
44+
): Promise<[QueryError, false] | [null, true]> {
45+
return new Promise((resolve, reject) => {
46+
pool.query(
47+
`
48+
INSERT INTO oauth_users (id, name, username, avatar, access_token, refresh_token, expires_at)
49+
VALUES (?, ?, ?, ?, ?, ?, ?)
50+
ON DUPLICATE KEY UPDATE
51+
name = VALUES(name),
52+
username = VALUES(username),
53+
avatar = VALUES(avatar),
54+
access_token = VALUES(access_token),
55+
refresh_token = VALUES(refresh_token),
56+
expires_at = VALUES(expires_at)
57+
`,
58+
[
59+
oauthUser.id,
60+
oauthUser.name,
61+
oauthUser.username,
62+
oauthUser.avatar,
63+
oauthUser.access_token,
64+
oauthUser.refresh_token,
65+
oauthUser.expires_at,
66+
],
67+
(err) => {
68+
if (err) {
69+
reject([err, false]);
70+
} else {
71+
resolve([null, true]);
72+
}
73+
}
74+
);
75+
});
76+
}

0 commit comments

Comments
 (0)