Hello I am writing a web application with Node JS
and I am trying to authenticate users in my website as students or as instructors which I defined multiple credentials for each route and I defined two anchors which are : /auth/google/student
and /auth/google/instructor
but unfortunately only the first route in passport.js
will be called for callback.
how can I fix this problem and exactly tell express to call which route?
here are my codes :
app.js
const express = require("express");
const cors = require("cors");
const env = require("./env");
const mysql = require("mysql");
const passport = require("passport");
const studentLoginGoogle = require("./studentLoginGoogle");
const instructorLoginGoogle = require("./instructorLoginGoogle");
const app = express();
app.use(passport.initialize());
app.use(passport.session());
app.use("/auth/google/instructor", instructorLoginGoogle);
app.use("/auth/google/student", studentLoginGoogle);
studentLoginGoogle.js:
const express = require("express");
const mysql = require("mysql");
const env = require("./env");
const cors = require("cors");
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy;
const FacebookStrategy = require("passport-facebook");
const app = express();
env.get();
const router = express.Router();
app.use(cors());
let userProfile;
passport.serializeUser(function (user, cb) {
cb(null, user);
});
passport.deserializeUser(function (obj, cb) {
cb(null, obj);
});
//authentication with google
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID_STUDENT,
clientSecret: process.env.GOOGLE_CLIENT_SECRET_STUDENT,
callbackURL: "http://localhost:3001/auth/google/student/callback",
},
function (accessToken, refreshToken, profile, done) {
userProfile = profile;
return done(null, userProfile);
}
)
);
router.get(
"/",
passport.authenticate("google", { scope: ["profile", "email"] })
);
router.get(
"/callback",
passport.authenticate("google", { failureRedirect: "/" }),
function (req, res) {
console.log(userProfile);
let sql = `SELECT user_name FROM students WHERE email="${userProfile.emails[0].value}"`;
con.query(sql, (err, result) => {
if (err) console.log(err);
if (result.length !== 0) {
res.send("you are validated as student ...");
} else {
let sql2 = `INSERT INTO students (first_name,last_name,email,user_name) VALUES ("${userProfile.name.givenName}","${userProfile.name.familyName}","${userProfile.emails[0].value}","${userProfile.displayName}")`;
con.query(sql2, (err, result) => {
if (err) console.log(err);
res.send("you are registered as student for this app");
});
}
});
}
);
module.exports = router;
instructorLoginGoogle.js:
const express = require("express");
const mysql = require("mysql");
const env = require("./env");
const cors = require("cors");
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy;
const FacebookStrategy = require("passport-facebook");
const app = express();
env.get();
const router = express.Router();
app.use(cors());
let userProfile;
passport.serializeUser(function (user, cb) {
cb(null, user);
});
passport.deserializeUser(function (obj, cb) {
cb(null, obj);
});
//authentication with google
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID_INSTRUCTOR,
clientSecret: process.env.GOOGLE_CLIENT_SECRET_INSTRUCTOR,
callbackURL: "http://localhost:3001/auth/google/instructor/callback",
},
function (accessToken, refreshToken, profile, done) {
userProfile = profile;
return done(null, userProfile);
}
)
);
router.get(
"/",
passport.authenticate("google", { scope: ["profile", "email"] })
);
router.get(
"/callback",
passport.authenticate("google", { failureRedirect: "/" }),
function (req, res) {
let sql = `SELECT user_name FROM instructors WHERE email="${userProfile.emails[0].value}"`;
con.query(sql, (err, result) => {
if (err) console.log(err);
if (result.length !== 0) {
res.send("you are validated as instructor ...");
} else {
let sql2 = `INSERT INTO instructors (first_name,last_name,email,user_name,gender,year_of_birth) VALUES ("${userProfile.name.givenName}","${userProfile.name.familyName}","${userProfile.emails[0].value}","${userProfile.displayName}","male",1990)`;
con.query(sql2, (err, result) => {
if (err) console.log(err);
res.send("you are registered as instructor for this app");
});
}
});
}
);
module.exports = router;
in the /auth/google/callback
for instance we can have multiple redirect urls
as follows :
router.get('/auth/google/callback' , passport.use('google'), (req,res) => {
if(req. ...){
res.redirect('http://localhost:3000/....')
}
else{
res.redirect('....')
}
} )
something like above.