feature(auth): replace clerk.com with custom auth (#103)
* feature(auth): replace clerk.com with custom auth * minor fixes * remove notification preferences * decrease live events interval fix(api): cookies.. # Conflicts: # .gitignore # apps/api/src/index.ts # apps/dashboard/src/app/providers.tsx # packages/trpc/src/trpc.ts
This commit is contained in:
committed by
Carl-Gerhard Lindesvärd
parent
f28802b1c2
commit
d31d9924a5
5
apps/dashboard/src/hooks/useAuth.tsx
Normal file
5
apps/dashboard/src/hooks/useAuth.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import { api } from '@/trpc/client';
|
||||
|
||||
export function useAuth() {
|
||||
return api.auth.session.useQuery();
|
||||
}
|
||||
15
apps/dashboard/src/hooks/useLogout.ts
Normal file
15
apps/dashboard/src/hooks/useLogout.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { api } from '@/trpc/client';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
export function useLogout() {
|
||||
const router = useRouter();
|
||||
const signOut = api.auth.signOut.useMutation({
|
||||
onSuccess() {
|
||||
setTimeout(() => {
|
||||
router.push('/login');
|
||||
}, 0);
|
||||
},
|
||||
});
|
||||
|
||||
return () => signOut.mutate();
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
'use client';
|
||||
|
||||
import { useAuth } from '@clerk/nextjs';
|
||||
import debounce from 'lodash.debounce';
|
||||
import { use, useEffect, useMemo, useState } from 'react';
|
||||
import useWebSocket from 'react-use-websocket';
|
||||
@@ -18,19 +17,10 @@ export default function useWS<T>(
|
||||
onMessage: (event: T) => void,
|
||||
options?: UseWSOptions,
|
||||
) {
|
||||
const auth = useAuth();
|
||||
const ws = String(process.env.NEXT_PUBLIC_API_URL)
|
||||
.replace(/^https/, 'wss')
|
||||
.replace(/^http/, 'ws');
|
||||
const [baseUrl, setBaseUrl] = useState(`${ws}${path}`);
|
||||
const [token, setToken] = useState<string | null>(null);
|
||||
const socketUrl = useMemo(() => {
|
||||
const parseUrl = new URL(baseUrl);
|
||||
if (token) {
|
||||
parseUrl.searchParams.set('token', token);
|
||||
}
|
||||
return parseUrl.toString();
|
||||
}, [baseUrl, token]);
|
||||
|
||||
const debouncedOnMessage = useMemo(() => {
|
||||
if (options?.debounce) {
|
||||
@@ -39,18 +29,12 @@ export default function useWS<T>(
|
||||
return onMessage;
|
||||
}, [options?.debounce?.delay]);
|
||||
|
||||
useEffect(() => {
|
||||
if (auth.isSignedIn) {
|
||||
auth.getToken().then(setToken);
|
||||
}
|
||||
}, [auth]);
|
||||
|
||||
useEffect(() => {
|
||||
if (baseUrl === `${ws}${path}`) return;
|
||||
setBaseUrl(`${ws}${path}`);
|
||||
}, [path, baseUrl, ws]);
|
||||
|
||||
useWebSocket(socketUrl, {
|
||||
useWebSocket(baseUrl, {
|
||||
shouldReconnect: () => true,
|
||||
onMessage(event) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user