-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99a31eb
commit 8570177
Showing
7 changed files
with
329 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.