The Noah
04/03/2022, 1:08 AMsrc/hooks.ts
. The user data is correctly fetched using supabase.auth.api.getUser(accessToken)
.
The issue comes in when I try to query my profiles
table. I query it by the user's id
. The profiles
table id
column is related to the auth.users
id
column. Everything properly matches, but the query returns nothing. This is where things start getting strange. I have a profiles.ts
file with helper functions which is what I use to call to Supabase. If I edit this file, SvelteKit hot reloads and the function then returns data as it should. However, just reloading the page does not make it return properly. As a sanity check I made it return the entire table instead of just the correct user, and it just returns an empty array.
I don't think I've ever run into an error quite like this, any help would be much appreciated.Needle
04/03/2022, 1:08 AM/title
command!
We have solved your problem?
Click the button below to archive it.garyaustin
04/03/2022, 1:21 AMsilentworks
04/03/2022, 7:21 PMsilentworks
04/03/2022, 7:22 PMcbert
04/03/2022, 8:10 PMsilentworks
04/03/2022, 8:14 PMcbert
04/03/2022, 8:15 PMcbert
04/03/2022, 8:16 PMcbert
04/03/2022, 8:16 PMsilentworks
04/03/2022, 8:17 PMcbert
04/03/2022, 8:17 PMcbert
04/03/2022, 8:17 PMsilentworks
04/03/2022, 8:18 PMcbert
04/03/2022, 8:24 PMThe Noah
04/03/2022, 10:49 PMts
// hooks.ts
if (cookies["access-token"]) {
const {user} = await db.supabase.auth.api.getUser(cookies["access-token"]);
if (user) {
event.locals.supabaseUser = user;
console.log("user id:", user.id);
const profile = await db.profiles.get({id: user.id});
console.log("profile:", profile);
event.locals.user = profile;
}
}
ts
// db/profiles.ts
export async function get(query: Record<string, unknown>): Promise<db.IProfile> {
const {data, error} = await db.supabase
.from("profiles")
.select("*, links (id, url, name)")
.match(query)
.single();
if (error) {
console.error("DB get error [profiles]", query, error);
return undefined;
}
return data;
}
silentworks
04/03/2022, 10:55 PMThe Noah
04/03/2022, 11:05 PMts
export async function handle({event, resolve}: {event: RequestEvent; resolve: any}) {
const cookies = cookie.parse(event.request.headers.get("cookie") || "");
if (cookies["access-token"]) {
const {user} = await db.supabase.auth.api.getUser(cookies["access-token"]);
if (user) {
event.locals.supabaseUser = user;
console.log("user id:", user.id);
const profile = await db.profiles.get({id: user.id});
console.log("profile:", profile);
event.locals.user = profile;
}
}
return await resolve(event);
}
silentworks
04/03/2022, 11:10 PMThe Noah
04/03/2022, 11:11 PMThe Noah
04/03/2022, 11:13 PMsilentworks
04/03/2022, 11:14 PMThe Noah
04/03/2022, 11:14 PMts
export async function handle({event, resolve}: {event: RequestEvent; resolve: any}) {
const cookies = cookie.parse(event.request.headers.get("cookie") || "");
if (cookies["access-token"]) {
const {user} = await db.supabase.auth.api.getUser(cookies["access-token"]);
if (user) {
event.locals.supabaseUser = user;
console.log("user id:", user.id);
const profile = await db.profiles.get({id: user.id});
console.log("profile:", profile);
event.locals.user = profile;
db.supabase.auth.setAuth(cookies["access-token"]);
}
}
return await resolve(event);
}
The Noah
04/03/2022, 11:14 PMThe Noah
04/03/2022, 11:15 PMts
export async function handle({event, resolve}: {event: RequestEvent; resolve: any}) {
const cookies = cookie.parse(event.request.headers.get("cookie") || "");
if (cookies["access-token"]) {
const {user} = await db.supabase.auth.api.getUser(cookies["access-token"]);
if (user) {
db.supabase.auth.setAuth(cookies["access-token"]);
event.locals.supabaseUser = user;
console.log("user id:", user.id);
const profile = await db.profiles.get({id: user.id});
console.log("profile:", profile);
event.locals.user = profile;
}
}
return await resolve(event);
}
Here we go. This still doesn't work though.silentworks
04/03/2022, 11:16 PMThe Noah
04/03/2022, 11:16 PMdb.profiles.get
doesn't return the user.silentworks
04/03/2022, 11:17 PMdb.supabase.auth.setAuth
outside of the if (user)
silentworks
04/03/2022, 11:18 PMThe Noah
04/03/2022, 11:20 PMprofiles.ts
which causes it to hot-reload then it works just fine so idk why it's doing that.silentworks
04/03/2022, 11:22 PMThe Noah
04/03/2022, 11:22 PMsilentworks
04/03/2022, 11:23 PMsilentworks
04/03/2022, 11:24 PMsilentworks
04/03/2022, 11:25 PMThe Noah
04/03/2022, 11:25 PMsilentworks
04/03/2022, 11:28 PMThe Noah
04/03/2022, 11:28 PMsilentworks
04/03/2022, 11:28 PMThe Noah
04/03/2022, 11:29 PMsilentworks
04/03/2022, 11:33 PMsilentworks
04/03/2022, 11:33 PMsilentworks
04/03/2022, 11:34 PMservice_role
with the db.supabase
object?The Noah
04/03/2022, 11:45 PMgaryaustin
04/03/2022, 11:51 PMThe Noah
04/04/2022, 12:02 AMThe Noah
04/04/2022, 12:09 AMdb.supabase.auth.setAuth(config.supabase.key)
after const {user} = await db.supabase.auth.api.getUser(cookies["access-token"])
seems to have fixed the issue. For some reason getUser
was changing which JWT Supabase used for future requests.silentworks
04/04/2022, 12:10 AMdb.supabase.auth.setAuth
does that not db.supabase.auth.api.getUser
silentworks
04/04/2022, 12:11 AMuser.id
from the profiles table.The Noah
04/04/2022, 12:11 AMgetUser
fixes it π€·ββοΈsilentworks
04/04/2022, 12:13 AMThe Noah
04/04/2022, 12:13 AMThe Noah
04/04/2022, 12:13 AMgetUser
and I didn't see any variable assignments which is quite odd.The Noah
04/04/2022, 12:14 AMgaryaustin
04/04/2022, 12:14 AMsilentworks
04/04/2022, 12:14 AMgaryaustin
04/04/2022, 12:14 AMgaryaustin
04/04/2022, 12:14 AMgaryaustin
04/04/2022, 12:15 AMgaryaustin
04/04/2022, 12:15 AMsilentworks
04/04/2022, 12:15 AMgaryaustin
04/04/2022, 12:16 AMsilentworks
04/04/2022, 12:16 AMgaryaustin
04/04/2022, 12:16 AMsilentworks
04/04/2022, 12:16 AMservice_role
in the browsergaryaustin
04/04/2022, 12:17 AMgaryaustin
04/04/2022, 12:17 AMgaryaustin
04/04/2022, 12:17 AMgaryaustin
04/04/2022, 12:17 AMsilentworks
04/04/2022, 12:18 AMsilentworks
04/04/2022, 12:19 AMgaryaustin
04/04/2022, 12:19 AMsilentworks
04/04/2022, 12:24 AMgaryaustin
04/04/2022, 12:41 AMgaryaustin
04/04/2022, 1:06 AMgaryaustin
04/04/2022, 1:25 AMgaryaustin
04/04/2022, 1:27 AMThe Noah
04/04/2022, 1:28 AMgaryaustin
04/04/2022, 1:32 AMgaryaustin
04/04/2022, 1:38 AMThe Noah
04/04/2022, 1:42 AMOlyno
04/04/2022, 9:25 AMcbert
04/04/2022, 9:27 AMsilentworks
04/04/2022, 10:00 AM