- Changing Password
Supabase provides a built-in functionality for resetting passwords via a link. However, if you want to change your password while logged in, there is no built-in functionality for this scenario. To handle this, you can use a custom function on Supabase.
Run the following code in your Supabase SQL Editor:
create or replace function changepassword("current_plain_password" text, "new_plain_password" text, "current_id" uuid)
returns varchar
language plpgsql
security definer
as $$
DECLARE
encpass auth.users.encrypted_password%type;
BEGIN
SELECT encrypted_password
FROM auth.users
INTO encpass
WHERE id = current_id and encrypted_password = crypt(current_plain_password, auth.users.encrypted_password);
-- Check the currect password and update
IF NOT FOUND THEN
return 'incorrect';
else
UPDATE auth.users SET encrypted_password = crypt(new_plain_password, gen_salt('bf')) WHERE id = current_id;
return 'success';
END IF;
END;
$$
This will create a custom function that you can call from any platform. Here’s the syntax for Javascript:
const { data, error } = await supabase.rpc('changepassword', {
current_plain_password: oldPassword,
new_plain_password: newPassword,
current_id: currentUserId
});
Here-
current_plain_password is the old password
new_plain_password is the new password
current_id is the id in the current session of the app.
That function returns incorrect if the password is wrong, success if the password is correct and the function updates correctly the password.
- Logging Out from Active Sessions on Other Browsers/Devices
To sign out from all active sessions, you can use the following commands:
// defaults to the global scope
await supabase.auth.signOut()
// sign out from the current session only
await supabase.auth.signOut({ scope: 'local' })
// sign out from the other session without the current
await supabase.auth.signOut({ scope: 'others' })
Upon sign out, all refresh tokens and potentially other database objects related to the affected sessions are destroyed and the client library removes the session stored in the local storage medium.