mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
fix: fetch session profile from DB
This commit is contained in:
@@ -34,10 +34,10 @@ export default function ProfileSettingsForm({ user }: { user: User }) {
|
|||||||
</form>
|
</form>
|
||||||
<Card className="mt-4 p-4">
|
<Card className="mt-4 p-4">
|
||||||
<p>
|
<p>
|
||||||
Storage: {user.storageUsed ? formatBytes(user.storageUsed) : "N/A"} /{" "}
|
Storage Used: {formatBytes(user.storageUsed)} /{" "}
|
||||||
{user.storageLimit && user.storageLimit > 0 ? formatBytes(user.storageLimit) : "Unlimited"}
|
{user.storageLimit > 0 ? formatBytes(user.storageLimit) : "Unlimited"}
|
||||||
</p>
|
</p>
|
||||||
<p>Tokens Balance: {user.tokenBalance ? formatNumber(user.tokenBalance) : "N/A"}</p>
|
<p>Tokens Balance: {formatNumber(user.tokenBalance)}</p>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ export default function SidebarUser({ profile, isSelfHosted }: { profile: UserPr
|
|||||||
<DropdownMenuItem asChild>
|
<DropdownMenuItem asChild>
|
||||||
<Link href="/settings/profile" className="flex items-center gap-2">
|
<Link href="/settings/profile" className="flex items-center gap-2">
|
||||||
<HardDrive className="h-4 w-4" />
|
<HardDrive className="h-4 w-4" />
|
||||||
Storage: {profile.storageUsed ? formatBytes(profile.storageUsed) : "N/A"}
|
Storage: {formatBytes(profile.storageUsed)}
|
||||||
</Link>
|
</Link>
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
</DropdownMenuGroup>
|
</DropdownMenuGroup>
|
||||||
|
|||||||
30
lib/auth.ts
30
lib/auth.ts
@@ -1,6 +1,6 @@
|
|||||||
import config from "@/lib/config"
|
import config from "@/lib/config"
|
||||||
import { createUserDefaults } from "@/models/defaults"
|
import { createUserDefaults } from "@/models/defaults"
|
||||||
import { getSelfHostedUser, getUserByEmail } from "@/models/users"
|
import { getSelfHostedUser, getUserByEmail, getUserById } from "@/models/users"
|
||||||
import { stripe } from "@better-auth/stripe"
|
import { stripe } from "@better-auth/stripe"
|
||||||
import { User } from "@prisma/client"
|
import { User } from "@prisma/client"
|
||||||
import { betterAuth } from "better-auth"
|
import { betterAuth } from "better-auth"
|
||||||
@@ -19,8 +19,9 @@ export type UserProfile = {
|
|||||||
name: string
|
name: string
|
||||||
email: string
|
email: string
|
||||||
avatar?: string
|
avatar?: string
|
||||||
storageUsed?: number
|
storageUsed: number
|
||||||
tokenBalance?: number
|
storageLimit: number
|
||||||
|
tokenBalance: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export const auth = betterAuth({
|
export const auth = betterAuth({
|
||||||
@@ -91,13 +92,24 @@ export async function getSession() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getCurrentUser(): Promise<User> {
|
export async function getCurrentUser(): Promise<User> {
|
||||||
const session = await getSession()
|
if (config.selfHosted.isEnabled) {
|
||||||
if (!session || !session.user) {
|
const user = await getSelfHostedUser()
|
||||||
if (config.selfHosted.isEnabled) {
|
if (user) {
|
||||||
redirect(config.selfHosted.redirectUrl)
|
return user
|
||||||
} else {
|
} else {
|
||||||
redirect(config.auth.loginUrl)
|
redirect(config.selfHosted.redirectUrl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return session.user as User
|
|
||||||
|
// Try to return user from session
|
||||||
|
const session = await getSession()
|
||||||
|
if (session && session.user) {
|
||||||
|
const user = await getUserById(session.user.id)
|
||||||
|
if (user) {
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No session or user found
|
||||||
|
redirect(config.auth.loginUrl)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,12 @@ export const createSelfHostedUser = cache(async () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const getUserById = cache(async (id: string) => {
|
||||||
|
return await prisma.user.findUnique({
|
||||||
|
where: { id },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
export const getUserByEmail = cache(async (email: string) => {
|
export const getUserByEmail = cache(async (email: string) => {
|
||||||
return await prisma.user.findUnique({
|
return await prisma.user.findUnique({
|
||||||
where: { email },
|
where: { email },
|
||||||
|
|||||||
Reference in New Issue
Block a user