1- import {
2- GetServerSideProps ,
1+ import type {
32 GetStaticPaths ,
43 GetStaticProps ,
5- InferGetServerSidePropsType ,
64 InferGetStaticPropsType ,
75} from 'next' ;
8- import { createServerSideHelpers } from '@trpc/ react-query/server ' ;
6+ import { useState , useEffect , useCallback } from 'react' ;
97import { api } from '~/utils/api' ;
108import { formatDistanceToNow } from 'date-fns' ;
119import styled from '@emotion/styled' ;
@@ -45,11 +43,80 @@ const FileCard = styled('div')({
4543const BrowsePage = ( {
4644 userId,
4745} : InferGetStaticPropsType < typeof getStaticProps > ) => {
48- const { data : filesData } = api . s3 . listUserFiles . useQuery (
49- { userId } ,
50- { enabled : userId !== undefined }
46+ const [ token , setToken ] = useState < string | null > ( null ) ;
47+ const utils = api . useContext ( ) ;
48+
49+ const { data : currentUser } = api . account . getCurrentUser . useQuery ( undefined , {
50+ enabled : ! ! token ,
51+ } ) ;
52+
53+ const signInMutation = api . account . signIn . useMutation ( {
54+ onSuccess : ( data ) => {
55+ setToken ( data . token ) ;
56+ localStorage . setItem ( 'authToken' , data . token ) ;
57+ void utils . s3 . listUserFiles . invalidate ( ) ;
58+ } ,
59+ } ) ;
60+
61+ const { data : filesData , error : filesError } = api . s3 . listUserFiles . useQuery (
62+ undefined ,
63+ { enabled : ! ! token }
5164 ) ;
5265
66+ const handleLogin = useCallback ( async ( ) => {
67+ const email = window . prompt ( 'Email:' ) ;
68+ if ( ! email ) return ;
69+
70+ const password = window . prompt ( 'Password:' ) ;
71+ if ( ! password ) return ;
72+
73+ void signInMutation . mutate ( { email, password } ) ;
74+ } , [ signInMutation ] ) ;
75+
76+ // Check for stored token on mount
77+ useEffect ( ( ) => {
78+ const storedToken = localStorage . getItem ( 'authToken' ) ;
79+ if ( storedToken ) {
80+ if ( ! currentUser || currentUser . name === userId ) {
81+ setToken ( storedToken ) ;
82+ } else {
83+ localStorage . removeItem ( 'authToken' ) ;
84+ }
85+ } else if ( ! signInMutation . isPending && ! signInMutation . error ) {
86+ void handleLogin ( ) ;
87+ }
88+ } , [ currentUser , userId , handleLogin , signInMutation ] ) ;
89+
90+ if ( signInMutation . error ) {
91+ return (
92+ < Container >
93+ < Title > Login Failed</ Title >
94+ < div className = "text-center text-red-500" >
95+ Please refresh to try again.
96+ </ div >
97+ </ Container >
98+ ) ;
99+ }
100+
101+ if ( ! token || ! currentUser ) {
102+ return (
103+ < Container >
104+ < Title > Waiting for authentication...</ Title >
105+ </ Container >
106+ ) ;
107+ }
108+
109+ if ( currentUser . name !== userId ) {
110+ return (
111+ < Container >
112+ < Title > Access Denied</ Title >
113+ < div className = "text-center text-red-500" >
114+ You cannot access files that do not belong to you.
115+ </ div >
116+ </ Container >
117+ ) ;
118+ }
119+
53120 return (
54121 < Container >
55122 < Title > My Files</ Title >
@@ -74,6 +141,11 @@ const BrowsePage = ({
74141 { filesData ?. files . length === 0 && (
75142 < div className = "text-center text-gray-500" > No files found</ div >
76143 ) }
144+ { filesError && (
145+ < div className = "text-center text-red-500" >
146+ Error loading files. Please try again later.
147+ </ div >
148+ ) }
77149 </ Grid >
78150 </ Container >
79151 ) ;
0 commit comments