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';
|
|
|
|
import { UserModel } from '../model/model.mjs';
|
|
|
|
import { Strategy as JWTstrategy, ExtractJwt } from 'passport-jwt';
|
|
|
|
passport.use('signup', new localStrategy({
|
|
|
|
usernameField: 'email',
|
|
|
|
passwordField: 'password'
|
|
|
|
}, (email, password, done) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
|
try {
|
|
|
|
const user = yield UserModel.create({ email, password });
|
|
|
|
return done(null, user);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
})));
|
2024-05-29 09:34:21 +00:00
|
|
|
passport.use('login', new localStrategy({
|
2024-05-28 20:22:41 +00:00
|
|
|
usernameField: "email",
|
|
|
|
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 {
|
|
|
|
const user = yield UserModel.findOne({ email });
|
|
|
|
console.log(`user: ${user}`);
|
|
|
|
if (!user) {
|
|
|
|
return done(null, false, { message: "user not found" });
|
|
|
|
}
|
|
|
|
const validate = yield user.isValidPassword(password);
|
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",
|
|
|
|
jwtFromRequest: ExtractJwt.fromUrlQueryParameter('secret_token')
|
|
|
|
}, (token, done) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
|
try {
|
|
|
|
return done(null, token.user);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
done(error);
|
|
|
|
}
|
|
|
|
})));
|