Skip to content

Commit

Permalink
16: Add signup/signin with google
Browse files Browse the repository at this point in the history
  • Loading branch information
SakshiShreya committed Oct 13, 2024
1 parent 99a31eb commit 8570177
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"object-shorthand": "off",
"prefer-destructuring": ["error", { "object": true, "array": false }],
"quotes": ["error", "double", { "allowTemplateLiterals": true }],
"spaced-comment": "off"
"spaced-comment": "off",
"import/extensions": "off",
}
}
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import compression from "compression";
import cors from "cors";
import rateLimit from "express-rate-limit";
import ingredients from "./routes/ingredients.js";
import users from "./routes/users.js";
import globalErrorHandler from "./utils/globalErrorHandler.js";

const app = express();
Expand Down Expand Up @@ -35,6 +36,7 @@ app.use(compression());

// 6. Routes
app.use("/api/ingredients", ingredients);
app.use("/auth", users);

app.all("*", (req, res) => {
res.status(404).json({
Expand Down
7 changes: 2 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import dotenv from "dotenv";
import mongoose from "mongoose";
import app from "./app.js";

dotenv.config({ path: "./config.env" });

import app from "./app.js";

// CONNECT MONGO
const DB = process.env.DB;
console.log({ DB });
mongoose.connect(DB).then(() => console.log("DB connection successful!"));
mongoose.connect(process.env.DB).then(() => console.log("DB connection successful!"));

const PORT = process.env.PORT || 8000;
app.listen(PORT, () => {
Expand Down
23 changes: 23 additions & 0 deletions models/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import mongoose from "mongoose";

const usersSchema = new mongoose.Schema({
email: {
type: String,
required: [true, "User should have an email"],
unique: [true, "User with this email already exists"]
},
name: {
type: String,
required: [true, "User should have a name"]
},
password: String,
authSource: {
type: String,
enum: ["self", "google"],
default: "self"
}
});

const Users = mongoose.model("Users", usersSchema);

export default Users;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"eslint-plugin-node": "^11.1.0",
"express": "^4.20.0",
"express-rate-limit": "^7.4.0",
"google-auth-library": "^9.14.1",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.3.1",
"morgan": "^1.10.0"
},
Expand Down
38 changes: 38 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import dotenv from "dotenv";
import express from "express";
import { OAuth2Client } from "google-auth-library";
import jwt from "jsonwebtoken";
import Users from "../models/users.js";

dotenv.config({ path: "./config.env" });
const router = express.Router();
const client = new OAuth2Client();
const { JWT_SECRET } = process.env;

router.post("/google", async (req, res) => {
const { credential, clientId } = req.body;
try {
const ticket = await client.verifyIdToken({
idToken: credential,
audience: clientId
});
const payload = ticket.getPayload();

let user = await Users.findOne({ email: payload.email });
if (!user) {
user = await Users.create({
email: payload.email,
name: payload.name,
authSource: "google"
});
}

const token = jwt.sign({ user }, JWT_SECRET);

res.status(200).json({ data: payload, jwt: token });
} catch (e) {
res.status(400).json({ msg: err });
}
});

export default router;
Loading

0 comments on commit 8570177

Please sign in to comment.