如果用户不使用特定域,则重定向用户
Redirecting users if they aren't using specific domain
我正在尝试使用 res.redirect 将用户重定向到路由,或者如果他们尝试使用指定域以外的域登录,则使用 res.send 发送文件。条件检查工作正常,但我正在尝试使用 res.sendFile/res.redirect 但它似乎不在此功能的范围内工作。很明显,此函数中没有 res,但这就是我想出的全部。在网上进行了很好的搜索,但我还没有解决问题。
感谢任何帮助。
passport.use(
new GoogleStrategy({
callbackURL: '/google/redirect',
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret
}, function (accessToken, refreshToken, profile, done){
if (profile._json.hd === "HIDDEN-DOMAIN.COM") {
User.findOne({googleId : profile.id})
.then(function(currentUser){
if(currentUser){
console.log('User with ID' + currentUser.googleId +' already exists. No new entry was made');
done(null, currentUser);
} else {
new User({
username: profile.displayName,
googleId: profile.id
})
.save()
.then(function(newUser){
console.log('New user created: ' + newUser);
done(null, newUser);
});
}
})
} else {
console.log(__dirname);
res.sendFile('../login.html');
};
}));
使用中间件执行检查,如果通过则next()
。
结帐:https://expressjs.com/en/guide/using-middleware.html
这个例子展示了一个挂载在 / 路径上的中间件函数。 / 路径上的任何类型的 HTTP 请求都会执行该函数。
这个例子展示了一个路由和它的处理函数(中间件系统)。该函数处理 GET 请求。
app.use('/', function (req, res, next) {
// Check 1
console.log('Request URL:', req.originalUrl)
next()
}, function (req, res, next) {
// Check 2: Pass first check
console.log('Request Type:', req.method)
next()
})
app.get('/', (req, res) => {
// Final Route
});
Example:
app.use('/first', function (req, res, next) {
passport.use(
new GoogleStrategy({
callbackURL: '/google/redirect',
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret
}, function (accessToken, refreshToken, profile, done){
if (profile._json.hd === "HIDDEN-DOMAIN.COM") {
User.findOne({googleId : profile.id})
.then(function(currentUser){
if(currentUser){
console.log('User with ID' + currentUser.googleId +' already exists. No new entry was made');
done(null, currentUser);
} else {
new User({
username: profile.displayName,
googleId: profile.id
})
.save()
.then(function(newUser){
console.log('New user created: ' + newUser);
done(null, newUser);
next(); // next();
});
}
})
} else {
console.log(__dirname);
next(); // next();
}
}));
}, function (req, res, next) {
// More checks
next()
});
app('/', (req, res) => {
// final route here
res.sendFile('../login.html');
})
我正在尝试使用 res.redirect 将用户重定向到路由,或者如果他们尝试使用指定域以外的域登录,则使用 res.send 发送文件。条件检查工作正常,但我正在尝试使用 res.sendFile/res.redirect 但它似乎不在此功能的范围内工作。很明显,此函数中没有 res,但这就是我想出的全部。在网上进行了很好的搜索,但我还没有解决问题。
感谢任何帮助。
passport.use(
new GoogleStrategy({
callbackURL: '/google/redirect',
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret
}, function (accessToken, refreshToken, profile, done){
if (profile._json.hd === "HIDDEN-DOMAIN.COM") {
User.findOne({googleId : profile.id})
.then(function(currentUser){
if(currentUser){
console.log('User with ID' + currentUser.googleId +' already exists. No new entry was made');
done(null, currentUser);
} else {
new User({
username: profile.displayName,
googleId: profile.id
})
.save()
.then(function(newUser){
console.log('New user created: ' + newUser);
done(null, newUser);
});
}
})
} else {
console.log(__dirname);
res.sendFile('../login.html');
};
}));
使用中间件执行检查,如果通过则next()
。
结帐:https://expressjs.com/en/guide/using-middleware.html
这个例子展示了一个挂载在 / 路径上的中间件函数。 / 路径上的任何类型的 HTTP 请求都会执行该函数。
这个例子展示了一个路由和它的处理函数(中间件系统)。该函数处理 GET 请求。
app.use('/', function (req, res, next) {
// Check 1
console.log('Request URL:', req.originalUrl)
next()
}, function (req, res, next) {
// Check 2: Pass first check
console.log('Request Type:', req.method)
next()
})
app.get('/', (req, res) => {
// Final Route
});
Example:
app.use('/first', function (req, res, next) {
passport.use(
new GoogleStrategy({
callbackURL: '/google/redirect',
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret
}, function (accessToken, refreshToken, profile, done){
if (profile._json.hd === "HIDDEN-DOMAIN.COM") {
User.findOne({googleId : profile.id})
.then(function(currentUser){
if(currentUser){
console.log('User with ID' + currentUser.googleId +' already exists. No new entry was made');
done(null, currentUser);
} else {
new User({
username: profile.displayName,
googleId: profile.id
})
.save()
.then(function(newUser){
console.log('New user created: ' + newUser);
done(null, newUser);
next(); // next();
});
}
})
} else {
console.log(__dirname);
next(); // next();
}
}));
}, function (req, res, next) {
// More checks
next()
});
app('/', (req, res) => {
// final route here
res.sendFile('../login.html');
})