-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: finalize no-auth vs auth experiences, conditional login dropdo…
…wn component, tuning logs
- Loading branch information
Showing
14 changed files
with
222 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,13 @@ | ||
import {verifySession} from 'supertokens-node/recipe/session/framework/express'; | ||
import {middleware, errorHandler} from 'supertokens-node/framework/express'; | ||
import config from '../config/config'; | ||
|
||
const sessionConfig = { | ||
sessionRequired: config.get('requireAuth'), | ||
}; | ||
|
||
export const authMiddleware = { | ||
verify: verifySession({ | ||
sessionRequired: false, | ||
}), | ||
verify: verifySession(sessionConfig), | ||
init: middleware(), | ||
error: errorHandler(), | ||
}; | ||
|
||
// export interface AuthRequest extends Request { | ||
// session: { | ||
// getUserId(): string; | ||
// }; | ||
// } | ||
// | ||
// export const requireAuth = ( | ||
// req: AuthRequest, | ||
// res: express.Response, | ||
// next: express.NextFunction | ||
// ) => { | ||
// if (!req.session) { | ||
// return res.status(401).json({error: 'Unauthorized'}); | ||
// } | ||
// next(); | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
frontend/src/components/ui/conditional-login-dropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import {config, getApiBaseUrl} from '../../config/config'; | ||
import {LogIn, LogOut} from 'lucide-react'; | ||
import {Avatar, AvatarFallback, AvatarImage} from './avatar'; | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from './dropdown-menu'; | ||
import {useNavigate} from 'react-router'; | ||
import {SuperTokensWrapper} from 'supertokens-auth-react'; | ||
import Session, { | ||
useSessionContext, | ||
} from 'supertokens-auth-react/recipe/session'; | ||
import {useEffect, useState} from 'react'; | ||
|
||
const LoginDropdownWithAuthRequired = () => { | ||
const [avatarUrl, setAvatarUrl] = useState<string>('/anonymous-avatar.svg'); | ||
const navigate = useNavigate(); | ||
|
||
async function handleSignOut() { | ||
await Session.signOut(); | ||
navigate('/auth'); | ||
} | ||
|
||
useEffect(() => { | ||
const fetchAvatar = async () => { | ||
try { | ||
const response = await fetch(`${getApiBaseUrl()}/api/avatar`, { | ||
method: 'GET', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
return; | ||
} | ||
|
||
const data = await response.json(); | ||
if (data.avatarUrl) { | ||
setAvatarUrl(data.avatarUrl); | ||
} | ||
} catch (err) { | ||
console.error('Error fetching avatar:', err); | ||
} | ||
}; | ||
|
||
fetchAvatar(); | ||
}, []); | ||
|
||
const sessionContext = useSessionContext(); | ||
if (sessionContext.loading === true) return null; | ||
|
||
const isLoggedIn = !!sessionContext.userId; | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Avatar className="cursor-pointer"> | ||
<AvatarImage src={avatarUrl} /> | ||
</Avatar> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="end" className="w-56"> | ||
{isLoggedIn ? ( | ||
<DropdownMenuItem onSelect={handleSignOut}> | ||
<LogOut className="mr-2 h-4 w-4" /> | ||
<span>Logout</span> | ||
</DropdownMenuItem> | ||
) : ( | ||
<DropdownMenuItem onSelect={() => navigate('/auth')}> | ||
<LogIn className="mr-2 h-4 w-4" /> | ||
<span>Login</span> | ||
</DropdownMenuItem> | ||
)} | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
); | ||
}; | ||
|
||
const LoginDropdownDisabled = () => { | ||
const [avatarUrl] = useState<string>('/anonymous-avatar.svg'); | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Avatar className="cursor-pointer"> | ||
<AvatarImage src={avatarUrl} /> | ||
</Avatar> | ||
</DropdownMenuTrigger> | ||
</DropdownMenu> | ||
); | ||
}; | ||
|
||
const ConditionalLoginDropdown = () => { | ||
return config.requireAuth ? ( | ||
<SuperTokensWrapper> | ||
<LoginDropdownWithAuthRequired /> | ||
</SuperTokensWrapper> | ||
) : ( | ||
<LoginDropdownDisabled /> | ||
); | ||
}; | ||
|
||
export default ConditionalLoginDropdown; |
Oops, something went wrong.