Skip to content

Commit a5bc375

Browse files
committed
now sending passwords in request body for login and added autorefresh
1 parent 2cb79de commit a5bc375

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/Auth.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const superagent = require('superagent')
22

33
class Auth {
4-
constructor(authUrl, supabaseKey, options = {}) {
4+
constructor(authUrl, supabaseKey, options = { autoRefreshToken: true }) {
55
this.authUrl = authUrl
66
this.accessToken = null
77
this.refreshToken = null
88
this.supabaseKey = supabaseKey
99
this.currentUser = null
10+
this.autoRefreshToken = options.autoRefreshToken
1011

1112
this.signup = async (email, password) => {
1213
const { body } = await superagent
@@ -18,13 +19,32 @@ class Auth {
1819
}
1920

2021
this.login = async (email, password) => {
22+
2123
const response = await superagent
22-
.post(`${authUrl}/token?grant_type=password&username=${email}&password=${password}`)
24+
.post(`${authUrl}/token?grant_type=password`, { email, password })
25+
.set('accept', 'json')
26+
.set('apikey', this.supabaseKey)
27+
28+
if (response.status === 200) {
29+
this.accessToken = response.body['access_token']
30+
this.refreshToken = response.body['refresh_token']
31+
if (this.autoRefreshToken && tokenExirySeconds)
32+
setTimeout(this.refreshToken, tokenExirySeconds - 60)
33+
}
34+
return response
35+
}
36+
37+
this.refreshToken = async () => {
38+
const response = await superagent
39+
.post(`${authUrl}/token?grant_type=refresh_token`, { refresh_token: this.refreshToken })
2340
.set('apikey', this.supabaseKey)
2441

2542
if (response.status === 200) {
2643
this.accessToken = response.body['access_token']
2744
this.refreshToken = response.body['refresh_token']
45+
let tokenExirySeconds = response.body['expires_in']
46+
if (this.autoRefreshToken && tokenExirySeconds)
47+
setTimeout(this.refreshToken, tokenExirySeconds - 60)
2848
}
2949
return response
3050
}
@@ -39,6 +59,14 @@ class Auth {
3959
this.accessToken = null
4060
}
4161

62+
// this.setRefreshTokenExpiry = (refreshTokenExpirySeconds) => {
63+
// let bufferSeconds = 60
64+
// let t = new Date() // current time
65+
// this.refreshTokenExpiry = t.setSeconds(
66+
// t.getSeconds() + (refreshTokenExpirySeconds - bufferSeconds)
67+
// )
68+
// }
69+
4270
this.user = async () => {
4371
if (this.currentUser) return this.currentUser
4472

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Auth } from './Auth'
44
import { PostgrestClient } from '@supabase/postgrest-js'
55

66
class SupabaseClient {
7-
constructor(supabaseUrl, supabaseKey, options = {}) {
7+
constructor(supabaseUrl, supabaseKey, options = { autoRefreshToken: true }) {
88
this.supabaseUrl = null
99
this.supabaseKey = null
1010
this.restUrl = null
@@ -20,7 +20,7 @@ class SupabaseClient {
2020

2121
this.authenticate(supabaseUrl, supabaseKey)
2222

23-
this.auth = new Auth(this.authUrl, supabaseKey)
23+
this.auth = new Auth(this.authUrl, supabaseKey, { autoRefreshToken: options.autoRefreshToken })
2424
}
2525

2626
/**

test/integration/testAuth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { createClient } from '../../src'
77

88
describe('test signing up and logging in as a new user', () => {
99
const supabase = createClient(
10-
'https://UxtUdvoEHGzXftFJhwwT.supabase.net',
11-
'XbBJdEH2WdymQ0Hq9Huk1JqCCmggPX'
10+
'https://HPPNcyqrPOIDwqzQHjRl.supabase.net',
11+
'JBkDpEMQw9a9yIeVuFhTt5JEhGjQEY'
1212
)
1313
const randomEmail = `a${Math.random()}@google.com`
1414

0 commit comments

Comments
 (0)