feat: auth using sse
This commit is contained in:
1 parent
b72355f7b8
commit
df0f7c943b
10 files changed
+1101
-292
No files matched your search
@@ -1,12 +1,40 @@
|
||||
'use client';
|
||||
|
||||
import { UserRound } from 'lucide-react';
|
||||
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuGroup,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuLabel,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger
|
||||
} from '@/components/ui/dropdown-menu';
|
||||
|
||||
import useAuth from './useAuth';
|
||||
|
||||
const User = () => {
|
||||
const auth = useAuth();
|
||||
|
||||
return (
|
||||
<Button variant='ghost' size='icon'>
|
||||
<UserRound size={20} />
|
||||
</Button>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant='ghost' size='icon'>
|
||||
<UserRound size={20} />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuGroup>
|
||||
<DropdownMenuItem className='flex justify-center text-center' onClick={() => auth.mutate()}>
|
||||
Log in
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuGroup>
|
||||
{/* <DropdownMenuLabel>My Account</DropdownMenuLabel>
|
||||
<DropdownMenuSeparator /> */}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { createRequestToken } from '@/api';
|
||||
|
||||
const useAuth = () => {
|
||||
const tabRef = useRef<Window | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const approveSource = new EventSource('/api/auth');
|
||||
|
||||
approveSource.onmessage = (event) => {
|
||||
console.log(event.data);
|
||||
tabRef.current?.close();
|
||||
approveSource.close();
|
||||
};
|
||||
|
||||
return () => {
|
||||
approveSource.close();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return useMutation({
|
||||
mutationFn: createRequestToken,
|
||||
onSuccess(requestToken) {
|
||||
const link = `https://www.themoviedb.org/authenticate/${requestToken}?redirect_to=${encodeURIComponent(
|
||||
`${location.origin}/api/approved`
|
||||
)}`;
|
||||
|
||||
tabRef.current = window.open(link, '_blank');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export default useAuth;
|
||||
@@ -52,6 +52,7 @@ const DropdownMenuSubContent = React.forwardRef<
|
||||
{...props}
|
||||
/>
|
||||
));
|
||||
|
||||
DropdownMenuSubContent.displayName = DropdownMenuPrimitive.SubContent.displayName;
|
||||
|
||||
const DropdownMenuContent = React.forwardRef<
|
||||
@@ -63,7 +64,7 @@ const DropdownMenuContent = React.forwardRef<
|
||||
ref={ref}
|
||||
sideOffset={sideOffset}
|
||||
className={cn(
|
||||
'z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
|
||||
'z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
@@ -81,7 +82,7 @@ const DropdownMenuItem = React.forwardRef<
|
||||
<DropdownMenuPrimitive.Item
|
||||
ref={ref}
|
||||
className={cn(
|
||||
'relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50',
|
||||
'relative flex cursor-default select-none items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0',
|
||||
inset && 'pl-8',
|
||||
className
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user