Skip to content

Commit d28aae3

Browse files
committed
fix traditional auth, added comments for proxy error, fix pool connection to mysql db from aiven
1 parent 41c975c commit d28aae3

File tree

7 files changed

+87
-54
lines changed

7 files changed

+87
-54
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ temp/
88
.cert/
99
ca.crt/
1010
ca.key/
11-
coverage/
11+
coverage/
12+
certs/

package-lock.json

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

server/models/userModel.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import mysql from 'mysql2';
22
import fs from 'fs';
33
import log from '../logger';
4+
import path from 'path';
45

56
const dotenv = require('dotenv');
67
dotenv.config();
78

8-
const { USER_DB_URL_MYSQL, USER_DB_USER_MYSQL, USER_DB_PW_MYSQL } = process.env;
9+
const { USER_DB_URL_MYSQL, USER_DB_USER_MYSQL, USER_DB_PW_MYSQL, DB_PORT } = process.env;
910

1011
// SSL data stored as environment variable for GitHub Actions access
1112
// Also stored in .cert file because Elastic Beanstalk has a ~4000 char limit for its environment variables
@@ -18,23 +19,41 @@ const { USER_DB_URL_MYSQL, USER_DB_USER_MYSQL, USER_DB_PW_MYSQL } = process.env;
1819
// ? Buffer.from(process.env.SSL_CERT, 'base64').toString('ascii')
1920
// : fs.readFileSync('./.cert/cert.pem').toString();
2021

22+
// Load CA certificate
23+
const caCertPath = path.resolve(__dirname, '../certs/ca.pem'); // Make sure this file exists
24+
const caCert = fs.readFileSync(caCertPath, 'utf-8');
25+
2126
const pool = mysql
2227
.createPool({
23-
connectionLimit: 10,
2428
host: USER_DB_URL_MYSQL,
2529
user: USER_DB_USER_MYSQL,
2630
password: USER_DB_PW_MYSQL,
27-
database: 'user',
31+
port: Number(DB_PORT),
32+
database: 'dbspy',
33+
waitForConnections: true,
34+
connectionLimit: 10,
35+
queueLimit: 0,
36+
ssl: {
37+
rejectUnauthorized: true,
38+
ca: caCert, // ✅ Provide Aiven's CA certificate
39+
},
2840
// ssl: {
2941
// key: SSL_KEY,
3042
// cert: SSL_CERT,
3143
// },
3244
})
3345
.promise(); // wrap with promise API
34-
//connect used to be a var in the function below but it didn't do anything -Stephen
35-
pool.on('connection', () => {
36-
log.info(`MySQL pool connection established`);
37-
});
46+
47+
// Test Connection
48+
pool
49+
.getConnection()
50+
.then((connection) => {
51+
log.info(`✅ MySQL pool connection established`);
52+
connection.release();
53+
})
54+
.catch((err) => {
55+
log.error(`❌ MySQL connection error:`, err);
56+
});
3857

