Skip to content

Commit

Permalink
Merge branch 'master' into add-setSession
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamshankhadeep committed May 7, 2021
2 parents fc0a130 + ca3d515 commit 463888e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,20 @@ export default class GoTrueClient {
return { error, session: null }
}
}
/**
* Overrides the JWT on the current client. The JWT will then be sent in all subsequent network requests.
* @param access_token a jwt access token
*/
setAuth(access_token: string): Session {
this.currentSession = {
...this.currentSession,
access_token,
token_type: 'bearer',
user: null,
}

return this.currentSession
}

/**
* Gets the session data from a URL string
Expand Down Expand Up @@ -431,7 +445,7 @@ export default class GoTrueClient {
private _recoverSession() {
try {
const json = isBrowser() && this.localStorage?.getItem(STORAGE_KEY)
if (!json) {
if (!json || typeof json !== 'string') {
return null
}

Expand Down Expand Up @@ -477,9 +491,10 @@ export default class GoTrueClient {
console.log('Current session is missing data.')
this._removeSession()
} else {
// should be handle on _recoverSession method already
// this._saveSession(currentSession)
// this._notifyAllSubscribers('SIGNED_IN')
// should be handled on _recoverSession method already
// But we still need the code here to accommodate for AsyncStorage e.g. in React native
this._saveSession(currentSession)
this._notifyAllSubscribers('SIGNED_IN')
}
} catch (err) {
console.error(err)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function isSecureEnvironment(req: any) {

const host =
(req.headers.host.indexOf(':') > -1 && req.headers.host.split(':')[0]) || req.headers.host
if (['localhost', '127.0.0.1'].indexOf(host) > -1) {
if (['localhost', '127.0.0.1'].indexOf(host) > -1 || host.endsWith('.local')) {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface Session {
expires_at?: number
refresh_token?: string
token_type: string
user: User
user: User | null
}
export interface User {
id: string
Expand Down
19 changes: 19 additions & 0 deletions test/clientWithAutoConfirmEnabled.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ let authWithSession = new GoTrueClient({
const email = `client_ac_enabled_${faker.internet.email()}`
const setSessionEmail = `client_ac_session_${faker.internet.email()}`
const password = faker.internet.password()
var access_token: string | null = null

test('signUp()', async () => {
let { error, user, session } = await auth.signUp({
email,
password,
})
access_token = session?.access_token || null
expect(error).toBeNull()
expect(error).toBeNull()

Expand Down Expand Up @@ -82,6 +84,23 @@ test('signUp() the same user twice should throw an error', async () => {
expect(user).toBeNull()
})


test('setAuth() should set the Auth headers on a new client', async () => {
expect(access_token).toBeTruthy()

const newClient = new GoTrueClient({
url: GOTRUE_URL,
autoRefreshToken: false,
persistSession: false,
})

newClient.setAuth(access_token!)

const authBearer = newClient.session()?.access_token
expect(authBearer).toEqual(access_token)
})


test('signIn()', async () => {
let { error, session, user } = await auth.signIn({
email,
Expand Down

0 comments on commit 463888e

Please sign in to comment.