Skip to content

Commit

Permalink
feat: blur on no password
Browse files Browse the repository at this point in the history
  • Loading branch information
rharkor committed Sep 14, 2023
1 parent aab7096 commit 76f4e43
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/app/[lang]/(protected)/profile/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default async function Profile({
</div>
</CardFooter>
</Card>
<SeeDetailsToggle dictionary={dictionary} />
<SeeDetailsToggle dictionary={dictionary} session={session} />
</div>
</main>
)
Expand Down
11 changes: 9 additions & 2 deletions src/app/[lang]/(protected)/profile/see-details-toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Session } from "next-auth"
import ProfileDetails from "@/components/profile/profile-details"
import UserActiveSessions from "@/components/profile/sessions/user-active-sessions"
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"
import { TDictionary } from "@/lib/langs"

export default function SeeDetailsToggle({ dictionary }: { dictionary: TDictionary }) {
export default function SeeDetailsToggle({
dictionary,
session,
}: {
dictionary: TDictionary
session: Session | null
}) {
return (
<div className="-mt-4 rounded-b-lg border bg-card p-2 pt-6 text-muted-foreground shadow-sm">
<Accordion type="single" collapsible>
Expand All @@ -12,7 +19,7 @@ export default function SeeDetailsToggle({ dictionary }: { dictionary: TDictiona
{dictionary.profilePage.profileDetails.toggle}
</AccordionTrigger>
<AccordionContent>
<UserActiveSessions dictionary={dictionary} />
<UserActiveSessions dictionary={dictionary} session={session} />
<ProfileDetails dictionary={dictionary} />
</AccordionContent>
</AccordionItem>
Expand Down
13 changes: 10 additions & 3 deletions src/components/profile/sessions/sessions-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import SessionRow from "./session-row"

const itemsPerPageInitial = 5

export default function SessionsTable({ dictionary }: { dictionary: TDictionary }) {
export default function SessionsTable({ dictionary, isDisabled }: { dictionary: TDictionary; isDisabled?: boolean }) {
const router = useRouter()
const utils = trpc.useContext()

Expand All @@ -35,7 +35,9 @@ export default function SessionsTable({ dictionary }: { dictionary: TDictionary
page: currentPage,
perPage: itemsPerPage,
}
const activeSessions = useActiveSessions(dictionary, callParams)
const activeSessions = useActiveSessions(dictionary, callParams, {
disabled: isDisabled,
})
const [meta, setMeta] = useState<IMeta | undefined>(activeSessions.data?.meta)

useEffect(() => {
Expand Down Expand Up @@ -81,7 +83,7 @@ export default function SessionsTable({ dictionary }: { dictionary: TDictionary
const showPagination = Boolean((meta && meta.totalPages > 1) || itemsPerPageInitial !== itemsPerPage)

return (
<div className="mt-4 flex flex-col space-y-4">
<div className="relative mt-4 flex flex-col space-y-4">
<AlertDialog>
{activeSessions.isFetched ? rows : skelRows}
<Pagination
Expand All @@ -108,6 +110,11 @@ export default function SessionsTable({ dictionary }: { dictionary: TDictionary
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{isDisabled && (
<div className="absolute inset-0 !mt-0 flex flex-col items-center justify-center backdrop-blur-sm">
<p className="text-sm text-muted-foreground">{dictionary.profilePage.unavailableWithOAuth}</p>
</div>
)}
</div>
)
}
28 changes: 9 additions & 19 deletions src/components/profile/sessions/user-active-sessions.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { Session } from "next-auth"
import { TDictionary } from "@/lib/langs"
import SessionsTable from "./sessions-table"

/*
...dictionary.profilePage.profileDetails,
sessionTable: {
areYouAbsolutelySure: dictionary.areYouAbsolutelySure,
cancel: dictionary.cancel,
continue: dictionary.continue,
deleteLoggedDevice: dictionary.profilePage.profileDetails.deleteLoggedDevice,
session: dictionary.profilePage.profileDetails.session,
sessions: dictionary.profilePage.profileDetails.sessions,
error: dictionary.error,
delete: dictionary.delete,
fetch: dictionary.fetch,
couldNotMessage: dictionary.couldNotMessage,
},
}}
*/

export default function UserActiveSessions({ dictionary }: { dictionary: TDictionary }) {
export default async function UserActiveSessions({
dictionary,
session,
}: {
dictionary: TDictionary
session: Session | null
}) {
return (
<section className="p-2 text-foreground">
<header>
Expand All @@ -27,7 +17,7 @@ export default function UserActiveSessions({ dictionary }: { dictionary: TDictio
{dictionary.profilePage.profileDetails.loggedDevicesDescription}
</p>
</header>
<SessionsTable dictionary={dictionary} />
<SessionsTable dictionary={dictionary} isDisabled={session?.user.hasPassword === false} />
</section>
)
}
2 changes: 2 additions & 0 deletions src/contexts/active-sessions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export function useActiveSessions(
params: Parameters<typeof trpc.me.getActiveSessions.useQuery>["0"] = {},
extendedOptions?: {
initialData?: z.infer<ReturnType<typeof getActiveSessionsResponseSchema>>
disabled?: boolean
}
) {
const router = useRouter()
const activeSessionsQuery = trpc.me.getActiveSessions.useQuery(params, {
initialData: extendedOptions?.initialData,
enabled: !(extendedOptions?.disabled ?? false),
onError(error) {
handleQueryError(error, dictionary, router)
},
Expand Down
3 changes: 2 additions & 1 deletion src/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"created": "Created",
"expires": "Expires",
"in": "in"
}
},
"unavailableWithOAuth": "This feature is not available with OAuth providers."
},
"auth": {
"orContinueWith": "Or continue with",
Expand Down
3 changes: 2 additions & 1 deletion src/langs/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"created": "Créé",
"expires": "Expire",
"in": "dans"
}
},
"unavailableWithOAuth": "Cette fonctionnalité n'est pas disponible avec ce type de connexion."
},
"auth": {
"orContinueWith": "Ou continuez avec",
Expand Down
10 changes: 9 additions & 1 deletion src/lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const nextAuthOptions: NextAuthOptions & {
},
authorize: async (credentials, req) => {
const referer = (req.headers?.referer as string | undefined) ?? ""
const refererUrl = ensureRelativeUrl(referer) ?? ""
const refererUrl = ensureRelativeUrl(referer)
const lang = i18n.locales.find((locale) => refererUrl.startsWith(`/${locale}/`)) ?? i18n.defaultLocale
const dictionary =
nextAuthOptions.loadedDictionary.get(lang) ??
Expand Down Expand Up @@ -127,6 +127,7 @@ export const nextAuthOptions: NextAuthOptions & {
if (isPossiblyUndefined(user)) {
token.id = user.id
token.email = user.email
if ("hasPassword" in user) token.hasPassword = user.hasPassword as boolean
if ("username" in user) token.username = user.username
if ("role" in user) token.role = user.role as string
if ("uuid" in user) token.uuid = user.uuid as string
Expand Down Expand Up @@ -154,6 +155,11 @@ export const nextAuthOptions: NextAuthOptions & {
}

//* Verify that the session still exists
if (dbUser.hasPassword && (!token.uuid || typeof token.uuid !== "string")) {
logger.debug("Missing token uuid")
return {} as Session
}

if (token.uuid) {
const loginSession = await prisma.session.findUnique({
where: {
Expand All @@ -179,6 +185,7 @@ export const nextAuthOptions: NextAuthOptions & {
//* Fill session with user data
const username = dbUser.username
const role = dbUser.role
const hasPassword = dbUser.hasPassword

//* Fill session with token data
const uuid = "uuid" in token ? token.uuid : undefined
Expand All @@ -191,6 +198,7 @@ export const nextAuthOptions: NextAuthOptions & {
username: username ?? undefined,
role,
uuid,
hasPassword,
},
}
return sessionFilled
Expand Down
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare module "next-auth" {
uuid: string
username?: string
role: string
hasPassword: boolean
}
}
}
Expand All @@ -16,6 +17,7 @@ declare module "next-auth/jwt/types" {
interface JWT {
uuid: string
role: string
hasPassword: boolean
}
}

Expand Down

0 comments on commit 76f4e43

Please sign in to comment.