Добрый день всем, ломаю голову над авторизацией, выдает ошибку следующего рода:
Error: Unknown authentication strategy "signin"
at attempt (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\passport\lib\middleware\authenticate.js:186:37)
at authenticate (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\passport\lib\middleware\authenticate.js:362:7)
at router.post (C:\Users\Александр\Desktop\Alejandro\sites\films\config\router.js:51:5)
at Layer.handle [as handle_request] (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:275:10)
at serveStatic (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\serve-static\index.js:75:16)
at Layer.handle [as handle_request] (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Александр\Desktop\Alejandro\sites\films\node_modules\express\lib\router\index.js:335:12)
Гуглил в инете, там в основном ошиюка в синтаксисе полей оговаривается.
Вот моя форма(код урезанный)
<td>Логин</td>
<td><input type="text" class="input" name="email"></td>
<td>Пароль</td>
<td><input type="text" class="input" name="password"></td>
<td><input type="submit" class="input_submit" value="Вход"></td>
код стратегии паспорта
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const { Users } = require('./database/dbUsers');
passport.serializeUser((user, done) => {
console.log('Серилизация' + user)
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
const user = await Users.findById(id);
console.log('Десериализация' + user);
done(null, user);
} catch (error) {
done(error);
}
});
passport.use('signin', new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password'
},
async (email, password, done) => {
try {
const user = await Users.findOne({
where: { email }
});
if (!user) {
done(null, false, {
message: 'Incorrect email'
// Better
// message: 'Incorrect email or password'
});
return;
}
if (user.password !== password) {
done(null, false, {
message: 'Incorrect password'
// Better
// message: 'Incorrect email or password'
});
return;
}
done(null, user);
} catch (error) {
done(error);
}
}
));
passport.use('signup', new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password'
},
async (email, password, done) => {
try {
const findUser = await User.findOne({
where: { email }
});
if (findUser) {
done(null, false, {
message: 'Email already taken'
});
return;
}
const user = await User.create({
email,
password
});
done(null, user);
} catch (error) {
done(error);
}
}
));
код роутера
const { Router } = require('express');
const router = new Router();
const passport = require('passport');
router.post('/signin', (req, res, next) => {
passport.authenticate('signin', (authenticateError, user, info) => { //на эту стратегию signin ругается
if (authenticateError) {
next(authenticateError);
return;
}
if (info && info.message) {
res.render('index', {
errorMessage: info.message
});
return;
}
req.logIn(user, (err) => {
if (err) {
next(err);
return;
}
res.redirect(`index`);
});
})(req, res, next);
});
module.exports = router;
код index
const express = require('express');
const app = express();
const session = require('express-session');
const fileStore = require('session-file-store')(session);
const router = require('./config/router');
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(router);
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(
session({
secret: "secretword",
store: new fileStore(),
cookie: {
path: "/",
httpOnly: true,
maxAge: 60 * 60 * 1000
},
resave: false,
saveUninitialized: false
})
)
app.listen(process.env.PORT || 3000);
Подскажите в чем ошибка пожалуйста?