-
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
a5eacb6
commit 6a90468
Showing
2 changed files
with
65 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { OAuth2Client } from "google-auth-library"; | ||
import jwt from "jsonwebtoken"; | ||
import Users from "../models/users.js"; | ||
|
||
export async function authenticateByGoogle(req, res, next) { | ||
const client = new OAuth2Client(); | ||
const { JWT_SECRET } = process.env; | ||
|
||
const { credential, clientId } = req.body; | ||
try { | ||
const ticket = await client.verifyIdToken({ | ||
idToken: credential, | ||
audience: clientId | ||
}); | ||
const { email, given_name, family_name, picture } = ticket.getPayload(); | ||
|
||
let user = await Users.findOne({ email }); | ||
if (!user) { | ||
user = await Users.create({ | ||
email, | ||
fName: given_name, | ||
lName: family_name, | ||
picture, | ||
authSource: "google" | ||
}); | ||
} | ||
|
||
user = user.toJSON(); | ||
delete user.password; | ||
delete user.__v; | ||
|
||
const token = jwt.sign({ user }, JWT_SECRET); | ||
res.status(200).json({ data: user, jwt: token }); | ||
} catch (e) { | ||
const error = { statusCode: 401, message: e.message || e }; | ||
next(error); | ||
} | ||
} | ||
|
||
export async function signupByEmail(req, res, next) { | ||
const { JWT_SECRET } = process.env; | ||
const { email, password, firstName, lastName } = req.body; | ||
try { | ||
let user = await Users.findOne({ email }); | ||
Check failure Code scanning / CodeQL Database query built from user-controlled sources High
This query object depends on a
user-provided value Error loading related location Loading |
||
if (user) { | ||
const error = { statusCode: 400, message: "User already exists" }; | ||
throw error; | ||
} | ||
|
||
user = await Users.create({ email, password, fName: firstName, lName: lastName, authSource: "self" }); | ||
|
||
user = user.toJSON(); | ||
delete user.password; | ||
delete user.__v; | ||
|
||
const token = jwt.sign({ user }, JWT_SECRET); | ||
res.status(201).json({ data: user, jwt: token }); | ||
} catch (e) { | ||
const error = { statusCode: e.statusCode || 401, message: e.message || e }; | ||
next(error); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,43 +1,11 @@ | ||
import dotenv from "dotenv"; | ||
import express from "express"; | ||
import { OAuth2Client } from "google-auth-library"; | ||
import jwt from "jsonwebtoken"; | ||
import Users from "../models/users.js"; | ||
import { authenticateByGoogle, signupByEmail } from "../controllers/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 { email, given_name, family_name, picture } = ticket.getPayload(); | ||
|
||
let user = await Users.findOne({ email }); | ||
if (!user) { | ||
user = await Users.create({ | ||
email, | ||
fName: given_name, | ||
lName: family_name, | ||
picture, | ||
authSource: "google" | ||
}); | ||
} | ||
|
||
user = user.toJSON(); | ||
delete user.password; | ||
delete user.__v; | ||
|
||
const token = jwt.sign({ user }, JWT_SECRET); | ||
res.status(200).json({ data: user, jwt: token }); | ||
} catch (e) { | ||
res.status(400).json({ msg: err }); | ||
} | ||
}); | ||
router.route("/google").post(authenticateByGoogle); | ||
router.route("/signup").post(signupByEmail); | ||
|
||
export default router; |