Skip to content

Commit

Permalink
Merge pull request #14 from izqalan/fix/not-working-react-native
Browse files Browse the repository at this point in the history
Fix/not working react native
  • Loading branch information
kiwicopple authored Oct 14, 2020
2 parents 986ab36 + 9ced198 commit 8d9dea5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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'

const DEFAULT_OPTIONS = {
url: GOTRUE_URL,
autoRefreshToken: true,
persistSession: true,
localStorage: global.localStorage,
detectSessionInUrl: true,
headers: DEFAULT_HEADERS,
}
Expand All @@ -16,6 +17,7 @@ export default class Client {
currentSession?: Session | null
autoRefreshToken: boolean
persistSession: boolean
localStorage: Storage
stateChangeEmmitters: Map<string, Subscription> = new Map()

/**
Expand All @@ -25,19 +27,22 @@ export default class Client {
* @param options.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user.
* @param options.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring.
* @param options.persistSession Set to "true" if you want to automatically save the user session into local storage.
* @param options.localStorage
*/
constructor(options: {
url?: string
headers?: { [key: string]: string }
detectSessionInUrl?: boolean
autoRefreshToken?: boolean
persistSession?: boolean
localStorage?: Storage
}) {
const settings = { ...DEFAULT_OPTIONS, ...options }
this.currentUser = null
this.currentSession = null
this.autoRefreshToken = settings.autoRefreshToken
this.persistSession = settings.persistSession
this.localStorage = new LocalStorage(settings.localStorage)
this.api = new Api({ url: settings.url, headers: settings.headers })
this._recoverSession()

Expand Down Expand Up @@ -89,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 @@ -266,17 +270,17 @@ export default class Client {
const timeNow = Math.round(Date.now() / 1000)
const expiresAt = timeNow + secondsToExpiry
const data = { currentSession, expiresAt }
isBrowser() && localStorage.setItem(STORAGE_KEY, JSON.stringify(data))
isBrowser() && this.localStorage.setItem(STORAGE_KEY, JSON.stringify(data))
}

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

private _recoverSession() {
const json = isBrowser() && localStorage.getItem(STORAGE_KEY)
private async _recoverSession() {
const json = isBrowser() && (await this.localStorage.getItem(STORAGE_KEY))
if (json) {
try {
const data = JSON.parse(json)
Expand Down
24 changes: 24 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,27 @@ export function getParameterByName(name: string, url?: string) {
if (!results[2]) return ''
return decodeURIComponent(results[2].replace(/\+/g, ' '))
}

export class LocalStorage implements Storage {
localStorage: Storage;
[name: string]: any
length!: number
constructor(localStorage: Storage) {
this.localStorage = localStorage || global.localStorage
}
clear(): void {
return this.localStorage.clear()
}
key(index: number): string | null {
return this.localStorage.key(index)
}
setItem(key: string, value: any): void {
return this.localStorage.setItem(key, value)
}
getItem(key: string): string | null {
return this.localStorage.getItem(key)
}
removeItem(key: string): void {
return this.localStorage.removeItem(key)
}
}

0 comments on commit 8d9dea5

Please sign in to comment.