3958
/**
4059
* User Database Schema

server/routes/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ const routes = (app: Express) => {
9797
// Status code being handled in middleware
9898
app.get('/api/retrieveSchema', retrieveSchema);
9999

100+
// TODO - look into this in vite.config.ts
101+
// Added /api/ route to resolve vite http proxy error?
102+
app.get('/api/', (req, res) => {
103+
res.json({ message: 'Reached /api/ route' });
104+
});
105+
100106
app.get('/*', (_req, res) => {
101107
res.status(404).send('Not found');
102108
});

server/seed/seed.ts

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,53 @@ import mysql from 'mysql2';
22
import dotenv from 'dotenv';
33
dotenv.config();
44
import log from '../logger/index';
5-
import { DataSource } from 'typeorm'
6-
import { User } from '../entities/user.entity'
5+
import { DataSource } from 'typeorm';
6+
import { User } from '../entities/user.entity';
77
// // Connect to SQL db and create users table
88
if (!process.env.USER_DB_URL) {
9-
console.info('We are in the first error')
10-
throw new Error('USER_DB_URL not found');}
9+
console.info('We are in the first error');
10+
throw new Error('USER_DB_URL not found');
11+
}
1112
const connection = mysql.createConnection(process.env.USER_DB_URL);
1213

13-
// const loadData = () => {
14-
// console.info('we are in loadData')
15-
// console.log(process.env.USER_DB_URL);
16-
17-
// const createUserTable: string =
18-
// `CREATE TABLE
19-
// 'Users' (
20-
// 'id' int NOT NULL AUTO_INCREMENT,
21-
// 'full_name' varchar(240) NOT NULL,
22-
// 'sub' varchar(40) DEFAULT NULL,
23-
// 'email' varchar(240) NOT NULL,
24-
// 'picture' varchar(240) DEFAULT NULL,
25-
// 'pg_schema' text,
26-
// 'password' varchar(240) NOT NULL,
27-
// PRIMARY KEY (id)
28-
// ) ENGINE = InnoDB AUTO_INCREMENT = 40 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci`;
29-
// try {
30-
// connection.query(createUserTable);
31-
// } catch (err) {
32-
// log.info(err);
33-
// }
34-
// };
35-
36-
// loadData();
14+
const loadData = async () => {
15+
console.info('we are in loadData');
16+
console.log(process.env.USER_DB_URL);
17+
console.log('Connected to database:', connection.config.database);
3718

19+
const createUserTable: string = `CREATE TABLE
20+
\`users\` (
21+
\`id\` int NOT NULL AUTO_INCREMENT,
22+
\`full_name\` varchar(240) NOT NULL,
23+
\`sub\` varchar(40) DEFAULT NULL,
24+
\`email\` varchar(240) NOT NULL,
25+
\`picture\` varchar(240) DEFAULT NULL,
26+
\`pg_schema\` text,
27+
\`password\` varchar(240) NOT NULL,
28+
PRIMARY KEY (\`id\`)
29+
) ENGINE = InnoDB AUTO_INCREMENT = 40 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci`;
30+
try {
31+
console.log('🔹 Executing SQL Query...');
32+
await connection.promise().query(createUserTable);
33+
console.log('data seeded!!');
34+
} catch (err) {
35+
log.info(err);
36+
} finally {
37+
connection.end(); // Close connection after execution
38+
}
39+
};
3840

41+
loadData();
3942

4043
const MysqlDataSource = new DataSource({
41-
type: "mysql",
42-
host: "process.env.USER_DB_URL",
44+
type: 'mysql',
45+
host: process.env.USER_DB_URL,
4346
port: 3306,
44-
username: "admin",
45-
password: "Codesmith",
46-
database: "dbSpy",
47-
entities: [
48-
User
49-
],
50-
})
47+
username: process.env.USER_DB_USER,
48+
password: process.env.USER_DB_PW,
49+
database: 'dbspy',
50+
entities: [User],
51+
});
5152

52-
process.exit(0);
53+
// TODO - do we still need this? Prevented db from seeding
54+
// process.exit(0);

src/pages/Login.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export default function Login() {
5454
const rootUrl: string = 'https://accounts.google.com/o/oauth2/v2/auth';
5555

5656
const options: Options = {
57-
redirect_uri: 'http://db-spy.io/display/',
57+
// redirect_uri: 'http://db-spy.io/display/',
58+
redirect_uri: 'http://localhost:8080/display/',
5859
client_id:
5960
'1050970973422-4am2mv6e621f83lggfcjubkl3hqtoj0k.apps.googleusercontent.com',
6061
access_type: 'offline',
@@ -76,7 +77,8 @@ export default function Login() {
7677
const getGithub = (): void => {
7778
const rootUrl: string = 'https://github.com/login/oauth/authorize';
7879
const options: Options = {
79-
redirect_uri: 'http://db-spy.io/display/',
80+
// redirect_uri: 'http://db-spy.io/display/',
81+
redirect_uri: 'http://localhost:8080/display/',
8082
client_id: '18d4ecdcc6ed1cb25240',
8183
state: 'randomstring',
8284
allow_signup: 'true',

vite.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ export default defineConfig({
1111
host: true,
1212
proxy: {
1313
'/api/': {
14-
target: 'http://localhost:3000/',
14+
// TODO - vite proxy error
15+
// Vite is unable to recognize localhost IPv6 equivalent, so have to change target to the loopback address for IPv4 (http://localhost:3000/ -> http://127.0.0.1:3000/)
16+
target: 'http://127.0.0.1:3000/',
1517
changeOrigin: true,
1618
secure: false,
17-
}
19+
},
1820
},
1921
},
2022
build: {

0 commit comments

Comments
 (0)