Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
10 changes: 7 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 UserController = require("../../controllers/users");

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

Expand All @@ -13,6 +14,7 @@ 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 emailHash = await UserController.hashUserEmail(email);
const user = await prisma.user.findUniqueOrThrow({
where: {
userId: googleUUID,
Expand All @@ -22,7 +24,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 +84,15 @@ async function register(newUser) {
const salt = await bcrypt.genSalt(saltRounds);
const passwordHash = await bcrypt.hash(newUser.password, salt);
const userId = uuidv4();
const emailHash = await UserController.hashUserEmail(newUser.email);

// 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 +194,10 @@ async function authenticate(request, response, next) {
}

async function isUserEmail(email) {
const emailHash = await UserController.hashUserEmail(email);
const result = await prisma.user.findUnique({
where: {
email: email,
email: emailHash,
},
});

Expand Down
15 changes: 13 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,20 @@ async function getUserEmail(userId) {
}

async function getUserByEmail(email) {
const emailHash = hashUserEmail(email);
const query = await prisma.user.findUnique({
where: {
email: email,
email: emailHash,
},
});
return query;
}

async function getUserIdByEmail(email) {
const emailHash = hashUserEmail(email);
const query = await prisma.user.findUnique({
where: {
email: email,
email: emailHash,
},
});
return query.userId;
Expand All @@ -62,11 +65,19 @@ async function getGuildsForUser(userId) {
return guilds;
}

async function hashUserEmail(email) {
const hash = crypto.createHash("sha256");
hash.update(email);
const emailHash = hash.digest("hex");
return emailHash;
}

module.exports = {
getUser,
getAllUsers,
getUserByEmail,
getGuildsForUser,
getUserIdByEmail,
getUserEmail,
hashUserEmail,
};