1import { User, Admin } from 'pocketbase';
2import React, { useEffect } from 'react'
3import { useQueryClient } from 'react-query';
4import { Navigate, useNavigate, useSearchParams } from 'react-router-dom';
5import { client } from '../../pb/config';
6import { LoadingShimmer } from '../Shared/loading/LoadingShimmer';
7
8
9interface RedirectProps {
10user?: User | Admin | null
11}
12
13export const Redirect: React.FC<RedirectProps> = ({user}) => {
14const [loading, setLoading] = React.useState(true)
15const queryClient = useQueryClient()
16const navigate = useNavigate()
17const [searchParams] = useSearchParams();
18const code = searchParams.get('code') as string
19const local_prov = JSON.parse(localStorage.getItem('provider') as string)
20
21let redirectUrl = 'http://localhost:3000/redirect'
22useEffect(()=>{
23 if (local_prov.state !== searchParams.get("state")) {
24 const url = 'http://localhost:3000/login'
25 if (typeof window !== 'undefined') {
26 window.location.href = url;
27 }
28 }
29 else {
30
31 client.users.authViaOAuth2(
32 local_prov.name,
33 code,
34 local_prov.codeVerifier,
35 redirectUrl
36 )
37 .then((response) => {
38
39
40
41 client.records.update('profiles', response.user.profile?.id as string, {
42 name: response.meta.name,
43 avatarUrl: response.meta.avatarUrl,
44
45 }).then((res) => {
46
47
48 }).catch((e) => {
49 console.log("error updating profile == ", e)
50 })
51 setLoading(false)
52
53 queryClient.setQueryData(['user'], client.authStore.model)
54 navigate('/')
55
56 }).catch((e) => {
57 console.log("error logging in with provider == ", e)
58 })
59 }
60
61},[])
62if (user) {
63 return <Navigate to="/" replace />;
64}
65return (
66 <div className='w-full h-full '>
67 {loading ? <LoadingShimmer/>:null}
68 </div>
69);
70}
71