-
-
Notifications
You must be signed in to change notification settings - Fork 428
feat(edit-profile): allow custom avatar #995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,52 @@ | ||||||||||||||
| import type { ApiHandler } from '../../types'; | ||||||||||||||
| import { error, success } from '../../utils/response'; | ||||||||||||||
| import { getUserBy, upsertUser } from '../../data/users'; | ||||||||||||||
| import { UserDto } from '../../common/dto/user.dto'; | ||||||||||||||
| import crypto from 'crypto'; | ||||||||||||||
|
|
||||||||||||||
| function getGravatarUrl(email: string): string { | ||||||||||||||
| const hash = crypto | ||||||||||||||
| .createHash('md5') | ||||||||||||||
| .update(email.trim().toLowerCase()) | ||||||||||||||
| .digest('hex'); | ||||||||||||||
| return `https://www.gravatar.com/avatar/${hash}?s=200&d=identicon`; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| type ToggleAvatarRequest = { | ||||||||||||||
| useGravatar: boolean; | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| export const toggleAvatarHandler: ApiHandler<ToggleAvatarRequest> = async (event, context) => { | ||||||||||||||
| try { | ||||||||||||||
| const { useGravatar } = event.parsedBody || {}; | ||||||||||||||
|
|
||||||||||||||
| if (typeof useGravatar !== 'boolean') { | ||||||||||||||
| return error('Invalid request body: useGravatar must be a boolean', 400); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const auth0Id = context.user?.auth0Id; | ||||||||||||||
| if (!auth0Id) { | ||||||||||||||
| return error('Unauthorized', 401); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const currentUser = await getUserBy('auth0Id', auth0Id); | ||||||||||||||
| if (!currentUser) { | ||||||||||||||
| return error('User not found', 404); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const avatarUrl = useGravatar ? getGravatarUrl(currentUser.email) : context.user?.picture; | ||||||||||||||
|
|
||||||||||||||
| const userDto: UserDto = new UserDto({ | ||||||||||||||
| _id: currentUser._id, | ||||||||||||||
| avatar: avatarUrl, | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| const updatedUser = await upsertUser(userDto); | ||||||||||||||
|
|
||||||||||||||
| return success({ | ||||||||||||||
| data: updatedUser, | ||||||||||||||
|
||||||||||||||
| data: updatedUser, | |
| data: { | |
| ...updatedUser, | |
| auth0Picture: context.user?.picture, | |
| auth0Id: updatedUser.auth0Id, | |
| }, |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,14 +1,16 @@ | ||||||
| import React, { FC } from 'react'; | ||||||
| import React, { FC, useState } from 'react'; | ||||||
| import styled from 'styled-components/macro'; | ||||||
| import { useUser } from '../../../../context/userContext/UserContext'; | ||||||
| import Camera from '../../../../assets/me/camera.svg'; | ||||||
| import CardContainer from '../../../components/Card/index'; | ||||||
| import { getAvatarUrl } from '../../../../helpers/avatar'; | ||||||
| import { isGoogleOAuthUser } from '../../../../helpers/authProvider'; | ||||||
| import { IconButton } from '../../../components/Button/IconButton'; | ||||||
| import { Tooltip } from 'react-tippy'; | ||||||
| import { toast } from 'react-toastify'; | ||||||
| import { report } from '../../../../ga'; | ||||||
| import { RedirectToGravatar } from '../../../Modals/RedirectToGravatar'; | ||||||
| import { useApi } from '../../../../context/apiContext/ApiContext'; | ||||||
| import messages from '../../../../messages'; | ||||||
| import Switch from '../../../../components/Switch/Switch'; | ||||||
|
|
||||||
| const ShareProfile = ({ url }: { url: string }) => { | ||||||
| const [showInput, setShowInput] = React.useState(false); | ||||||
|
|
@@ -51,41 +53,105 @@ const ShareProfile = ({ url }: { url: string }) => { | |||||
| }; | ||||||
|
|
||||||
| const Avatar: FC = () => { | ||||||
| const { currentUser } = useUser<true>(); | ||||||
| const { currentUser, updateCurrentUser } = useUser<true>(); | ||||||
| const api = useApi(); | ||||||
| const [isSaving, setIsSaving] = useState(false); | ||||||
|
||||||
|
|
||||||
| if (!currentUser) { | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| const isUsingGravatar = currentUser.avatar?.includes('gravatar.com') || false; | ||||||
|
|
||||||
| const handleToggleGravatar = async (newValue: boolean) => { | ||||||
| setIsSaving(true); | ||||||
| try { | ||||||
| report('Avatar', newValue ? 'use gravatar' : 'use google profile picture'); | ||||||
| const updatedUser = await api.toggleAvatar(newValue); | ||||||
| if (updatedUser) { | ||||||
| api.clearCurrentUser(); | ||||||
| updateCurrentUser(updatedUser); | ||||||
| toast.success('Avatar updated successfully', { toastId: 'avatar-updated' }); | ||||||
| } else { | ||||||
| toast.error(messages.GENERIC_ERROR); | ||||||
| } | ||||||
| } catch (error) { | ||||||
| toast.error(messages.GENERIC_ERROR); | ||||||
| } finally { | ||||||
| setIsSaving(false); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| const isGoogleUser = isGoogleOAuthUser(currentUser.auth0Id); | ||||||
|
|
||||||
| return ( | ||||||
| <CardContainer> | ||||||
| <Container> | ||||||
| <ShareProfile | ||||||
| url={`${process.env.NEXT_PUBLIC_AUTH_CALLBACK}/u/${currentUser._id}`} | ||||||
| /> | ||||||
| <div> | ||||||
| {currentUser && currentUser.avatar ? ( | ||||||
| <UserImage | ||||||
| alt={currentUser.email} | ||||||
| src={getAvatarUrl(currentUser.avatar)} | ||||||
| /> | ||||||
| ) : ( | ||||||
| <AvatarPlaceHolder alt="No profile picture" src={Camera} /> | ||||||
| )} | ||||||
| <ChangeAvatarSection> | ||||||
| Change your avatar on <RedirectToGravatar /> | ||||||
| </ChangeAvatarSection> | ||||||
| </div> | ||||||
| <AvatarContainer> | ||||||
| <AvatarWrapper> | ||||||
| {currentUser.avatar ? ( | ||||||
| <UserImage | ||||||
| alt={currentUser.email} | ||||||
| src={currentUser.avatar} | ||||||
| /> | ||||||
| ) : ( | ||||||
| <AvatarPlaceHolder alt="No profile picture" src={Camera} /> | ||||||
| )} | ||||||
| </AvatarWrapper> | ||||||
| </AvatarContainer> | ||||||
|
|
||||||
| {isGoogleUser && ( | ||||||
| <> | ||||||
| <Tooltip | ||||||
| title="Use Gravatar for a different avatar from your Google photo" | ||||||
| size="regular" | ||||||
| arrow={true} | ||||||
| position="bottom" | ||||||
| > | ||||||
| <i className="fa fa-info-circle"></i> | ||||||
| </Tooltip>{" "} | ||||||
|
||||||
| </Tooltip>{" "} | |
| </Tooltip> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
auth0Pictureandauth0Idfields are not being populated in the response. The frontend components likeAvatarField.tsxandAvatar.tsxdepend on these fields to determine if the user is a Google user and to provide fallback avatar URLs.You should add these fields to the response: