Comparing version 1.0.196 to 1.0.197
@@ -46,3 +46,3 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
constructor(service, owner, options, __etc) { | ||
this.__version = '1.0.195'; | ||
this.__version = '1.0.197'; | ||
this.session = null; | ||
@@ -49,0 +49,0 @@ this.connection = null; |
@@ -6,2 +6,3 @@ import SkapiError from '../main/error'; | ||
import { answerSdpOffer, receiveIceCandidate, __peerConnection, __receiver_ringing, closeRTC, respondRTC, __caller_ringing } from './webrtc'; | ||
import { getJwtToken } from './user'; | ||
let __roomList = {}; | ||
@@ -19,10 +20,11 @@ let __current_socket_room; | ||
} | ||
export function connectRealtime(cb, delay = 10) { | ||
export async function connectRealtime(cb, delay = 10) { | ||
if (typeof cb !== 'function') { | ||
throw new SkapiError(`Callback must be a function.`, { code: 'INVALID_REQUEST' }); | ||
} | ||
await getJwtToken.bind(this)(); | ||
if (this.__socket instanceof Promise) { | ||
return this.__socket; | ||
} | ||
this.__socket = new Promise(resolve => { | ||
this.__socket = new Promise(async (resolve) => { | ||
setTimeout(async () => { | ||
@@ -225,2 +227,3 @@ let socket = await prepareWebsocket.bind(this)(); | ||
} | ||
await getJwtToken.bind(this)(); | ||
message = extractFormData(message).data; | ||
@@ -258,2 +261,3 @@ if (socket.readyState === 1) { | ||
} | ||
await getJwtToken.bind(this)(); | ||
params = extractFormData(params).data; | ||
@@ -260,0 +264,0 @@ let { group = null } = params; |
@@ -88,2 +88,3 @@ import { CognitoUserAttribute, CognitoUser, CognitoUserSession } from 'amazon-cognito-identity-js'; | ||
}): Promise<string>; | ||
export declare function getJwtToken(): Promise<any>; | ||
export declare function authentication(): { | ||
@@ -90,0 +91,0 @@ getSession: (option?: { |
@@ -70,2 +70,40 @@ import SkapiError from '../main/error'; | ||
} | ||
export async function getJwtToken() { | ||
await this.__connection; | ||
if (this.session) { | ||
const currentTime = Math.floor(Date.now() / 1000); | ||
const idToken = this.session.getIdToken(); | ||
const idTokenExp = idToken.getExpiration(); | ||
this.log('request:tokens', { | ||
exp: this.session.idToken.payload.exp, | ||
currentTime, | ||
expiresIn: idTokenExp - currentTime, | ||
token: this.session.accessToken.jwtToken, | ||
refreshToken: this.session.refreshToken.token | ||
}); | ||
if (idTokenExp < currentTime) { | ||
this.log('request:requesting new token', null); | ||
try { | ||
await authentication.bind(this)().getSession({ refreshToken: true }); | ||
this.log('request:received new tokens', { | ||
exp: this.session.idToken.payload.exp, | ||
currentTime, | ||
expiresIn: idTokenExp - currentTime, | ||
token: this.session.accessToken.jwtToken, | ||
refreshToken: this.session.refreshToken.token | ||
}); | ||
} | ||
catch (err) { | ||
this.log('request:new token error', err); | ||
throw new SkapiError('User login is required.', { code: 'INVALID_REQUEST' }); | ||
} | ||
} | ||
return this.session?.idToken?.jwtToken; | ||
} | ||
else { | ||
this.log('request:no session', null); | ||
_out.bind(this)(); | ||
throw new SkapiError('User login is required.', { code: 'INVALID_REQUEST' }); | ||
} | ||
} | ||
let isRefreshing = null; | ||
@@ -165,3 +203,3 @@ function refreshSession(session, cognitoUser) { | ||
if (isExpired || refreshToken || !session.isValid()) { | ||
refreshSession.bind(this)(session, cognitoUser).then(async (refreshedSession) => respond(refreshedSession)).catch(err => { | ||
refreshSession.bind(this)(session, cognitoUser).then(refreshedSession => respond(refreshedSession)).catch(err => { | ||
_out.bind(this)(); | ||
@@ -168,0 +206,0 @@ rej(err); |
import SkapiError from '../main/error'; | ||
import validator from './validator'; | ||
import { MD5, generateRandom, extractFormData } from './utils'; | ||
import { authentication } from '../methods/user'; | ||
import { getJwtToken } from '../methods/user'; | ||
async function getEndpoint(dest, auth) { | ||
@@ -83,36 +83,3 @@ const endpoints = await Promise.all([ | ||
if (auth) { | ||
if (this.session) { | ||
const currentTime = Math.floor(Date.now() / 1000); | ||
const idToken = this.session.getIdToken(); | ||
const idTokenExp = idToken.getExpiration(); | ||
this.log('request:tokens', { | ||
exp: this.session.idToken.payload.exp, | ||
currentTime, | ||
expiresIn: idTokenExp - currentTime, | ||
token: this.session.accessToken.jwtToken, | ||
refreshToken: this.session.refreshToken.token | ||
}); | ||
if (idTokenExp < currentTime) { | ||
this.log('request:requesting new token', null); | ||
try { | ||
await authentication.bind(this)().getSession({ refreshToken: true }); | ||
this.log('request:received new tokens', { | ||
exp: this.session.idToken.payload.exp, | ||
currentTime, | ||
expiresIn: idTokenExp - currentTime, | ||
token: this.session.accessToken.jwtToken, | ||
refreshToken: this.session.refreshToken.token | ||
}); | ||
} | ||
catch (err) { | ||
this.log('request:new token error', err); | ||
throw new SkapiError('User login is required.', { code: 'INVALID_REQUEST' }); | ||
} | ||
} | ||
token = this.session?.idToken?.jwtToken; | ||
} | ||
else { | ||
this.log('request:no session', null); | ||
throw new SkapiError('User login is required.', { code: 'INVALID_REQUEST' }); | ||
} | ||
token = await getJwtToken.bind(this)(); | ||
} | ||
@@ -119,0 +86,0 @@ let fetchOptions = {}; |
{ | ||
"name": "skapi-js", | ||
"version": "1.0.196", | ||
"version": "1.0.197", | ||
"description": "Skapi: Backend API for HTML frontend.", | ||
@@ -5,0 +5,0 @@ "main": "js/Main.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
3429635
9596