Karthik S K
Karthik S K

Reputation: 40

How to implement non-persisting session In Supabase Auth and Next JS?

I am a beginner to supabase and have implemented login system using supabase authentication and Next JS for the front-end. It works fine, but the issue is the login session, by default, is persisting even after closing the browser window. The behaviour I want to implement is the have a non-persisting user session by default, there by logging the user out if they close the window.

Code I use to auth user:

const { data, error } = await supabase.auth.signInWithPassword({
      email: emailField,
      password: passwordField,
    });

I tried using client side event listeners like beforeunload but it also means that the logout fires if the user reloads the page. I googled and looked through the docs but couldn't spot anything I implement. Appreciate any help! Thanks in advance.

Upvotes: 0

Views: 984

Answers (1)

thorwebdev
thorwebdev

Reputation: 1152

By default supabase-js uses localstorage which is persisted indefinitely. You can implement your "own storage" with sessionstorage instead which means the stored values would be destroyed when the user closes the session/window: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API

import { createClient } from '@supabase/supabase-js'

const supabase = createClient("https://xyzcompany.supabase.co", "public-anon-key", {
  auth: {
    storage: window.sessionStorage,
  },
});

Upvotes: 0

Related Questions