Compare commits
No commits in common. "auth" and "master" have entirely different histories.
|
@ -1,80 +0,0 @@
|
|||
import passport from "passport";
|
||||
import { Strategy as localStrategy } from "passport-local";
|
||||
import { encryptPwd, pwdIsValid } from "../model/model.mjs";
|
||||
import { Strategy as JWTstrategy, ExtractJwt } from "passport-jwt";
|
||||
import { userDb } from "../db.mjs";
|
||||
|
||||
passport.use(
|
||||
"signup",
|
||||
new localStrategy(
|
||||
{
|
||||
usernameField: "username",
|
||||
passwordField: "password",
|
||||
},
|
||||
async (username, password, done) => {
|
||||
console.log("signup auth strategy has begun");
|
||||
try {
|
||||
const encryptedPwd = await encryptPwd(password);
|
||||
const user = await userDb("users")
|
||||
.insert({ username: username, password: encryptedPwd })
|
||||
.returning(["username", "password"]);
|
||||
console.log(`user: ${user}`);
|
||||
return done(null, user);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
done(err);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
passport.use(
|
||||
"login",
|
||||
new localStrategy(
|
||||
{
|
||||
usernameField: "username",
|
||||
passwordField: "password",
|
||||
session: false,
|
||||
},
|
||||
async (email, password, done) => {
|
||||
console.log("local strategy called");
|
||||
try {
|
||||
let returnedUser = await userDb("users")
|
||||
.select("username", "password")
|
||||
.where({ username: email });
|
||||
const user = returnedUser[0];
|
||||
console.log(`user: ${user}`);
|
||||
if (!user || returnedUser.length === 0) {
|
||||
return done(null, false, { message: "user not found" });
|
||||
}
|
||||
|
||||
const validate = await pwdIsValid(password, user);
|
||||
console.log(`isValidPassword? ${validate}`);
|
||||
|
||||
if (!validate) {
|
||||
return done(null, false, { message: "wrong password" });
|
||||
}
|
||||
|
||||
return done(null, user, { message: "logged in successfully" });
|
||||
} catch (error) {
|
||||
return done(error);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
passport.use(
|
||||
new JWTstrategy(
|
||||
{
|
||||
secretOrKey: "TOP_SECRET",
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme("secret_token"),
|
||||
},
|
||||
async (token, done) => {
|
||||
try {
|
||||
return done(null, token.user);
|
||||
} catch (error) {
|
||||
done(error);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
10
curl-res
10
curl-res
|
@ -1,10 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Error</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre>SyntaxError: Unexpected token 'e', "email:demo@demo.test" is not valid JSON<br> at JSON.parse (<anonymous>)<br> at createStrictSyntaxError (/home/andrzej/dev/sub-manager-backend/node_modules/body-parser/lib/types/json.js:169:10)<br> at parse (/home/andrzej/dev/sub-manager-backend/node_modules/body-parser/lib/types/json.js:86:15)<br> at /home/andrzej/dev/sub-manager-backend/node_modules/body-parser/lib/read.js:128:18<br> at AsyncResource.runInAsyncScope (node:async_hooks:206:9)<br> at invokeCallback (/home/andrzej/dev/sub-manager-backend/node_modules/raw-body/index.js:238:16)<br> at done (/home/andrzej/dev/sub-manager-backend/node_modules/raw-body/index.js:227:7)<br> at IncomingMessage.onEnd (/home/andrzej/dev/sub-manager-backend/node_modules/raw-body/index.js:287:7)<br> at IncomingMessage.emit (node:events:511:28)<br> at endReadableNT (node:internal/streams/readable:1367:12)</pre>
|
||||
</body>
|
||||
</html>
|
36
db.mjs
36
db.mjs
|
@ -1,24 +1,20 @@
|
|||
import knex from "knex";
|
||||
|
||||
export const db = knex({
|
||||
client: "sqlite3",
|
||||
connection: {
|
||||
filename: "./submissions.db",
|
||||
},
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
export const userDb = knex({
|
||||
client: "sqlite3",
|
||||
connection: {
|
||||
filename: "./users.db",
|
||||
},
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
client: 'sqlite3',
|
||||
connection: {
|
||||
filename: "./submissions"
|
||||
},
|
||||
useNullAsDefault: true
|
||||
})
|
||||
|
||||
export const testDb = knex({
|
||||
client: 'sqlite3',
|
||||
connection: {
|
||||
filename: "./test.db"
|
||||
},
|
||||
useNullAsDefault: true
|
||||
})
|
||||
|
||||
|
||||
|
||||
export const testDb = knex({
|
||||
client: "sqlite3",
|
||||
connection: {
|
||||
filename: "./test.db",
|
||||
},
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
import bcrypt from "bcrypt";
|
||||
|
||||
export async function encryptPwd(pwd) {
|
||||
return Promise.resolve(bcrypt.hash(pwd, 10));
|
||||
}
|
||||
|
||||
export async function pwdIsValid(pwd, user) {
|
||||
return Promise.resolve(bcrypt.compare(pwd, user.password));
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -9,21 +9,16 @@
|
|||
"author": "Andrzej Stepien",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"dependencies": {
|
||||
"bcrypt": "^5.1.1",
|
||||
"body-parser": "^1.20.2",
|
||||
"chai": "^4.3.8",
|
||||
"chai-as-promised": "^7.1.1",
|
||||
"chai-http": "^4.4.0",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.19.2",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"express": "^4.18.2",
|
||||
"knex": "^2.5.1",
|
||||
"lodash": "^4.17.21",
|
||||
"luxon": "^3.4.3",
|
||||
"mocha": "^10.2.0",
|
||||
"passport": "^0.7.0",
|
||||
"passport-jwt": "^4.0.1",
|
||||
"passport-local": "^1.0.0",
|
||||
"pino": "^8.15.0",
|
||||
"pino-http": "^8.5.0",
|
||||
"sqlite3": "^5.1.6"
|
||||
|
|
48
server.mjs
48
server.mjs
|
@ -1,34 +1,36 @@
|
|||
import express from "express";
|
||||
import pinoHTTP from "pino-http";
|
||||
import express from "express"
|
||||
import pinoHTTP from 'pino-http'
|
||||
import logger from "./logger.mjs";
|
||||
import bodyParser from "body-parser";
|
||||
import passport from "passport";
|
||||
import "./auth/auth.mjs";
|
||||
import { Data } from "./objects/Data.mjs";
|
||||
import { db } from "./db.mjs";
|
||||
import { getEndpoints, postEndpoints } from "./objects/Endpoints.mjs";
|
||||
import cors from "cors";
|
||||
import cors from 'cors'
|
||||
|
||||
const app = express();
|
||||
app.use(passport.initialize());
|
||||
const port = 4000;
|
||||
app.use(cors());
|
||||
app.use(pinoHTTP({ logger }));
|
||||
app.use(bodyParser.json());
|
||||
const app = express()
|
||||
const port = 4000
|
||||
const corsOptions={
|
||||
origin: ['http://localhost:5173']
|
||||
}
|
||||
app.use(cors())
|
||||
app.use(pinoHTTP({logger}))
|
||||
app.use(bodyParser.json())
|
||||
|
||||
|
||||
|
||||
|
||||
const data = new Data(db)
|
||||
await data.init()
|
||||
|
||||
|
||||
app.use('/api',getEndpoints(data))
|
||||
app.use('/api',postEndpoints(db,data) )
|
||||
|
||||
const data = new Data(db);
|
||||
await data.init();
|
||||
|
||||
app.use("/api", getEndpoints(data));
|
||||
app.use(
|
||||
"/api",
|
||||
passport.authenticate("jwt", { session: false }),
|
||||
postEndpoints(db, data),
|
||||
);
|
||||
|
||||
app.listen(port, (err) => {
|
||||
if (err) logger.error(err);
|
||||
logger.info("Server listening on PORT " + port);
|
||||
});
|
||||
if (err) logger.error(err);
|
||||
logger.info("Server listening on PORT " + port)
|
||||
})
|
||||
|
||||
export default app;
|
||||
export default app
|
BIN
submissions
BIN
submissions
Binary file not shown.
BIN
submissions.db
BIN
submissions.db
Binary file not shown.
Loading…
Reference in New Issue