@remote.it/core
Version:
Core remote.it JavasScript/TypeScript library
186 lines (169 loc) • 4.94 kB
text/typescript
import { API } from './API'
import { DateTime } from 'luxon'
export class User {
static async passwordSignIn(username: string, password: string) {
return API.post<RemoteitUserRaw>('/user/login', {
username,
password,
}).then(user => this.process(user, username))
}
static async authHashSignIn(username: string, authhash: string) {
return API.post<RemoteitUserRaw>('/user/login/authhash', {
username,
authhash,
}).then(user => this.process(user, username))
}
private static process(
user: RemoteitUserRaw,
username: string
): RemoteitUser {
const isTrial = Boolean(user.r3_trial && user.r3_trial !== '')
const trial = user.r3_trial
let planQuantity = user.R3_commercial_quantity
? Number(user.R3_commercial_quantity)
: 0
let planName: RemoteitPlanName
let planPrice = user.R3_commercial_cost
? // The value 0.02 from the API, times 100 to make 2 then
// times 100 again to make 200 cents
Number(user.R3_commercial_cost) * 10000
: 0
let planExpires = user.R3_commercial_expire_date
? // "2019-07-25 12:03:35"
this.parseLegacyDate(
user.R3_commercial_expire_date + ' -0500',
'y-MM-dd h:mm:ss ZZZ'
)
: undefined
const plan = user.R3_commercial_plan
if (trial) {
planExpires = undefined
planPrice = 0
planName = trial as RemoteitPlanName
if (
trial === 'remote.it, 100 Devices, Commercial' ||
trial === 'Promotional, 100 Devices, Commercial'
) {
planQuantity = 100
} else {
// Unlimited plan:
// 'remote.it, Unlimited Devices, Commercial'
planQuantity = Infinity
}
} else if (!plan) {
planName = 'free'
} else if (plan === 'remot3.it.device') {
planName = 'device'
} else if (plan === 'remot3.it.seat') {
planName = 'seat'
} else {
planName = 'free'
}
const planLastTransaction = user.R3_commercial_last_purchase_date
? this.parseLegacyDate(
user.R3_commercial_last_purchase_date + ' -0500',
'y-MM-dd h:mm:ss ZZZ'
)
: undefined
const u = {
apiKey: user.apikey,
authHash: user.service_authhash,
email: user.email,
id: user.guid,
language: user.language || 'en',
pubSubChannel: user.pubsub_channel,
token: user.token || user.auth_token,
username,
plan: {
id: user.R3_commercial_subscription_id,
trial: isTrial,
commercial: this.isCommercialPlan(user),
name: planName,
expires: planExpires,
lastTransaction: planLastTransaction,
quantity: planQuantity,
price: planPrice,
},
}
// Set the API token after login for future API requests
API.token = u.token
return u
}
private static isCommercialPlan(user: RemoteitUserRaw): boolean {
if (user.r3_trial) return true
const isLegacyCommercial =
user.commerical_setting && user.commerical_setting === 'COMMERCIAL'
const plan = user.R3_commercial_plan
const isNewCommercial =
plan &&
(plan.includes('remot3.it.device') || plan.includes('remot3.it.seat'))
return Boolean(isNewCommercial || isLegacyCommercial)
}
private static parseLegacyDate(date: string, format: string) {
return DateTime.fromFormat(date, format).toJSDate()
}
}
export interface RemoteitUser {
apiKey: string
authHash: string
email: string
id: string
language: string
pubSubChannel: string
token: string
username: string
plan: RemoteitPlan
}
interface RemoteitUserRaw {
R3_commercial_cost?: number
R3_commercial_expire_date?: string
R3_commercial_last_purchase_date?: string
R3_commercial_plan?: string
R3_commercial_quantity?: string
R3_commercial_subscription_id?: string
R3_developer_expire_date?: string
R3_developer_last_purchase_date?: string
R3_developer_plan?: string
r3_trial?: string
announcements: string[]
apikey: string
auth_expiration: number
auth_token: string
aws_identity: string
commerical_setting: string
developer_key: string
developer_plan: string
email: string
guid: string
language: string
member_since: string | null
portal_plan: string
portal_plan_expires: string
pubsub_channel: string
secondary_auth: string
service_authhash: string
service_features: string
service_level: string
service_token: string
status: string
storage_plan: string
token: string
token_index: string
}
export type RemoteitPlanName =
| 'device'
| 'seat'
| 'free'
| 'remote.it, 100 Devices, Commercial'
| 'Promotional, 100 Devices, Commercial'
| 'remote.it, Unlimited Devices, Commercial'
export interface RemoteitPlan {
id?: string
commercial: boolean
trial: boolean
name: RemoteitPlanName
expires?: Date
lastTransaction?: Date
quantity: number
price: number
}