-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategies.js
62 lines (53 loc) · 1.52 KB
/
strategies.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
52
53
54
55
56
57
58
59
60
61
62
import { Strategy as LocalStrategy } from "passport-local";
import { hashPassword, isValidPassword } from "./services.js";
import { modelo } from "./models.js";
import mongoose from "mongoose";
import { mongoConnection } from "./db.js";
mongoose
.connect(mongoConnection)
.then(() => console.log("Conexión establecida con Mongo"))
.catch((error) => console.log("error: ", error));
//DEFINO ESTRATEGIA DE REGISTRO//
const signUp_strategy = new LocalStrategy(
{
passReqToCallback: true,
},
async (req, username, password, done) => {
try {
const existingUser = await modelo.findOne({
username
});
if (existingUser) {
return done(null, null);
}
const newUser = {
password: hashPassword(password),
username: req.body.username,
firstName: req.body.input_name,
lastName: req.body.input_lastName,
avatar: req.file.filename,
admin: false,
};
const createdUser = await modelo.create(newUser);
done(null, createdUser);
} catch (err) {
done("Error en registro", null);
}
}
);
//DEFINO ESTRATEGIA DE LOGUEO//
const login_strategy = new LocalStrategy(async (username, password, done) => {
try {
const user = await modelo.findOne({
username,
});
if (!user || !isValidPassword(password, user.password)) {
return done(null, null);
}
done(null, user);
} catch (err) {
console.log("Error login", err);
done(null, null);
}
});
export { signUp_strategy, login_strategy };