#not sure how to use await?
114 messages · Page 1 of 1 (latest)
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const UserModel = require('./User');
// maps the passport fields to the names of fields in database
const localOpt = {
usernameField : 'email',
passwordField : 'password'
};
// define strategy for validating login
const strategy = new LocalStrategy(localOpt, async (email,
password, done) => {
try {
// Find the user in the DB associated with this email
const userChosen = await UserModel.findOne({ email: email });
if( !userChosen ){
//If the user isn't found in the database, set flash message
return done(null, false, { message : 'Email not found'});
}
// Validate password and make sure it matches the bcrypt digest
//stored in the database. If they match, return a value of true.
const validate = await userChosen.isValidPassword(password);
if( !validate ){
return done(null, false, { message : 'Wrong Password'});
}
// Send the user information to the next middleware
return done(null, userChosen, { message : 'Logged in Successfully'});
}
catch (error) {
return done(error);
}
});
// for localLogin, use our strategy to handle User login
passport.use('localLogin', strategy);
// by default, passport uses sessions to maintain login status ...
// you have to determine what to save in session via serializeUser
// and deserializeUser. In our case, we will save the email in the
// session data
passport.serializeUser( (user, done) => done(null, user.email) );
passport.deserializeUser( (email, done) => {
UserModel.findOne({ email: email }, (err, user) => done(err,
user) );
});
im pretty sure that the resource im using to code this is outdated
you should look up the official docs for the version of mongoose that npm installed
the version that you have installed
or install the version of mongoose from your book
so i'm saying either look up the docs for the version you have installed, OR install the version the book was based on
and i was asking i guess how i can refactor my code and use promises instead of callbacks and what the difference is in terms of syntax
but if you follow some book/guide but install a different version of a library, you will be susceptible to these kinds of breaking changes
i guess it matters about context
like here its erroring when i use the function findOne
what's the method signature for the version of findOne you have installed?
^
i see
I'm guessing it now returns a promise, but knowing how to look up the official API docs of libraries that you depend on is a critical skill
so you don't need to use await with promises
probably the easiest way to update your code is to call .then() and then pass your callback to that
(I'm assuming a <<Query>> is a promise)
So try:
passport.deserializeUser( (email, done) => {
UserModel.findOne({ email: email })
.exec()
.then((err, user) => done(err, user)),
});
or something like that
ya its just a bit diffcult. since book doesnt explain anything thats happening with the code and excepts you to edit it and figure stuff out. its like using someone esles code that has more experience than you and your trying to learn off somoene elses code base
ill try
but learning about promises syntax probably requires a whole other tutorial in itself. I'm not prepared to teach you about promises here 🙂
I generally find that code samples from books that use 3rd party libraries often struggle from this problem where if you try to run it, you either need to find out the old version of the library that was used when the book was written, or you'll need to adapt it to newer APIs
ya and it becomes harder
If I'm trying to learn how to use a library, I try to use code samples from the library itself, as this has the highest chance of being maintained
beause your the one trying to learn
but here is the issue
your trying to follow a larger scale project
for example: https://mongoosejs.com/docs/
from a book and youd be bettrer off rewriting code
than following book
but teacher grades you based off the book
and lab itself
its like your learning with broken code
I personally would prioritize developing a learning strategy/system that will last me my whole career, rather than optimizing for what the teacher is looking for
i guess. but if teacher is grading you based on your lab and following steps and taking screenshots
it becomes double the work
of learning stuff twice almost
i got this
i think thats correct
idk didnt give me an output
of what is evne suppose to happen
I would prioritize developing a learning strategy/system that will benefit me long-term, rather than optimizing for what the teacher is looking for 🙂
ya i agree. but do you want to pass the class or do you want to spend time doing it your way and then still having to do the book
at same time
of coruse if it wasnt for teacher i would not be learning tihs way
it sounds like you're developing bad habits
i am.
but employeers want this piece of paper that says you went to school. but at same time teachers give you books with buggy outdated code
but employeers want this piece of paper that says you went to school
do they really?
or do they want someone who can do the job?
well not neccarily
but id like to think that the paper gets you a chance to show your skills
against thousands of applications that get reviewed
and filitered
like at ibm where my uncle works they only accept people with 4 years/bachleors degree
i dont think they would even look at or consider your application
on paper that should work
but i dont think thats the reality. especially when trying to get your first job. after that it doesnt matter i feel
depends on company too. at game companies they could care less i feel.
first, that's only one company. second, if you read the mongoose docs and build something that works, is your teacher really going to give you a bad grade? and third, is IBM gonna look at your transcripts and see that bad grade?
idk im just trying to survive the system
lol
forutenly for me i want to get into tech art
so i dont think a degree really matters that much anyways
but its nice to have something to fall back on
I'm just saying, maybe the system isn't the way you think it is
prob. i dont think there is a black and white
and maybe you're better off prioritizing your own learning than grades
congrats
oh nice
now i can go sculpt shit. i wanted to be a character artist. but doing it on the side as of right now
i was thinkojng about going into tech art
since its a combination of both
what is tech art? like doing 3d modeling?
its the person in charge of the pipeline
or making sure stuff is smooth sailing
for artists in terms of shaders, tools, rigging etc
i want to work more on the shader side in engine
for games or movies
ahh, i see
mostly games
that's cool
im not sure if movies have tech artists on same level as games
cause games are more technical
its a very high level job
or job where you need to know yoru shit
cause there is not much of them per a team
right
only like 2-3 prob
your the person people fall back on when stuff doesnt work in engine. they are artists but dont know enough about tech side. but the programmers or technical people dont know much about art side
so tech artists comes in and makes sure artists know what they can or cant do
since they understand both sides
but anyhow its a cool job