I'm trying to get the host the app is currently on and then change a variable accordingly. I know I can use req.get('host')
to get the host but I believe my problem stems from callbacks
var callBackUrl;
app.use(function(req,res,next){
if(req.get('host') == 'localhost:3000'){
callBackUrl = 'http://localhost:3000/handleauth';
}
else{
callBackUrl = 'http://example.com/handleauth';
}
console.log('CALL BACK URL: ', callBackUrl);
next();
});
console.log(callBackUrl); //undefined
app.use('/', routes);
... //more code
I would like to make a note that I have read about asynchronicity and understand why console.log
prints undefined
. I simply do not know how to tie callbacks in with req
and res
.
req.host
returns a value correctly. I simply need to get the current host and then use it for authentication purposes (production vs. development)
EDIT: Perhaps this additional code will help others in understanding what I am trying to accomplish
//... original code from question
passport.use(new InstagramStrategy({
clientID: INSTAGRAM_CLIENT_ID,
clientSecret: INSTAGRAM_CLIENT_SECRET,
callbackURL: callBackUrl //set to undefined and therefore authentication fails
},
function(accessToken, refreshToken, profile, done){
process.nextTick(function(){
app.set('instaID', profile.id.toString());
app.set('fullName', profile.displayName);
app.set('imgSource', profile._json.data.profile_picture);
return done(null,profile.id);
});
}));
I think you should use if else like this;
var callBackUrl;
app.use(function(req,res,next){
if(req.get('host') == 'localhost:3000'){
callBackUrl = 'http://localhost:3000/handleauth';
console.log('CALL BACK URL: ', callBackUrl);
next();
}
else{
callBackUrl = 'http://example.com/handleauth';
console.log('CALL BACK URL: ', callBackUrl);
next();
}
});
console.log(callBackUrl); //undefined
app.use('/', routes);
... //more code
I was able to fix this problem by moving all the authentication code into the app.use
callback. If anyone has another solution please let me know. It just seems strange to me to stick that entire block of code in there just to access the value of one variable.
var callBackUrl;
app.use(function(req,res, next){
if(req.get('host') == 'localhost:3000'){
callBackUrl = 'http://localhost:3000/handleauth';
}
else{
callBackUrl = 'http://example.com/handleauth';
}
console.log('CALL BACK URL: ', callBackUrl);
passport.serializeUser(function(user,done){
done(null,user);
});
passport.deserializeUser(function(obj,done){
done(null,obj);
});
passport.use(new InstagramStrategy({
clientID: INSTAGRAM_CLIENT_ID,
clientSecret: INSTAGRAM_CLIENT_SECRET,
callbackURL: callBackUrl //no longer undefined
},
function(accessToken, refreshToken, profile, done){
process.nextTick(function(){
app.set('instaID', profile.id.toString());
app.set('fullName', profile.displayName);
app.set('imgSource', profile._json.data.profile_picture);
return done(null,profile.id);
});
}));
next();
});
console.log("URL: ", callBackUrl) //still prints undefined but that it ok since we use the value of callBackUrl inside the callback
app.use('/', routes);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.