-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
85 lines (72 loc) · 2.4 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import express from "express";
import authRouter from "./routes/auth.js";
import { config } from "dotenv";
import { errorMiddleware } from "./middlewares/error.js";
import passport from "passport";
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
import cors from "cors";
import { createGoogleUser, googleCallback } from "./controllers/auth.js";
import { backendUrl, frontendUrl } from "./config/constants.js";
import teamRouter from "./routes/team.js";
import memberRouter from "./routes/members.js";
import caRouter from "./routes/ca.js";
import eventRouter from "./routes/events.js";
import userRouter from "./routes/user.js";
import winzoRouter from "./routes/winzo.js";
import swaggerUi from "swagger-ui-express";
import { loadSwaggerWithDynamicUrl } from "./utils/features.js";
import { cbMiddleware } from "./middlewares/auth.js";
export const app = express();
app.use(cors());
config();
app.use(express.json());
const swaggerDocument = loadSwaggerWithDynamicUrl("./docs/swagger.yaml")
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use(passport.initialize());
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: `${backendUrl}/api/v1/Oauth2/google/callback`,
scope: ["profile", "email"],
},
async function (accessToken, refreshToken, profile, cb) {
return createGoogleUser(accessToken, refreshToken, profile, cb);
}
)
);
app.get(
"/api/v1/Oauth2/google",
(req, res, next) => {
const { referralCode } = req.query;
const frontendUrl = req.headers.referer;
const state = JSON.stringify({ frontendUrl, referralCode });
const authUrl = passport.authenticate("google", {
scope: ["profile", "email"],
state,
});
authUrl(req, res, next);
}
);
app.get(
"/api/v1/Oauth2/google/callback",
cbMiddleware,
async (req, res, next) => {
googleCallback(req.user, req, res, next);
}
);
app.use("/api/v1/auth", authRouter);
app.use("/api/v1/team", teamRouter);
app.use("/api/v1/member", memberRouter);
app.use("/api/v1/ca", caRouter);
app.use("/api/v1/user", userRouter);
app.use("/api/v1/event", eventRouter);
app.use("/api/v1/winzo", winzoRouter);
app.get("/", (req, res) => {
res.send("Server is working");
});
app.get("/failure", (req, res) => {
res.send("Failed to Login");
});
app.use(errorMiddleware);