Skip to content

Commit 2cb79de

Browse files
committed
initial version of Auth and checking in prettier config files for consistency
1 parent 812bbd6 commit 2cb79de

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

.prettierignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.expo
2+
.next
3+
node_modules
4+
package-lock.json
5+
docker*

.prettierrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true,
6+
"printWidth": 100
7+
}
8+

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"clean": "rimraf lib",
88
"test": "mocha -r @babel/register -r babel-polyfill test/unit/**/*.js",
99
"test:integration": "mocha -r @babel/register -r babel-polyfill test/integration/**/*.js",
10+
"test:auth": "mocha -r @babel/register -r babel-polyfill test/integration/testAuth.js",
1011
"test:integration:full": "docker-compose up -d && sleep 10 && mocha -r @babel/register -r babel-polyfill test/integration/**/*.js ; docker-compose down --remove-orphans",
1112
"test:prod": "cross-env BABEL_ENV=production npm run test",
1213
"test:watch": "npm test -- --watch",
@@ -43,6 +44,7 @@
4344
"babel-polyfill": "^6.26.0",
4445
"babel-preset-minify": "^0.5.1",
4546
"chai": "^4.1.2",
47+
"chai-as-promised": "^7.1.1",
4648
"cross-env": "^7.0.2",
4749
"jest-websocket-mock": "^2.0.1",
4850
"mocha": "^8.0.1",

src/Auth.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const superagent = require('superagent')
2+
3+
class Auth {
4+
constructor(authUrl, supabaseKey, options = {}) {
5+
this.authUrl = authUrl
6+
this.accessToken = null
7+
this.refreshToken = null
8+
this.supabaseKey = supabaseKey
9+
this.currentUser = null
10+
11+
this.signup = async (email, password) => {
12+
const { body } = await superagent
13+
.post(`${authUrl}/signup`, { email, password })
14+
.set('accept', 'json')
15+
.set('apikey', this.supabaseKey)
16+
17+
return body
18+
}
19+
20+
this.login = async (email, password) => {
21+
const response = await superagent
22+
.post(`${authUrl}/token?grant_type=password&username=${email}&password=${password}`)
23+
.set('apikey', this.supabaseKey)
24+
25+
if (response.status === 200) {
26+
this.accessToken = response.body['access_token']
27+
this.refreshToken = response.body['refresh_token']
28+
}
29+
return response
30+
}
31+
32+
this.logout = async () => {
33+
await superagent
34+
.post(`${authUrl}/logout`)
35+
.set('Authorization', `Bearer ${this.accessToken}`)
36+
.set('apikey', this.supabaseKey)
37+
38+
this.currentUser = null
39+
this.accessToken = null
40+
}
41+
42+
this.user = async () => {
43+
if (this.currentUser) return this.currentUser
44+
45+
const response = await superagent
46+
.get(`${authUrl}/user`)
47+
.set('Authorization', `Bearer ${this.accessToken}`)
48+
.set('apikey', this.supabaseKey)
49+
50+
if (response.status === 200) {
51+
this.currentUser = response.body
52+
this.currentUser['access_token'] = this.accessToken
53+
this.currentUser['refresh_token'] = this.refreshToken
54+
}
55+
return this.currentUser
56+
}
57+
}
58+
}
59+
60+
export { Auth }

src/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { uuid } from './utils/Helpers'
22
import Realtime from './Realtime'
3+
import { Auth } from './Auth'
34
import { PostgrestClient } from '@supabase/postgrest-js'
45

56
class SupabaseClient {
@@ -8,6 +9,7 @@ class SupabaseClient {
89
this.supabaseKey = null
910
this.restUrl = null
1011
this.realtimeUrl = null
12+
this.authUrl = null
1113
this.schema = 'public'
1214
this.subscriptions = {}
1315

@@ -17,6 +19,8 @@ class SupabaseClient {
1719
if (options.schema) this.schema = options.schema
1820

1921
this.authenticate(supabaseUrl, supabaseKey)
22+
23+
this.auth = new Auth(this.authUrl, supabaseKey)
2024
}
2125

2226
/**
@@ -28,6 +32,7 @@ class SupabaseClient {
2832
this.supabaseKey = supabaseKey
2933
this.restUrl = `${supabaseUrl}/rest/v1`
3034
this.realtimeUrl = `${supabaseUrl}/realtime/v1`.replace('http', 'ws')
35+
this.authUrl = `${supabaseUrl}/auth/v1`
3136
}
3237

3338
clear() {
@@ -84,8 +89,12 @@ class SupabaseClient {
8489
}
8590

8691
initClient() {
92+
const headers = { apikey: this.supabaseKey }
93+
94+
if (this.auth.accessToken) headers['Authorization'] = `Bearer ${this.auth.accessToken}`
95+
8796
let rest = new PostgrestClient(this.restUrl, {
88-
headers: { apikey: this.supabaseKey },
97+
headers,
8998
schema: this.schema,
9099
})
91100
let api = rest.from(this.tableName)

test/integration/testAuth.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const chai = require('chai')
2+
const expect = chai.expect
3+
const assert = chai.assert
4+
chai.use(require('chai-as-promised'))
5+
6+
import { createClient } from '../../src'
7+
8+
describe('test signing up and logging in as a new user', () => {
9+
const supabase = createClient(
10+
'https://UxtUdvoEHGzXftFJhwwT.supabase.net',
11+
'XbBJdEH2WdymQ0Hq9Huk1JqCCmggPX'
12+
)
13+
const randomEmail = `a${Math.random()}@google.com`
14+
15+
it('should register a new user', async () => {
16+
const response = await supabase.auth.signup(randomEmail, '11password')
17+
assert(response.email === randomEmail, 'user could not sign up')
18+
})
19+
20+
it('should log in a user and return an access token', async () => {
21+
const response = await supabase.auth.login(randomEmail, '11password')
22+
assert(response.body.access_token !== undefined, 'user could not log in')
23+
})
24+
25+
it('should return the currently logged in user', async () => {
26+
const user = await supabase.auth.user()
27+
assert(user.email === randomEmail, 'user could not be retrieved')
28+
})
29+
30+
it('should logout and invalidate the previous access_token', async () => {
31+
await supabase.auth.logout()
32+
await expect(supabase.auth.user()).to.be.rejectedWith(Error)
33+
})
34+
})

0 commit comments

Comments
 (0)