2024-05-28 20:22:41 +00:00
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
import passport from 'passport';
|
|
|
|
import { Strategy as localStrategy } from 'passport-local';
|
2024-06-03 10:21:54 +00:00
|
|
|
import { encryptPwd, pwdIsValid } from '../model/model.mjs';
|
2024-05-28 20:22:41 +00:00
|
|
|
import { Strategy as JWTstrategy, ExtractJwt } from 'passport-jwt';
|
2024-06-03 10:21:54 +00:00
|
|
|
import { userDb } from '../db.mjs';
|
2024-05-28 20:22:41 +00:00
|
|
|
passport.use('signup', new localStrategy({
|
2024-06-03 10:21:54 +00:00
|
|
|
usernameField: 'username',
|
2024-05-28 20:22:41 +00:00
|
|
|
passwordField: 'password'
|
2024-06-03 10:21:54 +00:00
|
|
|
}, (username, password, done) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
|
console.log("signup auth strategy has begun");
|
2024-05-28 20:22:41 +00:00
|
|
|
try {
|
2024-06-03 10:21:54 +00:00
|
|
|
const encryptedPwd = yield encryptPwd(password);
|
|
|
|
const user = yield userDb("users").insert({ username: username, password: encryptedPwd }).returning(["username", "password"]);
|
|
|
|
console.log(`user: ${user}`);
|
2024-05-28 20:22:41 +00:00
|
|
|
return done(null, user);
|
|
|
|
}
|
|
|
|
catch (err) {
|
2024-06-03 10:21:54 +00:00
|
|
|
console.error(err);
|
2024-05-28 20:22:41 +00:00
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
})));
|
2024-05-29 09:34:21 +00:00
|
|
|
passport.use('login', new localStrategy({
|
2024-06-03 10:21:54 +00:00
|
|
|
usernameField: "username",
|
2024-05-28 20:22:41 +00:00
|
|
|
passwordField: "password",
|
|
|
|
session: false
|
|
|
|
}, (email, password, done) => __awaiter(void 0, void 0, void 0, function* () {
|
2024-05-29 09:34:21 +00:00
|
|
|
console.log("local strategy called");
|
2024-05-28 20:22:41 +00:00
|
|
|
try {
|
2024-06-03 10:21:54 +00:00
|
|
|
let returnedUser = yield userDb("users").select("username", "password").where({ username: email });
|
|
|
|
const user = returnedUser[0];
|
2024-05-28 20:22:41 +00:00
|
|
|
console.log(`user: ${user}`);
|
2024-06-03 10:21:54 +00:00
|
|
|
if (!user || returnedUser.length === 0) {
|
2024-05-28 20:22:41 +00:00
|
|
|
return done(null, false, { message: "user not found" });
|
|
|
|
}
|
2024-06-03 10:21:54 +00:00
|
|
|
const validate = yield pwdIsValid(password, user);
|
2024-05-29 09:34:21 +00:00
|
|
|
console.log(`isValidPassword? ${validate}`);
|
2024-05-28 20:22:41 +00:00
|
|
|
if (!validate) {
|
|
|
|
return done(null, false, { message: "wrong password" });
|
|
|
|
}
|
2024-05-29 09:34:21 +00:00
|
|
|
return done(null, user, { message: "logged in successfully" });
|
2024-05-28 20:22:41 +00:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return done(error);
|
|
|
|
}
|
|
|
|
})));
|
|
|
|
passport.use(new JWTstrategy({
|
|
|
|
secretOrKey: "TOP_SECRET",
|
2024-06-03 10:21:54 +00:00
|
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme('secret_token')
|
2024-05-28 20:22:41 +00:00
|
|
|
}, (token, done) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
|
try {
|
|
|
|
return done(null, token.user);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
done(error);
|
|
|
|
}
|
|
|
|
})));
|