Simple node.js script fails getSession(), insert()
Trying (for 2 hours) to get a node.js script to insert to a RLS table. Narrowed down my code to the simplest helloworld I can think of.
I have added the template policy "Enable insert for authenticated users only".
I don't understand why getSession() returns null and insert() throws Unauthorized:
Output:
I also tried with setSession(session) without success.
Why does getSession() return null after login success?
I have added the template policy "Enable insert for authenticated users only".
I don't understand why getSession() returns null and insert() throws Unauthorized:
async function doit() {
const supabase = await createClient(my_supa_host, my_anon_key);
const sign_in_data = await supabase.auth.signInWithPassword({
email, password
});
console.log('sign in data:');
console.log(sign_in_data);
const sess_data = await supabase.auth.getSession();
console.log('sess data:');
console.log(sess_data);
const insert_resp = await supabase
.from('txns_eth')
.insert({blockNum: '1234', hash: 'asdf'});
console.log('insert response:');
console.log(insert_resp);
console.log('Done.');
}
doit();async function doit() {
const supabase = await createClient(my_supa_host, my_anon_key);
const sign_in_data = await supabase.auth.signInWithPassword({
email, password
});
console.log('sign in data:');
console.log(sign_in_data);
const sess_data = await supabase.auth.getSession();
console.log('sess data:');
console.log(sess_data);
const insert_resp = await supabase
.from('txns_eth')
.insert({blockNum: '1234', hash: 'asdf'});
console.log('insert response:');
console.log(insert_resp);
console.log('Done.');
}
doit();Output:
sign in data:
{
data: {
session: {
access_token: 'REDACTED',
token_type: 'bearer',
expires_in: 86400,
refresh_token: 'REDACTED',
user: [Object],
expires_at: 1670435411
},
user: {
id: 'REDACTED',
aud: 'authenticated',
role: 'authenticated',
email: 'REDACTED@yahoo.com',
email_confirmed_at: '2022-12-06T03:30:19.466461Z',
phone: '',
confirmation_sent_at: '2022-12-06T03:29:58.480631Z',
confirmed_at: '2022-12-06T03:30:19.466461Z',
last_sign_in_at: '2022-12-06T17:50:11.113006993Z',
app_metadata: [Object],
user_metadata: {},
identities: [Array],
created_at: '2022-12-06T03:29:58.476374Z',
updated_at: '2022-12-06T17:50:11.114704Z'
}
},
error: null
}
sess data:
{ data: { session: null }, error: null }
insert response:
{
error: {
code: '42501',
details: null,
hint: null,
message: 'new row violates row-level security policy for table "txns_eth"'
},
data: null,
count: null,
status: 401,
statusText: 'Unauthorized'
}
Done.sign in data:
{
data: {
session: {
access_token: 'REDACTED',
token_type: 'bearer',
expires_in: 86400,
refresh_token: 'REDACTED',
user: [Object],
expires_at: 1670435411
},
user: {
id: 'REDACTED',
aud: 'authenticated',
role: 'authenticated',
email: 'REDACTED@yahoo.com',
email_confirmed_at: '2022-12-06T03:30:19.466461Z',
phone: '',
confirmation_sent_at: '2022-12-06T03:29:58.480631Z',
confirmed_at: '2022-12-06T03:30:19.466461Z',
last_sign_in_at: '2022-12-06T17:50:11.113006993Z',
app_metadata: [Object],
user_metadata: {},
identities: [Array],
created_at: '2022-12-06T03:29:58.476374Z',
updated_at: '2022-12-06T17:50:11.114704Z'
}
},
error: null
}
sess data:
{ data: { session: null }, error: null }
insert response:
{
error: {
code: '42501',
details: null,
hint: null,
message: 'new row violates row-level security policy for table "txns_eth"'
},
data: null,
count: null,
status: 401,
statusText: 'Unauthorized'
}
Done.I also tried with setSession(session) without success.
Why does getSession() return null after login success?