Skip to content

Commit

Permalink
Merge pull request #31 from Red-Stream-Y3/feat/OAtuh-implementation/s…
Browse files Browse the repository at this point in the history
…achini

Feat/o atuh implementation/sachini
  • Loading branch information
sachiniLekamge authored Sep 26, 2024
2 parents d08bb15 + e48270c commit 7956549
Show file tree
Hide file tree
Showing 13 changed files with 789 additions and 718 deletions.
73 changes: 73 additions & 0 deletions BackEnd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import blogRoutes from './routes/blogRoutes.js';
import cropRoutes from './routes/cropRoutes.js';
import diseaseRoutes from './routes/diseaseRoutes.js';

import passport from 'passport';
import session from 'express-session';
import MongoStore from 'connect-mongo';
import User from './models/userModel.js';
import './passport-setup.js';

if (process.env.NODE_ENV !== 'production') {
dotenv.config({ path: findConfig('.env.dev') });
}
Expand Down Expand Up @@ -53,6 +59,73 @@ const csrfProtection = csurf({
},
});

const store = MongoStore.create({
mongoUrl: process.env.MONGO_URI,
collectionName: "sessions",
});

app.use(
session({
secret: process.env.COOKIE_KEY,
saveUninitialized: false,
resave: false,
store: store,
cookie: { secure: process.env.NODE_ENV === "production" },
})
);

app.use(passport.initialize());
app.use(passport.session());


app.get(
"/auth/google",
passport.authenticate("google", {
scope: ["profile", "email"],
})
);

app.get(
"/auth/google/callback",
passport.authenticate("google", {
failureRedirect: `${process.env.FRONTEND_URL}/login`,
}),
async (req, res) => {
const { googleId, username, email, firstName, lastName, profilePic } =
req.user;
console.log("user", req.user);

let existingUser = await User.findOne({ googleId: googleId });
console.log("existingUser", existingUser);

if (!existingUser) {
existingUser = new User({
username: username,
googleId: googleId,
email: email,
firstName: firstName,
lastName: lastName,
profilePic: profilePic || User.schema.path("profilePic").defaultValue,
});
await existingUser.save();
}

const googleAccessToken = req.user.accessToken;

req.login(existingUser, (err) => {
if (err) return res.status(500).send(err);

res.redirect(
`${process.env.FRONTEND_URL}/login?googleAuthSuccess` +
`&username=${username}&email=${email}&firstName=${firstName}` +
`&lastName=${lastName}&profilePic=${profilePic}` +
`&role=${existingUser.role}&request=${existingUser.request}` +
`&token=${googleAccessToken}`
);
});
}
);

// Use middleware to increment visitor count
app.use(visitMiddleware);
app.use(incrementCountMiddleware);
Expand Down
4 changes: 4 additions & 0 deletions BackEnd/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const userSchema = mongoose.Schema(
default:
'https://images.unsplash.com/photo-1633332755192-727a05c4013d?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=580&q=80',
},
googleId: {
type: String,
unique: true,
},
role: {
type: String,
enum: ['regular', 'contributor', 'moderator', 'admin'],
Expand Down
Loading

0 comments on commit 7956549

Please sign in to comment.