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: dashboard #94

Merged
merged 9 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
DISCORD_TOKEN='TOKEN'
DISCORD_TOKEN_DEV='DEV_TOKEN'
DISCORD_CLIENT_ID='CLIENT_ID'
DISCORD_CLIENT_SECRET='CLIENT_SECRET'
WEBSITE_URL='http://localhost:3000'
NEXT_PUBLIC_API_URL='http://localhost:18103'

MYSQL_ADDRESS='YOUR_MYSQL_SERVER_ADDRESS'
MYSQL_PORT='YOUR_MYSQL_SERVER_PORT'
MYSQL_USER='YOUR_MYSQL_USER'
MYSQL_PASSWORD='YOUR_MYSQL_PASSWORD'
MYSQL_DATABASE='YOUR_DATABASE_NAME'

JWT_SECRET='YOUR_JWT_SECRET'
AUTH="AUTH_KEY_FOR_API"
42 changes: 22 additions & 20 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{
"name": "@chatr/api",
"type": "module",
"version": "0.1.0",
"scripts": {
"dev": "bun with-env bun --watch src/index.ts --dev",
"with-env": "dotenv -e ../.env --"
},
"dependencies": {
"cors": "^2.8.5",
"cron": "^3.1.7",
"express": "^4.19.2",
"mysql2": "^3.10.3"
},
"devDependencies": {
"@types/bun": "latest",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"dotenv-cli": "^7.4.2"
}
}
"name": "@chatr/api",
"type": "module",
"version": "0.1.0",
"scripts": {
"dev": "bun with-env bun --watch src/index.ts --dev",
"with-env": "dotenv -e ../.env --"
},
"dependencies": {
"cors": "^2.8.5",
"cron": "^3.1.7",
"express": "^4.19.2",
"jsonwebtoken": "^9.0.2",
"mysql2": "^3.10.3"
},
"devDependencies": {
"@types/bun": "latest",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.7",
"dotenv-cli": "^7.4.2"
}
}
19 changes: 19 additions & 0 deletions api/src/db/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ export async function initTables() {
xp INT NOT NULL
)
`;
const createOauthUsersTable = `
CREATE TABLE IF NOT EXISTS oauth_users (
id VARCHAR(255) NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
avatar VARCHAR(255) NOT NULL,
access_token VARCHAR(255) NOT NULL,
refresh_token VARCHAR(255) NOT NULL,
expires_at TIMESTAMP NOT NULL
)
`;

pool.query(createGuildsTable, (err) => {
if (err) {
Expand Down Expand Up @@ -76,4 +87,12 @@ export async function initTables() {
console.log("Tracking table created");
}
});

pool.query(createOauthUsersTable, (err) => {
if (err) {
console.error("Error creating OAuth users table:", err);
} else {
console.log("OAuth users table created");
}
});
}
2 changes: 1 addition & 1 deletion api/src/db/queries/guilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { pool } from "..";
export interface Guild {
id: string;
name: string;
icon: string;
icon?: string;
members: number;
cooldown: number;
updates_enabled: 0 | 1;
Expand Down
76 changes: 76 additions & 0 deletions api/src/db/queries/oauth-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { QueryError } from "mysql2";

import { pool } from "..";

export interface OAuthUser {
id: string;
name: string;
username: string;
avatar: string;
access_token: string;
refresh_token: string;
expires_at: Date;
}

export type OAuthUserWithoutTokens = Without<
OAuthUser,
"access_token" | "refresh_token" | "expires_at"
>;

type Without<T, K> = {
[L in keyof T]: L extends K ? undefined : T[L];
};

export function getOAuthUser(
id: string
): Promise<[QueryError, null] | [null, OAuthUser]> {
return new Promise((resolve, reject) => {
pool.query(
"SELECT * FROM oauth_users WHERE id = ?",
[id],
(err, results) => {
if (err) {
reject([err, null]);
} else {
resolve([null, (results as OAuthUser[])[0]]);
}
}
);
});
}

export function updateOAuthUser(
oauthUser: Partial<OAuthUser>
): Promise<[QueryError, false] | [null, true]> {
return new Promise((resolve, reject) => {
pool.query(
`
INSERT INTO oauth_users (id, name, username, avatar, access_token, refresh_token, expires_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
name = VALUES(name),
username = VALUES(username),
avatar = VALUES(avatar),
access_token = VALUES(access_token),
refresh_token = VALUES(refresh_token),
expires_at = VALUES(expires_at)
`,
[
oauthUser.id,
oauthUser.name,
oauthUser.username,
oauthUser.avatar,
oauthUser.access_token,
oauthUser.refresh_token,
oauthUser.expires_at,
],
(err) => {
if (err) {
reject([err, false]);
} else {
resolve([null, true]);
}
}
);
});
}
Loading
Loading