added complete api functionality
This commit is contained in:
parent
956ede5619
commit
24bd570795
13 changed files with 205 additions and 107 deletions
|
|
@ -2,7 +2,7 @@ GOOGLE_CLIENT_ID="485880105639-1in8tvb6ondnn198rasuj2d8ank06ntp.apps.googleuserc
|
|||
GOOGLE_CLIENT_SECRET="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"
|
||||
GOOGLE_CALLBACK_URL="http://localhost:8080/auth/google/callback"
|
||||
LOGIN_REDIRECT_URL="http://localhost:5173/"
|
||||
ACCEPTED_ORIGINS=http://localhost:3000,http://localhost:8081,http://localhost:3001,http://localhost:5137
|
||||
ACCEPTED_ORIGINS=http://localhost:3000,http://localhost:8081,http://localhost:3001,http://localhost:5173
|
||||
DB_USER_SERVICE_URL="http://js-user-db-service:3100/"
|
||||
AUTH_SESSION_KEY="f3f4d8e6b17a4b3abdc8e9a2c0457aaf91c0d5f6e3b7a9c8df624bd71ea35f42"
|
||||
PORT=8080
|
||||
|
|
@ -5,35 +5,39 @@ const axios = require("axios");
|
|||
router.get(
|
||||
"/google/callback",
|
||||
passport.authenticate("google", {
|
||||
successRedirect: "/auth/login",
|
||||
successRedirect: "/auth/google/login",
|
||||
failureRedirect: "/auth/login/failed",
|
||||
})
|
||||
);
|
||||
|
||||
router.get('/current_user', (req, res) => {
|
||||
router.get("/current_user", (req, res) => {
|
||||
if (req.isAuthenticated()) {
|
||||
console.log("Authenticated user:", req.user);
|
||||
res.json(req.user);
|
||||
} else {
|
||||
res.status(401).json({ error: 'Not authenticated' });
|
||||
console.log("User not authenticated");
|
||||
res.status(401).json({ error: "Not authenticated" });
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/login", (req, res) => {
|
||||
router.get("/google/login", (req, res) => {
|
||||
if (req.user) {
|
||||
|
||||
console.log(`${process.env.DB_USER_SERVICE_URL}instructor/register-user`)
|
||||
axios.post(`${process.env.DB_USER_SERVICE_URL}instructor/register-user`, {
|
||||
user: req.user,
|
||||
})
|
||||
.then(response => {
|
||||
console.log("User registration response:", response.data);
|
||||
res.redirect(process.env.LOGIN_REDIRECT_URL);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Error registering user:", error.message);
|
||||
res.status(500).json({ error: true, message: "User login failed" });
|
||||
});
|
||||
|
||||
console.log(`${process.env.DB_USER_SERVICE_URL}instructor/register-user`);
|
||||
axios
|
||||
.post(`${process.env.DB_USER_SERVICE_URL}instructor/register-user`, {
|
||||
user: req.user,
|
||||
})
|
||||
.then((response) => {
|
||||
req.user.userId = response.data.user.userid;
|
||||
console.log("User ID:", response.data.user.userid);
|
||||
req.user.role = "instructor";
|
||||
console.log("User registration response:", response.data);
|
||||
res.redirect(process.env.LOGIN_REDIRECT_URL);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error registering user:", error.message);
|
||||
res.status(500).json({ error: true, message: "User login failed" });
|
||||
});
|
||||
} else {
|
||||
res.status(403).json({ error: true, message: "Not Authorized" });
|
||||
}
|
||||
|
|
@ -49,8 +53,8 @@ router.get("/login/failed", (req, res) => {
|
|||
router.get("/google", passport.authenticate("google", ["profile", "email"]));
|
||||
|
||||
router.get("/logout", (req, res) => {
|
||||
req.logOut();
|
||||
res.redirect(process.env.LOGIN_REDIRECT_URL);
|
||||
req.logOut();
|
||||
res.redirect(process.env.LOGIN_REDIRECT_URL);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports = router;
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ app.use(passport.session());
|
|||
app.use(
|
||||
cors({
|
||||
origin: process.env.ACCEPTED_ORIGINS.split(","),
|
||||
methods: "GET",
|
||||
methods: ["GET", "POST"],
|
||||
credentials: true,
|
||||
})
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue