Skip to content

Commit

Permalink
feat: user avatars from plex (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
sct committed Sep 6, 2020
1 parent 190a883 commit e6349c1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
3 changes: 3 additions & 0 deletions server/entity/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export class User {
@Column({ type: 'integer', default: 0 })
public permissions = 0;

@Column()
public avatar: string;

@CreateDateColumn()
public createdAt: Date;

Expand Down
2 changes: 2 additions & 0 deletions server/overseerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ components:
permissions:
type: number
example: 0
avatar:
type: string
createdAt:
type: string
example: '2020-09-02T05:02:23.000Z'
Expand Down
4 changes: 4 additions & 0 deletions server/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ authRoutes.post('/login', async (req, res) => {
user.plexToken = body.authToken;
await userRepository.save(user);
}

// Update the users avatar with their plex thumbnail (incase it changed)
user.avatar = account.thumb;
} else {
// Here we check if it's the first user. If it is, we create the user with no check
// and give them admin permissions
Expand All @@ -56,6 +59,7 @@ authRoutes.post('/login', async (req, res) => {
email: account.email,
plexToken: account.authToken,
permissions: Permission.ADMIN,
avatar: account.thumb,
});
await userRepository.save(user);
}
Expand Down
8 changes: 3 additions & 5 deletions src/components/Layout/UserDropdown/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { useState } from 'react';
import Transition from '../../Transition';
import { useUser } from '../../../hooks/useUser';

const UserDropdown: React.FC = () => {
const { user } = useUser();
const [isDropdownOpen, setDropdownOpen] = useState(false);

return (
Expand All @@ -14,11 +16,7 @@ const UserDropdown: React.FC = () => {
aria-haspopup="true"
onClick={() => setDropdownOpen((state) => !state)}
>
<img
className="h-8 w-8 rounded-full"
src="https://avatars1.githubusercontent.com/u/234213?s=460&u=7f30f76bd7bbdab45bab7544ebd80aa88ea11caf&v=4"
alt=""
/>
<img className="h-8 w-8 rounded-full" src={user?.avatar} alt="" />
</button>
</div>
<Transition
Expand Down
6 changes: 5 additions & 1 deletion src/context/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ export const UserContext: React.FC<UserContextProps> = ({
initialUser,
children,
}) => {
const { user } = useUser({ initialData: initialUser });
const { user, revalidate } = useUser({ initialData: initialUser });
const router = useRouter();

useEffect(() => {
revalidate();
}, [router.pathname, revalidate]);

useEffect(() => {
if (!router.pathname.match(/(setup|login)/) && !user) {
router.push('/login');
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useRef } from 'react';
export interface User {
id: number;
email: string;
avatar: string;
permissions: number;
}

interface UserHookResponse {
Expand Down

0 comments on commit e6349c1

Please sign in to comment.