Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/server/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const token = require("../utils/token");
const bcrypt = require("bcrypt");
const client = new OAuth2Client(process.env.WEB_CLIENT_ID);
const nodemailer = require("nodemailer");
const crypto = require("node:crypto");

const NAMESPACE = "7af17462-8078-4703-adda-be2143a4d93a";

Expand All @@ -13,6 +14,9 @@ async function create(accessToken, refreshToken, profile, callback) {
let { sub, given_name, family_name, picture, email } = profile._json;
picture = picture.replace("=s96-c", "");
const googleUUID = uuidv5(sub, NAMESPACE);
const hash = crypto.createHash("sha256");
hash.update(email);
const emailHash = hash.digest("hex");
const user = await prisma.user.findUniqueOrThrow({
where: {
userId: googleUUID,
Expand All @@ -22,7 +26,7 @@ async function create(accessToken, refreshToken, profile, callback) {
const newUser = await prisma.user.create({
data: {
userId: googleUUID,
email: email,
email: emailHash,
avatar: picture,
firstName: given_name,
lastName: family_name,
Expand Down Expand Up @@ -82,14 +86,17 @@ async function register(newUser) {
const salt = await bcrypt.genSalt(saltRounds);
const passwordHash = await bcrypt.hash(newUser.password, salt);
const userId = uuidv4();
const hash = crypto.createHash("sha256");
hash.update(newUser.email);
const emailHash = hash.digest("hex");

// placeholder names until new user puts in their names in onboarding screen
return await prisma.user.create({
data: {
userId,
firstName: "New",
lastName: "User",
email: newUser.email,
email: emailHash,
password: passwordHash,
},
});
Expand Down Expand Up @@ -191,9 +198,12 @@ async function authenticate(request, response, next) {
}

async function isUserEmail(email) {
const hash = crypto.createHash("sha256");
hash.update(email);
const emailHash = hash.digest("hex");
const result = await prisma.user.findUnique({
where: {
email: email,
email: emailHash,
},
});

Expand Down
11 changes: 9 additions & 2 deletions packages/server/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const prisma = require("../prisma/prisma");
const crypto = require("node:crypto");

async function getAllUsers() {
const query = await prisma.user.findMany();
Expand All @@ -24,18 +25,24 @@ async function getUserEmail(userId) {
}

async function getUserByEmail(email) {
const hash = crypto.createHash("sha256");
hash.update(email);
const emailHash = hash.digest("hex");
const query = await prisma.user.findUnique({
where: {
email: email,
email: emailHash,
},
});
return query;
}

async function getUserIdByEmail(email) {
const hash = crypto.createHash("sha256");
hash.update(email);
const emailHash = hash.digest("hex");
const query = await prisma.user.findUnique({
where: {
email: email,
email: emailHash,
},
});
return query.userId;
Expand Down