Skip to content

Commit

Permalink
No email
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsm412 committed Feb 6, 2025
1 parent d70fd1f commit c91cb27
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 51 deletions.
70 changes: 34 additions & 36 deletions rair-front/src/hooks/useConnectUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,29 +292,13 @@ const useConnectUser = () => {
dispatchStack.push(setExchangeRates(await getCoingeckoRates()));
dispatchStack.push(setConnectedChain(loginData.blockchain));

let willUpdateUserData = false;
let email: string | undefined = undefined;

try {
// Check if user exists in DB
const userDataResponse = await axios.get<TUserResponse>(
`/api/users/${loginData.userAddress}`
);
let user = userDataResponse.data.user;
if (!user?.email) {
// Try getting the email from social login
switch (loginMethod) {
case 'alchemyV4':
email = (await loginData.userDetails.getAuthDetails()).email;
break;
case 'web3auth':
email = loginData.userDetails.email;
break;
}
}
if (!userDataResponse.data.success || !user) {
// If the user doesn't exist, send a request to register him using a TEMP adminNFT
willUpdateUserData = true;
const userCreation = await axios.post<TUserResponse>(
'/api/users',
JSON.stringify({ publicAddress: loginData.userAddress }),
Expand All @@ -326,8 +310,6 @@ const useConnectUser = () => {
}
);
user = userCreation.data.user;
} else if (!userDataResponse?.data?.user?.email && email) {
willUpdateUserData = true;
}

// Authorize user
Expand Down Expand Up @@ -359,27 +341,43 @@ const useConnectUser = () => {
reactSwal.close();
break;
}
if (willUpdateUserData) {
const userData = await loginData.userDetails;
const availableData: Partial<User> = {};
if (email && !loginResponse.user.email) {
availableData.email = email;
}
if (email && !loginResponse.user.nickName) {
availableData.nickName = email?.split('@')?.[0];

const updateData = {};
if (!user?.gitHandle || !user?.email) {
// Try getting the git ID from social login
switch (loginMethod) {
case 'alchemyV4':
if (!user.email) {
updateData['email'] = loginData.userDetails.email;
}
if (!user.gitHandle) {
updateData['gitId'] = (
await loginData.userDetails.getAuthDetails()
).claims.sub.split('|')[1];
}
break;
case 'web3auth':
if (!user.email) {
updateData['email'] = loginData.userDetails.email;
}
if (!user.gitHandle) {
updateData['gitId'] = '';
}
break;
}
if (userData.name && !userData.name.includes('@')) {
availableData.firstName = userData.name.split(' ')?.[0];
availableData.lastName = userData.name.split(' ')?.[0];
}
if (Object.keys(availableData).length !== 0) {
const newUserResponse = await axios.patch(
`/api/users/${loginData.userAddress.toLowerCase()}`,
availableData
);
user = newUserResponse.data.user;
if (!user.nickName) {
updateData['nickName'] = updateData['email']?.split('@')?.[0];
}
}

if (Object.keys(updateData).length) {
console.info(updateData);
const newUserResponse = await axios.patch(
`/api/users/${loginData.userAddress.toLowerCase()}`,
updateData
);
user = newUserResponse.data.user;
}
dispatch(loadCurrentUser());
if (loginResponse.success) {
dispatchStack.forEach((dispatchItem) => {
Expand Down
1 change: 0 additions & 1 deletion rair-front/src/redux/web3Slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export const connectChainAlchemyV4 = createAsyncThunk(
signer
})
});
console.info({ client });

return {
connectedChain: chainData.hash,
Expand Down
1 change: 1 addition & 0 deletions rair-front/src/types/databaseTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export interface User extends MongoDocument {
publicAddress?: Hex;
creationDate?: string;
blocked?: boolean;
gitHandle?: string;
}

export interface MetadataAttribute {
Expand Down
32 changes: 19 additions & 13 deletions rair-node/bin/api/users/users.Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,27 +250,33 @@ exports.updateUserByUserAddress = async (req, res, next) => {
...updatedUser,
};

res.json({ success: true, user: updatedUser });
return next();
} catch (e) {
return next(e);
}
};

exports.queryGithubData = async (req, res, next) => {
const { email, publicAddress, gitHandle } = req.session.userData;
if (gitHandle) {
return;
const { publicAddress, gitHandle } = req.session.userData;
const { gitId } = req.body;
if (gitHandle || !gitId) {
return res.json({ success: true, user: req.session.userData });
}
const query = await (await fetch(`https://api.github.com/search/users?q=${email}`)).json();
if (query.total_count === 1) {
await User.findOneAndUpdate({ publicAddress }, {
gitHandle: query.items[0].login,
gitBio: query.items[0].bio,
const query = await (await fetch(`https://api.github.com/user/${gitId}`)).json();

Check failure

Code scanning / CodeQL

Server-side request forgery Critical

The
URL
of this request depends on a
user-provided value
.
if (query.login) {
const updatedUser = await User.findOneAndUpdate({ publicAddress }, {
gitHandle: query.login,
gitBio: query.bio,
// available: query.items[0].hireable,
avatar: query.items[0].avatar_url,
});
return;
avatar: query.avatar_url,
}, { new: true, projection: { nonce: 0 } }).lean();

req.session.userData = {
...req.session.userData,
...updatedUser,
};
return res.json({ success: true, user: req.session.userData });
}
log.error("Couldn't fetch Github data, more than one account associated with the email");
log.error("Couldn't fetch Github data");
return res.json({ success: true, user: req.session.userData });
};
2 changes: 1 addition & 1 deletion rair-node/bin/middleware/isAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = (req, res, next) => {
const { adminRights, publicAddress } = req.user;

if (!adminRights) {
return next(new AppError(`User ${publicAddress} don't have admin rights.`, 401));
return next(new AppError(`User ${publicAddress} does not have admin rights.`, 401));
}

return next();
Expand Down
1 change: 1 addition & 0 deletions rair-node/bin/schemas/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ module.exports = {
firstName: Joi.string(),
lastName: Joi.string(),
blocked: Joi.boolean(),
gitId: Joi.string(),
}),
};

0 comments on commit c91cb27

Please sign in to comment.