|
| 1 | +const superagent = require('superagent') |
| 2 | + |
| 3 | +class Auth { |
| 4 | + constructor(authUrl, supabaseKey, options = { autoRefreshToken: true }) { |
| 5 | + this.authUrl = authUrl |
| 6 | + this.accessToken = null |
| 7 | + this.refreshToken = null |
| 8 | + this.supabaseKey = supabaseKey |
| 9 | + this.currentUser = null |
| 10 | + this.autoRefreshToken = options.autoRefreshToken |
| 11 | + |
| 12 | + this.signup = async (email, password) => { |
| 13 | + const { body } = await superagent |
| 14 | + .post(`${authUrl}/signup`, { email, password }) |
| 15 | + .set('accept', 'json') |
| 16 | + .set('apikey', this.supabaseKey) |
| 17 | + |
| 18 | + return body |
| 19 | + } |
| 20 | + |
| 21 | + this.login = async (email, password) => { |
| 22 | + |
| 23 | + const response = await superagent |
| 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 }) |
| 40 | + .set('apikey', this.supabaseKey) |
| 41 | + |
| 42 | + if (response.status === 200) { |
| 43 | + this.accessToken = response.body['access_token'] |
| 44 | + this.refreshToken = response.body['refresh_token'] |
| 45 | + let tokenExirySeconds = response.body['expires_in'] |
| 46 | + if (this.autoRefreshToken && tokenExirySeconds) |
| 47 | + setTimeout(this.refreshToken, tokenExirySeconds - 60) |
| 48 | + } |
| 49 | + return response |
| 50 | + } |
| 51 | + |
| 52 | + this.logout = async () => { |
| 53 | + await superagent |
| 54 | + .post(`${authUrl}/logout`) |
| 55 | + .set('Authorization', `Bearer ${this.accessToken}`) |
| 56 | + .set('apikey', this.supabaseKey) |
| 57 | + |
| 58 | + this.currentUser = null |
| 59 | + this.accessToken = null |
| 60 | + } |
| 61 | + |
| 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 | + |
| 70 | + this.user = async () => { |
| 71 | + if (this.currentUser) return this.currentUser |
| 72 | + |
| 73 | + const response = await superagent |
| 74 | + .get(`${authUrl}/user`) |
| 75 | + .set('Authorization', `Bearer ${this.accessToken}`) |
| 76 | + .set('apikey', this.supabaseKey) |
| 77 | + |
| 78 | + if (response.status === 200) { |
| 79 | + this.currentUser = response.body |
| 80 | + this.currentUser['access_token'] = this.accessToken |
| 81 | + this.currentUser['refresh_token'] = this.refreshToken |
| 82 | + } |
| 83 | + return this.currentUser |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +export { Auth } |
0 commit comments