OAuth (Passport.js) Tutorial #18 - Redirecting Users

preview_player
Показать описание
Hey gang, in this OAuth tutorial I'll show you how we can redirect users based on their login status. For example, if a page requires authenication and a user is not logged in, we'll redirect them to the login page.

----- COURSE LINKS:

---------------------------------------------------------------------------------------------
Other tutorials:

----- NODE.JS TUTORIALS

----- MONGODB TUTORIALS

============== The Net Ninja =====================

================== Social Links ==================

Рекомендации по теме
Комментарии
Автор

It's crazy. I looked at a ton of tutorials around the web and every single one of them is either uncomplete, assuming stuff, mixed up, and just doing stuff not right.

Now I binged through this series and it all just clicked. just like that. Finally got my head around passport.

THANK YOU !

Superturds
Автор

Best Passport.js & OAuth Tuts On Youtube hands down !!!

mohamedhefnawy
Автор

bro i never seen in my lyf this type of guys who make this kind of efforts for other fking best tutorials ever

jaygondalia
Автор

I can always count on Net Ninja to make something I've struggled with FOREVER finally understandable. Thank you!

eolculnamo
Автор

These videos are really underestimated. This saved me a lot of time and really helped me understand a lot of weirdness of nodejs that was lost on me. Thank you.

joshmadakor
Автор

wow i've been looking for how to do this for the past week, and you just solved my problem, simply and easily, in under 10 minutes!

Kalmanheyn
Автор

Before I continue watching, I just want to say thank you for making these tutorial.

vincebanzon
Автор

The order of middleware loading is important: middleware functions that are loaded first are also executed first.
In my case it was the order of cookiesession and passport

app.use(cookieSession({
maxAge: 24 * 60 * 60 * 1000,

}))


app.use(passport.session());

When passport block is front of cookiesession it throw error undefined.

mildza
Автор

As you said -> The best OAuth tutorials on the planet :)

保険太郎-gh
Автор

seriously ..the best tutorials ...thank you very much

sagarbula
Автор

Thanks for the lessons Ninja, your a life saver.. literally your saving me tons of time of my life..

kodieivie
Автор

For anybody getting the username undefined error, check the order of the middleware. For me i needed to usue cookie sessions before registering passport.initialize() and passport.session()

TheErw
Автор

Same here after redirecting req.user is showing undefined.
How to fix this??

SivaneshFiz
Автор

res.send('You are logged in, this is your profile ' + req.user.username);

req.user.username is sending 'undefined'

saurabhshubham
Автор

To fix my undefined error, I didn't put the secret key inside of [] which would cause the deserializeUser to not call.

devinzimmerman
Автор

For all those who are receiving undefined error, scroll down you will see many solution. Also do not that in
app.use(Cookiesession({

maxAge:24*60,
keys:['euhftcycidjdnsns']

}));

KEYS ARE ARRAY

avichalchadha
Автор

my if(!req.user) keeps kicking to my login page no matter how many times I log in. also from the auth routes, i keep getting [object Object] when I try and inspect the object.

lilrex
Автор

req.user is undefined, someone help? in deployment im getting this issue but not in local

tusharsoni
Автор

I just had a funky bug.. n i spent a few hours on it.. everything was good at google/redirect route but then when i used res.redirect i couldn't get anything from req.user.username at my new route.. like it dropped from the request object.. i spent hours going over everything, trying anything out.. it wasnt working. then i decided to try res.send('your logged in as ' + req.user); {{<---without the .username}} i got the object back then i tried it normal again{{ res.send('your logged in as ' + req.user.username);}} and it worked..? so then i hit cmd + z twice and try it again like it was and its still works.. i jus dont get what was wrong? i tried for hours to figure this out. i erased the sites cookies.. could it have been the browser cache? it makes no sense i even chose out new routes. logged each step to the console. even a different google account. you would think something was misspelled but thats why i checked with the undo.. atleast im more familiar with passport now.. it just bugs me that i dont know what was wrong..

kodieivie
Автор

Almost everybody has error: "req.user.username is undefined" when redirects to profile page. Just try to change your deserializeUser function to this:

passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {

if (err) {
throw err;
}

console.log('deserializing user', id);
done(null, user);
});
});

Hope it helps someone.

vitaliishapoval