jaredhanson / jaredhanson/passport

What's wrong with my code?

Open
#562 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
23.5k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Hi everyone! Pls, can you explain me, what's i am doing wrong? I want to redirect user after registration, but it's don't work... New user is created, but redirect doesn't working.

Thats how i try to add new user:

```
export const register = (req, res, next) => {
console.log(req.body);
const user = new User({
username: req.body.username,
password: req.body.password,
favorite: []
});
user.save(function(err) {
if(err) {
res.send(err);
} else {
console.log('user: ' + user.username + " saved.");
req.logIn(user, function(err) {
console.log('ttttt');
if (err) {
next(err);
} else {
res.redirect('/app');
}
});
}
})
};
```

Thats my routes:

```
import mustAutenticaten from '../authntication/middleware'
import path from 'path'
export default (app) => {
app.all('/app', mustAutenticaten);
app.all('/fav', mustAutenticaten);
app.get('/reg', (req, res) => {
res.sendFile(path.join(__dirname, '../../', 'index.html'));
});
app.get('/app', (req, res) => {
res.sendFile(path.join(__dirname, '../../', 'index.html'));
});

app.get('/fav', (req, res) => {
res.sendFile(path.join(__dirname, '../../', 'index.html'));
})
}
```

Middleware:

`
export default (req, res, next) => {
req.isAuthenticated() ? next() : res.redirect('/');
}
`
Thanks a lot.

If you need more code - https://github.com/zelenkoff/react-intro/tree/features - my project

Contributor guide

Open the contributing guide

Research direction

Start with the register handler, the /app route protected by mustAutenticaten, and the middleware's req.isAuthenticated() check. Inspect the linked features project and reproduce registration to determine whether the redirect occurs and whether authentication is retained; done means post-registration navigation reaches /app as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript, node.js
Domain
authentication, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.