Answer the question
In order to leave comments, you need to log in
Why does passport.js not find the strategy?
Good afternoon everyone, I'm puzzling over authorization, it gives an error of the following kind:
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;
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);
Answer the question
In order to leave comments, you need to log in
/*
* @param {String} strategy
* @param {Object} options
* @param {Function} callback
* @return {Function} middleware
* @api public
*/
Authenticator.prototype.authenticate = function(strategy, options, callback) {
return this._framework.authenticate(this, strategy, options, callback);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question