L
L
lordprotein2020-03-01 23:31:54
Node.js
lordprotein, 2020-03-01 23:31:54

Passport.js not saving authorization?

When submitting the form, the login / password displays a message about successful authorization, but as soon as I access the secure path (in the code, for example, this is the path / word ), then a message is displayed stating that I am not authorized.
Perhaps the problem is that I am accessing from one react.js address to another express.js address. How did I get it? In Postman, authorization and subsequent access to secure paths works fine.
Hence the question: how can I get around this or fix it? Thank you.

app.use(cors());
app.use('/downloads', express.static('downloads'));


app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({ secret: 'SECRET' }));

app.use(passport.initialize());
app.use(passport.session());
app.use(flash());



passport.use('login', new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password',
    passReqToCallback: true
}, (req, username, password, done) => {
    db.query('SELECT * FROM users WHERE login=? AND password=?', [username, password], (err, user) => {
        if (err) return done(err);

        if (user.length) {
            console.log(`Succsess login`)
            console.log(user)
            return done(null, user[0]);
        }

        console.log('Haven`t this a user');
        return done(null, false, req.flash('dsa'));
    })
}));


function isAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return next();
    }

    res.send({ status: 'close' })
}




passport.serializeUser((user, done) => {
    console.log("Serialize: ", user.ID);
    done(null, user.ID);
});


passport.deserializeUser(function (ID, done) {
    db.query('SELECT * FROM users WHERE ID=?', [ID], (err, user) => {
        if (err) return done(err);

        done(null, user[0])
    })
});

app.get('/word', isAuthenticated, (req, res) => {
    res.send({ word: 'word' })
})

app.post('/login', passport.authenticate('login', {
    // successRedirect: '/menu',
    failureRedirect: '/',
    failureFlash: true,
}));

app.get('/signout', (req, res) => {
    req.logout();
    console.log('signout')
    res.send('yes')
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Lambov, 2020-03-02
@maxilamb

package.json set proxy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question