S
SolidJS9mo ago
sh1man

what write so as not to constantly write a redirect in every function in case of unauthorize

I don’t have a database on the solidstart server, I only work with third-party APIs. Where to store user information. I want not to write in some functions constantly the same code for processing unauthorized. You need to write a wrapper function?
export const getUser = cache(async () => {
"use server";
try {
const session = await getSession();
const userId = session.data.userId;
if (userId === undefined) throw new Error("User not found");
const user = await db.user.findUnique({ where: { id: userId } });
if (!user) throw new Error("User not found");
return { id: user.id, username: user.username };
} catch {
await logoutSession();
throw redirect("/login");
}
}, "user");
export const getUser = cache(async () => {
"use server";
try {
const session = await getSession();
const userId = session.data.userId;
if (userId === undefined) throw new Error("User not found");
const user = await db.user.findUnique({ where: { id: userId } });
if (!user) throw new Error("User not found");
return { id: user.id, username: user.username };
} catch {
await logoutSession();
throw redirect("/login");
}
}, "user");
13 Replies
Dog
Dog9mo ago
Are you using supabase and @solid-mediakit/auth?
sh1man
sh1manOP9mo ago
no i use lucia everything works fine for me
Dog
Dog9mo ago
oh ok do you use supabase with lucia?
sh1man
sh1manOP9mo ago
no, i use prisma
Dog
Dog9mo ago
because I'm trying to implement row level security with supabase and can't get it to work ok I think I have another project that used prisma let me check
sh1man
sh1manOP9mo ago
I did it like this
No description
No description
Dog
Dog9mo ago
export const authOpts: SolidAuthConfig = {
adapter: PrismaAdapter(prisma),
providers: [
Google({
clientId: serverEnv.GOOGLE_CLIENT_ID,
clientSecret: serverEnv.GOOGLE_CLIENT_SECRET,
})
],
callbacks: {
async session({ session, user }) {
if (session.user) {
session.user.id = user.id;
}
const signingSecret = serverEnv.SUPABASE_JWT_SECRET
if (signingSecret) {
const payload = {
aud: "authenticated",
exp: Math.floor(new Date(session.expires).getTime() / 1000),
// sub: user.id,
email: user.email,
role: "authenticated",
}
session.supabaseAccessToken = jwt.sign(payload, signingSecret)
}
return session
},
},
debug: false,
};
export const authOpts: SolidAuthConfig = {
adapter: PrismaAdapter(prisma),
providers: [
Google({
clientId: serverEnv.GOOGLE_CLIENT_ID,
clientSecret: serverEnv.GOOGLE_CLIENT_SECRET,
})
],
callbacks: {
async session({ session, user }) {
if (session.user) {
session.user.id = user.id;
}
const signingSecret = serverEnv.SUPABASE_JWT_SECRET
if (signingSecret) {
const payload = {
aud: "authenticated",
exp: Math.floor(new Date(session.expires).getTime() / 1000),
// sub: user.id,
email: user.email,
role: "authenticated",
}
session.supabaseAccessToken = jwt.sign(payload, signingSecret)
}
return session
},
},
debug: false,
};
export const useSession = () => {
return createServerData$(
async (_, { request }) => {
return await getSession(request, authOpts)
},
{ key: () => ["auth_user"] }
)
}

const AuthShowcase: VoidComponent = () => {
const session = useSession();
return (
<div>
<Show
when={session()}
fallback={
<button
onClick={() => signIn("google", { redirect: false })}
>
Sign in
</button>
}
>
<span>Welcome {session()?.user?.name}</span>
<button
onClick={() => signOut({ redirect: true, redirectTo: "/" })}
>
Sign out
</button>
</Show>
</div>
);
};
export const useSession = () => {
return createServerData$(
async (_, { request }) => {
return await getSession(request, authOpts)
},
{ key: () => ["auth_user"] }
)
}

const AuthShowcase: VoidComponent = () => {
const session = useSession();
return (
<div>
<Show
when={session()}
fallback={
<button
onClick={() => signIn("google", { redirect: false })}
>
Sign in
</button>
}
>
<span>Welcome {session()?.user?.name}</span>
<button
onClick={() => signOut({ redirect: true, redirectTo: "/" })}
>
Sign out
</button>
</Show>
</div>
);
};
I was doing somethig like this to get the session user data but this was sing the old solid-start
sh1man
sh1manOP9mo ago
I didn't work with @solid-mediakit/auth and supabase. I can't help with this
Dog
Dog9mo ago
no this is what you can do for your question, do the <Show when={session} or did you all ready solve this?
sh1man
sh1manOP9mo ago
yes
Dog
Dog9mo ago
oh my bad I thought you hadn't
sh1man
sh1manOP9mo ago
Enough time has passed)
Dog
Dog9mo ago
I normally get stuck on problems for weeks 😆
Want results from more Discord servers?
Add your server