Skip to content

Commit 74c1582

Browse files
committed
fix: uses the bearer token in local storage, if it exists
1 parent 21df063 commit 74c1582

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Auth.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,19 @@ class Auth {
105105

106106
this.removeSavedSession = () => {
107107
this.currentUser = null
108+
this.refreshToken = null
108109
this.accessToken = this.supabaseKey
109110
isBrowser() && localStorage.removeItem(storageKey)
110111
}
111112

113+
this.authHeader = () => {
114+
let json = isBrowser() && localStorage.getItem(storageKey)
115+
let persisted = json ? JSON.parse(json) : null
116+
if (persisted?.accessToken) return `Bearer ${persisted.accessToken}`
117+
else if (this.accessToken) return `Bearer ${this.accessToken}`
118+
else return null
119+
}
120+
112121
this.recoverSession = () => {
113122
const json = isBrowser() && localStorage.getItem(storageKey)
114123
if (json) {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class SupabaseClient {
9191
initClient() {
9292
let headers = { apikey: this.supabaseKey }
9393

94-
if (this.auth.accessToken) headers['Authorization'] = `Bearer ${this.auth.accessToken}`
94+
if (this.auth.authHeader()) headers['Authorization'] = this.auth.authHeader()
9595

9696
let rest = new PostgrestClient(this.restUrl, {
9797
headers,

0 commit comments

Comments
 (0)