-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.mjs
144 lines (107 loc) · 4.01 KB
/
app.mjs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import express from "express";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import bcrypt from 'bcrypt';
import path from "path";
import jwt from 'jsonwebtoken';
import low from 'lowdb';
import FileSync from 'lowdb/adapters/FileSync.js';
import { v4 as uuidv4 } from 'uuid';
const adapter = new FileSync('./database.json');
const db = low(adapter);
import togeminiRouter from "./routers/togemini.router.js";
import cors from "cors";
import tolammaRouter from "./routers/tolemma.router.js";
import savevRouter from "./routers/savev.router.js";
import togroqRouter from "./routers/togroq.router.js";
import savefRouter from "./routers/savef.router.js";
import apikeyRouter from "./routers/apikey.router.js";
const app = express();
const PORT = process.env.PORT || 5003;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const jwtSecretKey = 'dsfdsfsdfdsvcsvdfgefg';
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(express.static(path.join(__dirname, "client/build")));
app.use("/api/togemini", togeminiRouter);
app.use("/api/tolamma", tolammaRouter);
app.use("/api/savev", savevRouter);
app.use("/api/togroq", togroqRouter);
app.use("/api/savef", savefRouter);
app.use("/api/apikey", apikeyRouter);
app.listen(PORT, () => console.log("Server running on " + PORT));
//@@
// Basic home route for the API
app.get('/', (_req, res) => {
res.send('Auth API.\nPlease use POST /auth & POST /verify for authentication')
})
// The auth endpoint that creates a new user record or logs a user based on an existing record
app.post('/auth', (req, res) => {
const { email, password } = req.body
// Look up the user entry in the database
const user = db
.get('users')
.value()
.filter((user) => email === user.email)
// If found, compare the hashed passwords and generate the JWT token for the user
if (user.length === 1) {
bcrypt.compare(password, user[0].password, function (_err, result) {
if (!result) {
return res.status(401).json({ message: 'Invalid password' })
} else {
let loginData = {
email,
signInTime: Date.now(),
}
const token = jwt.sign(loginData, jwtSecretKey)
res.status(200).json({ message: 'success', token })
}
})
// If no user is found, hash the given password and create a new entry in the auth db with the email and hashed password
} else if (user.length === 0) {
bcrypt.hash(password, 10, function (_err, hash) {
console.log({ email, password: hash })
db.get('users').push({ email, password: hash }).write()
let loginData = {
email,
signInTime: Date.now(),
}
const token = jwt.sign(loginData, jwtSecretKey)
res.status(200).json({ message: 'success', token })
})
}
})
// The verify endpoint that checks if a given JWT token is valid
app.post('/verify', (req, res) => {
const tokenHeaderKey = 'jwt-token'
const authToken = req.headers[tokenHeaderKey]
try {
const verified = jwt.verify(authToken, jwtSecretKey)
if (verified) {
return res.status(200).json({ status: 'logged in', message: 'success' })
} else {
// Access Denied
return res.status(401).json({ status: 'invalid auth', message: 'error' })
}
} catch (error) {
// Access Denied
return res.status(401).json({ status: 'invalid auth', message: 'error' })
}
})
// An endpoint to see if there's an existing account for a given email address
app.post('/check-account', (req, res) => {
const { email } = req.body
console.log(req.body)
const user = db
.get('users')
.value()
.filter((user) => email === user.email)
console.log(user)
res.status(200).json({
status: user.length === 1 ? 'User exists' : 'User does not exist',
userExists: user.length === 1,
})
})
// app.listen(3080)