forked from lcrowther-snyk/tictactoe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
51 lines (38 loc) · 1.05 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
const url = 'mongodb://127.0.0.1:27017';
const dbName = 'tic-tac-toe';
mongoose.connect(url+"/"+dbName);
let Data = new Schema({
data: Array,
player: String,
type: String,
name: String
});
const Board = mongoose.model('Board', Data)
const userSchema = new Schema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true }
});
const User = mongoose.model('User', userSchema);
// Check if the first user exists
checkAdmin();
async function checkAdmin() {
var adminUser = await User.findOne({username: 'admin'});
if (!adminUser) {
const newUser = new User({
username: 'admin',
password: 'snyk2023'
});
newUser.save();
}
}
async function login(username, password) {
let users = await User.find({username: username, password: password});
if (users.length > 0) {
return true;
} else {
return false;
}
}
module.exports = {Board,User,login}