Skip to content

Commit

Permalink
fix: async storage class
Browse files Browse the repository at this point in the history
  • Loading branch information
izqalan committed Oct 10, 2020
1 parent 3d2ba6e commit 494d70d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/Api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, post, put } from './lib/fetch'
import { get, post, put, posh } from './lib/fetch'
import { Provider, UserAttributes } from './lib/types'

export default class Api {
Expand Down Expand Up @@ -39,12 +39,14 @@ export default class Api {
*/
async signInWithEmail(email: string, password: string) {
try {
console.log('signInWithEmail API call');
let data: any = await post(
`${this.url}/token?grant_type=password`,
{ email, password },
{ email, password},
{ headers: this.headers }
)
console.log('signInWithEmaildata', data)

// console.log('signInWithEmaildata', data)
return { data, error: null }
} catch (error) {
return { data: null, error }
Expand Down
17 changes: 10 additions & 7 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Api from './Api'
import { isBrowser, getParameterByName, uuid } from './lib/helpers'
import { isBrowser, getParameterByName, uuid, LocalStorage } from './lib/helpers'
import { GOTRUE_URL, DEFAULT_HEADERS, STORAGE_KEY } from './lib/constants'
import { Session, User, UserAttributes, Provider, Subscription, AuthChangeEvent } from './lib/types'

Expand Down Expand Up @@ -42,7 +42,7 @@ export default class Client {
this.currentSession = null
this.autoRefreshToken = settings.autoRefreshToken
this.persistSession = settings.persistSession
this.localStorage = settings.localStorage
this.localStorage = new LocalStorage(settings.localStorage);
this.api = new Api({ url: settings.url, headers: settings.headers })
this._recoverSession()

Expand Down Expand Up @@ -94,7 +94,6 @@ export default class Client {
try {
this._removeSession()
let { email, password, provider } = credentials

if (email && password) return this._handeEmailSignIn(email, password)
if (provider) return this._handeProviderSignIn(provider)
else throw new Error(`You must provide either an email or a third-party provider.`)
Expand Down Expand Up @@ -272,14 +271,18 @@ export default class Client {
isBrowser() && this.localStorage.setItem(STORAGE_KEY, JSON.stringify(data))
}

private _removeSession() {
private async _removeSession() {
this.currentSession = null
this.currentUser = null
isBrowser() && this.localStorage.removeItem(STORAGE_KEY)
isBrowser() && await this.localStorage.removeItemAsync(STORAGE_KEY)
}

private _recoverSession() {
const json = isBrowser() && this.localStorage.getItem(STORAGE_KEY)
private async _recoverSession() {

console.log('recover session')

const json = isBrowser() && this.localStorage.getItemAsync(STORAGE_KEY)

if (json) {
try {
const data = JSON.parse(json)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from 'cross-fetch';
import fetch from 'cross-fetch'

export interface FetchOptions {
headers?: {
Expand Down
14 changes: 14 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ export function getParameterByName(name: string, url?: string) {
if (!results[2]) return ''
return decodeURIComponent(results[2].replace(/\+/g, ' '))
}

export class LocalStorage extends Storage{
localStorage!: Storage
constructor(localStorage: Storage){
super();
this.localStorage = localStorage;
}
async getItemAsync(key: string) {
return await this.localStorage.getItem(key);
}
async removeItemAsync(key: string) {
return await this.localStorage.removeItem(key);
}
}

0 comments on commit 494d70d

Please sign in to comment.