Skip to content

Commit 7a02685

Browse files
authored
Merge pull request #8 from oslabs-beta/convertToDB
Convert to db
2 parents 1725b46 + 8c0b433 commit 7a02685

File tree

6 files changed

+94
-23
lines changed

6 files changed

+94
-23
lines changed

app/components/SignUp.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const SignUp:React.FC = React.memo(() => {
1010
const navigate = useNavigate();
1111
const { setUser } = useContext(DashboardContext);
1212
const [failedSignUp, setFailedSignUp] = useState<JSX.Element>(<>hey</>);
13+
const [username, setUsername] = useState('');
14+
const [email, setEmail] = useState('');
15+
const [password, setPassword] = useState('');
16+
const [confirmPassword, setConfirmPassword] = useState('');
1317

1418
function handleSubmit(e: any) {
1519
e.preventDefault();
@@ -18,6 +22,12 @@ const SignUp:React.FC = React.memo(() => {
1822
const email = inputFields[1].value;
1923
const password = inputFields[2].value;
2024
const confirmPassword = inputFields[3].value;
25+
// setUsername(inputFields[0].value);
26+
// setEmail(inputFields[1].value);
27+
// setPassword(inputFields[2].value);
28+
// setConfirmPassword(inputFields[3].value);
29+
30+
2131

2232
// eslint-disable-next-line no-return-assign
2333
inputFields.forEach(input => (input.value = ''));

electron/databases/mongo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import mongoose from 'mongoose';
22
// import { MongoError } from 'mongodb';
33

44
// Mongoose connection wrapped in function that takes the index of the selected database as the parameter. This index is used to target the correct database for querying.
5-
const connectMongoose = async (i: number, URI: string) => {
5+
const connectMongoose = async (URI: string) => {
66
try {
77
await mongoose.connection.close();
88
const db = await mongoose.connect(URI);

electron/databases/mongoDB.js

Whitespace-only changes.

electron/models/UserModel.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// import mongoose, { Schema, Document } from 'mongoose';
2+
3+
// export interface IService extends Document {
4+
// microservice: string;
5+
// interval: number;
6+
// }
7+
const mongoose = require('mongoose')
8+
9+
const userSchema = new mongoose.Schema({
10+
// User: {
11+
username: {type: String, required:true, unique: true},
12+
password: {type: String, required:true},
13+
email: String,
14+
services: [],
15+
mode: {type: String, default: 'light'}
16+
// },
17+
});
18+
19+
// export default mongoose.model<IService>('services', ServicesSchema);
20+
21+
// export default mongoose.model('User', userSchema);
22+
23+
module.exports = mongoose.model('users', userSchema)

electron/routes/dashboard.ts

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,59 @@ import path from 'path';
44
import fs from 'fs';
55
const bcrypt = require('bcrypt');
66
const saltRounds = 12;
7+
const User = require('../models/UserModel')
8+
const mongoose = require('mongoose');
9+
// const db = require('../databases/mongo')
10+
11+
const MONGO_URI = ''
12+
mongoose.connect(MONGO_URI, {
13+
useNewUrlParser: true,
14+
useUnifiedtopology: true,
15+
})
716

817
// GLOBAL VARIABLES
918
let currentUser = 'guest';
1019
const settingsLocation = path.resolve(__dirname, '../../settings.json');
1120

12-
class User {
13-
username: string;
14-
password: string;
15-
email: string;
16-
services: string[][];
17-
mode: string;
18-
19-
constructor(username: string, password: string, email: string) {
20-
this.username = username;
21-
this.password = this.hashPassword(password);
22-
this.email = email;
23-
this.services = [];
24-
this.mode = 'light';
25-
}
2621

27-
hashPassword(password: string) {
22+
23+
// class User {
24+
// username: string;
25+
// password: string;
26+
// email: string;
27+
// services: string[][];
28+
// mode: string;
29+
30+
// constructor(username: string, password: string, email: string) {
31+
// this.username = username;
32+
// this.password = this.hashPassword(password);
33+
// this.email = email;
34+
// this.services = [];
35+
// this.mode = 'light';
36+
// }
37+
38+
function hashPassword(password: string) {
2839
const salt = bcrypt.genSaltSync(saltRounds);
2940
return bcrypt.hashSync(password, salt);
3041
}
42+
// }
43+
function checkUser(username) {
44+
const userExist = User.findOne({ username })
45+
.then(() => console.log('heeere', userExist))
46+
.catch((error) => console.log('checkUser failed'))
47+
// console.log('heeeeere', userExist)
48+
// return userExist ? true : false;
49+
}
50+
51+
function addUser(username, password, email) {
52+
console.log('inside addUser', username)
53+
const newUser = new User({ username: username, password: hashPassword(password), email: email})
54+
console.log('after calling new User')
55+
56+
newUser.save()
57+
.then((data) => {
58+
console.log('data hurr', data)
59+
})
3160
}
3261

3362
function clearGuestSettings() {
@@ -164,22 +193,31 @@ ipcMain.handle(
164193
'addUser',
165194
(message: IpcMainEvent, user: { username: string; password: string; email: string }) => {
166195
const { username, password, email } = user;
167-
console.log(user)
196+
console.log('in ipcMainhandle', user)
168197

169198
// Verify that username and email have not been taken
170199
const settings = JSON.parse(fs.readFileSync(settingsLocation).toString('utf8'));
171200
if (settings[username]) {
172201
message.returnValue = false;
173202
return message.returnValue;
174203
}
204+
// if (checkUser(username) === true) {
205+
// message.returnValue = false;
206+
// return message.returnValue;
207+
// }
175208

176209
// Add the new user to the local storage
210+
// else {
211+
// const newUser = new User(username, password, email);
212+
// settings[username] = newUser;
213+
// fs.writeFileSync(settingsLocation, JSON.stringify(settings, null, '\t'));
214+
// currentUser = username;
215+
// message.returnValue = true;
216+
// return message.returnValue;
217+
// }
177218
else {
178-
const newUser = new User(username, password, email);
179-
settings[username] = newUser;
180-
fs.writeFileSync(settingsLocation, JSON.stringify(settings, null, '\t'));
181-
currentUser = username;
182-
message.returnValue = true;
219+
addUser(username, password, email)
220+
message.returnValue = true;
183221
return message.returnValue;
184222
}
185223
}

electron/routes/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, i
4747
const [databaseType, URI] = [userDatabase[1], userDatabase[2]];
4848

4949
// Connect to the proper database
50-
if (databaseType === 'MongoDB') await connectMongo(index, URI);
50+
if (databaseType === 'MongoDB') await connectMongo(URI);
5151
if (databaseType === 'SQL') pool = await connectPostgres(index, URI);
5252

5353
// Currently set to a global variable

0 commit comments

Comments
 (0)