fix sign out, theme switch, auth provider icons

This commit is contained in:
Max Leiter 2023-04-13 20:13:08 -07:00
parent 55e19381a7
commit d0a73a7cbc
6 changed files with 30 additions and 25 deletions

View file

@ -6,7 +6,7 @@ import Link from "../../../components/link"
import { signIn } from "next-auth/react" import { signIn } from "next-auth/react"
import Input from "@components/input" import Input from "@components/input"
import Button from "@components/button" import Button from "@components/button"
import { Key } from "react-feather" import { GitHub, Key, User } from "react-feather"
import { useToasts } from "@components/toasts" import { useToasts } from "@components/toasts"
import { useRouter } from "next/navigation" import { useRouter } from "next/navigation"
import Note from "@components/note" import Note from "@components/note"
@ -146,7 +146,7 @@ function Auth({
style={{ style={{
color: "var(--fg)" color: "var(--fg)"
}} }}
iconLeft={<Key />} iconLeft={getProviderIcon(provider.id)}
onClick={(e) => { onClick={(e) => {
e.preventDefault() e.preventDefault()
signIn(provider.id, { signIn(provider.id, {
@ -186,3 +186,14 @@ function Auth({
} }
export default Auth export default Auth
const getProviderIcon = (provider: string) => {
switch (provider) {
case "github":
return <GitHub />
case "keycloak":
return <Key />
default:
return <User />
}
}

View file

@ -7,7 +7,7 @@ import { useMemo, useState, useEffect } from "react"
import Badge from "../badge" import Badge from "../badge"
const CreatedAgoBadge = ({ createdAt }: { createdAt: string | Date }) => { const CreatedAgoBadge = ({ createdAt }: { createdAt: string | Date }) => {
const createdDate = useMemo(() => new Date(createdAt), [createdAt]) const createdDate = new Date(createdAt)
const [time, setTimeAgo] = useState(timeAgo(createdDate)) const [time, setTimeAgo] = useState(timeAgo(createdDate))
const { setToast } = useToasts() const { setToast } = useToasts()

View file

@ -36,16 +36,11 @@ const ExpirationBadge = ({
} }
}, [expirationDate]) }, [expirationDate])
const isExpired = useMemo(() => {
if (!expirationDate) {
return false
}
return expirationDate < new Date()
}, [expirationDate])
if (!expirationDate) { if (!expirationDate) {
return null return null
} }
const isExpired = expirationDate < new Date()
return ( return (
<Badge type={isExpired ? "error" : "warning"}> <Badge type={isExpired ? "error" : "warning"}>

View file

@ -1,6 +1,6 @@
"use client" "use client"
import { useSelectedLayoutSegment } from "next/navigation" import { useSelectedLayoutSegment, useSelectedLayoutSegments } from "next/navigation"
import FadeIn from "@components/fade-in" import FadeIn from "@components/fade-in"
import { setDriftTheme } from "src/app/lib/set-theme" import { setDriftTheme } from "src/app/lib/set-theme"
import { import {
@ -40,27 +40,23 @@ type Tab = {
export function HeaderButtons({ export function HeaderButtons({
isAuthenticated, isAuthenticated,
theme theme: initialTheme
}: { }: {
isAuthenticated: boolean isAuthenticated: boolean
theme: string theme: string
}) { }) {
const { isAdmin, userId } = useSessionSWR() const { isAdmin, userId } = useSessionSWR()
const { resolvedTheme } = useTheme();
return ( return getButtons({
<>
{getButtons({
isAuthenticated, isAuthenticated,
theme, theme: resolvedTheme ? resolvedTheme : initialTheme,
isAdmin, isAdmin,
userId userId
})} })
</>
)
} }
function NavButton(tab: Tab) { function NavButton(tab: Tab) {
const segment = useSelectedLayoutSegment() const segment = useSelectedLayoutSegments().slice(-1)[0]
const isActive = segment === tab.value.toLowerCase() const isActive = segment === tab.value.toLowerCase()
const activeStyle = isActive ? styles.active : undefined const activeStyle = isActive ? styles.active : undefined
if (tab.onClick) { if (tab.onClick) {

View file

@ -1,8 +1,9 @@
import { THEME_COOKIE } from "@lib/constants" import { THEME_COOKIE } from "@lib/constants"
import { Cookies } from "react-cookie" import { Cookies } from "react-cookie"
const cookies = new Cookies()
export function setDriftTheme(theme: string, setter: (theme: string) => void) { export function setDriftTheme(theme: string, setter: (theme: string) => void) {
setter(theme) setter(theme)
const cookies = new Cookies()
cookies.set(THEME_COOKIE, theme, { path: "/" }) cookies.set(THEME_COOKIE, theme, { path: "/" })
} }

View file

@ -36,6 +36,7 @@ const credentialsOptions = () => {
return options return options
} }
const providers = () => { const providers = () => {
const providers = [] const providers = []
@ -71,15 +72,16 @@ const providers = () => {
} }
}) })
const originalKeycloakProfile = keycloak.profile const originalKeycloakProfile = keycloak.profile
;(keycloak.profile = async (profile, tokens) => { keycloak.profile = async (profile, tokens) => {
const originalProfile = await originalKeycloakProfile(profile, tokens) const originalProfile = await originalKeycloakProfile(profile, tokens)
const newProfile: User & { displayName?: string | null } = { const newProfile: User & { displayName?: string | null } = {
...originalProfile, ...originalProfile,
displayName: originalProfile.name ?? null displayName: originalProfile.name ?? null
} }
return newProfile return newProfile
}), }
providers.push(keycloak)
providers.push(keycloak)
} }
if (isCredentialEnabled()) { if (isCredentialEnabled()) {