merge with main
This commit is contained in:
parent
9149bba868
commit
9b7e8ab276
10 changed files with 434 additions and 54 deletions
|
|
@ -1,36 +1,51 @@
|
|||
require('dotenv').config();
|
||||
require("dotenv").config();
|
||||
|
||||
const GoogleStrategy = require("passport-google-oauth20").Strategy;
|
||||
const passport = require("passport");
|
||||
const CustomStrategy = require("passport-custom").Strategy;
|
||||
|
||||
passport.use(
|
||||
new GoogleStrategy(
|
||||
{
|
||||
clientID: process.env.GOOGLE_CLIENT_ID,
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
|
||||
callbackURL: process.env.GOOGLE_CALLBACK_URL,
|
||||
scope: ["profile", "email"],
|
||||
},
|
||||
function (accessToken, refreshToken, profile, callback) {
|
||||
new GoogleStrategy(
|
||||
{
|
||||
clientID: process.env.GOOGLE_CLIENT_ID,
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
|
||||
callbackURL: process.env.GOOGLE_CALLBACK_URL,
|
||||
scope: ["profile", "email"],
|
||||
},
|
||||
function (accessToken, refreshToken, profile, callback) {
|
||||
callback(null, profile);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// Save the user info to your DB here if still not yet saved
|
||||
// Example of what profile might contain:
|
||||
// {
|
||||
// "id": "112233445566778899",
|
||||
// "displayName": "John Doe",
|
||||
// "emails": [{ "value": "john.doe@gmail.com" }],
|
||||
// "photos": [{ "value": "https://.../photo.jpg" }]
|
||||
// }
|
||||
passport.use(
|
||||
"student-auth",
|
||||
new CustomStrategy(async (req, done) => {
|
||||
const { assignment, password } = req.body;
|
||||
|
||||
callback(null, profile);
|
||||
}
|
||||
)
|
||||
try {
|
||||
// Call your external auth service
|
||||
const response = await axios.post("http://localhost:8082/student/verify", {
|
||||
assignment,
|
||||
password,
|
||||
});
|
||||
|
||||
if (response.data && response.data.success) {
|
||||
const user = response.data.user;
|
||||
return done(null, user); // success
|
||||
} else {
|
||||
return done(null, false, { message: "Invalid credentials" });
|
||||
}
|
||||
} catch (err) {
|
||||
return done(err);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, user);
|
||||
done(null, user);
|
||||
});
|
||||
|
||||
passport.deserializeUser((user, done) => {
|
||||
done(null,user);
|
||||
});
|
||||
done(null, user);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue