Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Commit

Permalink
feat: logout (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
moliva authored Sep 20, 2019
1 parent 27eaee9 commit 3075c97
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 23 deletions.
3 changes: 2 additions & 1 deletion packages/entryPoints/babylon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { WebGLParcelScene } from '../engine/dcl/WebGLParcelScene'
import { enableMiniMap } from '../engine/dcl/widgets/minimap'
import { worldRunningObservable } from '../shared/world/worldState'
import { InstancedSpawnPoint } from '../shared/types'
import { Session } from '../shared/session/index'

document.body.classList.remove('dcl-loading')

Expand All @@ -30,7 +31,7 @@ async function loadClient() {

bodyReadyFuture
.then(async body => {
await initShared(container)
Session.current = await initShared()

await loadClient()

Expand Down
7 changes: 5 additions & 2 deletions packages/shared/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export async function initialize(segmentKey: string, id: string): Promise<void>
return
}

window.analytics.load(segmentKey)
window.analytics.page()
if (window.analytics.load) {
// loading client for the first time
window.analytics.load(segmentKey)
window.analytics.page()
}

return window.analytics.identify(id)
}
Expand Down
22 changes: 12 additions & 10 deletions packages/shared/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ export function isTokenExpired(expiresAt: number) {
}

export class Auth {
store: Store<{ auth: AuthState }>
ephemeralKey?: BasicEphemeralKey

webAuth: auth0.WebAuth
sagaMiddleware: any

comms: CommsAuth

isExpired = createSelector<RootState, AuthState['data'], boolean>(
getData,
data => !!data && isTokenExpired(data.expiresAt)
) as (store: any) => boolean

private store: Store<{ auth: AuthState }>
private ephemeralKey?: BasicEphemeralKey

private webAuth: auth0.WebAuth
private sagaMiddleware: any

private comms: CommsAuth

constructor(
public config: {
private config: {
ephemeralKeyTTL: number
clientId: string
domain: string
Expand All @@ -60,6 +60,7 @@ export class Auth {
scope: 'openid email'
})
}

setup() {
this.sagaMiddleware.run(createSaga(this))
}
Expand Down Expand Up @@ -193,7 +194,8 @@ export class Auth {
logout(): Promise<void> {
return new Promise(resolve => {
this.webAuth.logout({
returnTo: window.location.origin
returnTo: window.location.origin,
federated: true
})
resolve()
})
Expand Down
22 changes: 14 additions & 8 deletions packages/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { defaultLogger } from './logger'
import { ProfileSpec } from './types'
import { getAppNetwork, getNetworkFromTLD, initWeb3 } from './web3'
import { initializeUrlPositionObserver } from './world/positionThings'
import { Session } from './session/index'
import {
createProfile,
createStubProfileSpec,
Expand All @@ -29,7 +30,7 @@ import {
} from './world/profiles'

// TODO fill with segment keys and integrate identity server
export async function initializeAnalytics(userId: string) {
async function initializeAnalytics(userId: string) {
const TLD = getTLD()
switch (TLD) {
case 'org':
Expand All @@ -43,12 +44,17 @@ export async function initializeAnalytics(userId: string) {
}
}

export async function initShared(container: HTMLElement): Promise<ETHEREUM_NETWORK> {
declare let window: any

export async function initShared(): Promise<Session> {
const session = new Session()

const auth = new Auth({
...getLoginConfigurationForCurrentDomain(),
redirectUri: window.location.href,
ephemeralKeyTTL: 24 * 60 * 60 * 1000
})
session.auth = auth

let userId: string

Expand Down Expand Up @@ -105,11 +111,11 @@ export async function initShared(container: HTMLElement): Promise<ETHEREUM_NETWO

// DCL Servers connections/requests after this
if (STATIC_WORLD) {
return net
return session
}

console['group']('connect#comms')
const maxTries = 5
const maxAttemps = 5
for (let i = 1; ; ++i) {
try {
defaultLogger.info(`Try number ${i}...`)
Expand All @@ -120,9 +126,9 @@ export async function initShared(container: HTMLElement): Promise<ETHEREUM_NETWO
)
break
} catch (e) {
if (!e.message.startsWith('Communications link') || i === maxTries) {
// max number of tries reached, rethrow error
defaultLogger.info(`Max number of tries reached (${maxTries}), unsuccessful connection`)
if (!e.message.startsWith('Communications link') || i === maxAttemps) {
// max number of attemps reached, rethrow error
defaultLogger.info(`Max number of attemps reached (${maxAttemps}), unsuccessful connection`)
disconnect()
throw e
}
Expand Down Expand Up @@ -169,5 +175,5 @@ export async function initShared(container: HTMLElement): Promise<ETHEREUM_NETWO
}
console['groupEnd']()

return net
return session
}
23 changes: 23 additions & 0 deletions packages/shared/session/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Auth } from '../auth/index'
import { disconnect } from '../comms'
import { setLoadingScreenVisible } from '../../unity-interface/dcl'

export class Session {
private static _instance: Session = new Session()

auth?: Auth

static set current(instance: Session) {
Session._instance = instance
}

static get current() {
return Session._instance
}

async logout() {
setLoadingScreenVisible(true)
disconnect()
this.auth && (await this.auth.logout())
}
}
7 changes: 6 additions & 1 deletion packages/unity-interface/dcl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { chatObservable } from '../shared/comms/chat'
import { getUserProfile } from '../shared/comms/peers'
import { sceneLifeCycleObservable } from '../decentraland-loader/lifecycle/controllers/scene'
import { worldRunningObservable } from '../shared/world/worldState'
import { Session } from 'shared/session'

let gameInstance!: GameInstance

Expand Down Expand Up @@ -80,6 +81,10 @@ const browserInterface = {
// stub. there is no code about this in unity side yet
},

LogOut() {
Session.current.logout().catch(e => defaultLogger.error('error while logging out', e))
},

ControlEvent({ eventType, payload }: { eventType: string; payload: any }) {
switch (eventType) {
case 'SceneReady': {
Expand All @@ -99,7 +104,7 @@ const browserInterface = {
}
}

function setLoadingScreenVisible(shouldShow: boolean) {
export function setLoadingScreenVisible(shouldShow: boolean) {
document.getElementById('overlay')!.style.display = shouldShow ? 'block' : 'none'
document.getElementById('progress-bar')!.style.display = shouldShow ? 'block' : 'none'
}
Expand Down
3 changes: 2 additions & 1 deletion packages/unity-interface/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { initShared } from '../shared'
import { defaultLogger } from '../shared/logger'
import { initializeEngine } from './dcl'
import future from 'fp-future'
import { Session } from '../shared/session/index'
const queryString = require('query-string')

declare var global: any
Expand Down Expand Up @@ -45,7 +46,7 @@ const engineInitialized = future()
export async function initializeUnity(container: HTMLElement): Promise<InitializeUnityResult> {
_container = container

await initShared(container)
Session.current = await initShared()

const qs = queryString.parse(document.location.search)

Expand Down

0 comments on commit 3075c97

Please sign in to comment.