Skip to content

Commit

Permalink
allow re-logging in with session object. (#20)
Browse files Browse the repository at this point in the history
* how I would handle this

* update test
  • Loading branch information
KennyHarrer authored Aug 4, 2023
1 parent ae75e16 commit 2e21665
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 41 deletions.
80 changes: 48 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,48 +120,61 @@ export class Life360Handler {
* @param userAgent Life360 HTTP user agent string (optional)
* @param apiBaseURL Life360 API base URL, e.g. https://www.life360.com (optional)
*/
constructor(
constructor(options: {
username?: string,
password?: string,
phonenumber?: string,
countryCode?: number,
deviceId?: string,
clientVersion?: string,
userAgent?: string,
apiBaseURL?: string) {
apiBaseURL?: string
session?: Life360.Session}) {

// Generate temp. device ID?
this.deviceId = options.deviceId || createLife360DeviceID();

// Set custom client version?
this.clientVersion = options.clientVersion || DEFAULT_CLIENT_VERSION;

// Set user agent
this.userAgent = options.userAgent || DEFAULT_USER_AGENT;

// Set Life360 API endpoint
this.apiBaseURL = options.apiBaseURL || DEFAULT_API_BASE_URL;

if (options.session) {

this.life360session = new Life360.Session(options.session);

this.auth = {
access_token: options.session.access_token,
token_type: options.session.token_type
};

}

// Check credentials
if ((username && password) || (countryCode && phonenumber && password)) {
if ((options.username && options.password) || (options.countryCode && options.phonenumber && options.password)) {
// Initialize properties
this.username = username || "";
this.password = password || "";
this.phonenumber = phonenumber || "";
this.countryCode = countryCode || 1;

this.username = options.username || "";
this.password = options.password || "";
this.phonenumber = options.phonenumber || "";
this.countryCode = options.countryCode || 1;
this.auth = {
access_token: "",
token_type: ""
};

this.life360session = undefined;

this.sessionCookies = [];
}
else {
// Too few credentials
throw new Error("You MUST provide `username` and `password` *OR* `countryCode`, `phonenumber` and `password` to login.");
throw new Error("You MUST provide `username` and `password` *OR* `countryCode`, `phonenumber` and `password` *OR* `session` to login.");
}

// Generate temp. device ID?
this.deviceId = deviceId || createLife360DeviceID();

// Set custom client version?
this.clientVersion = clientVersion || DEFAULT_CLIENT_VERSION;

// Set user agent
this.userAgent = userAgent || DEFAULT_USER_AGENT;

// Set Life360 API endpoint
this.apiBaseURL = apiBaseURL || DEFAULT_API_BASE_URL;
}

/**
Expand Down Expand Up @@ -325,7 +338,6 @@ export class Life360Handler {
this.auth = {
access_token: response.data["access_token"],
token_type: response.data["token_type"]

};

// return this.auth;
Expand Down Expand Up @@ -515,7 +527,8 @@ export class Life360API extends Life360Handler {
* @param msWaitForRetry Time to wait (ms) before trying a requst again after failure
* @param maxTriesRequest Maximum amout of retries for a single request
*/
constructor(

constructor(options: {
username?: string,
password?: string,
phonenumber?: string,
Expand All @@ -524,13 +537,16 @@ export class Life360API extends Life360Handler {
clientVersion?: string,
userAgent?: string,
apiBaseURL?: string,
autoReconnect = true,
msWaitForRetry = 1000,
maxTriesRequest = 3) {
super(username, password, phonenumber,countryCode, deviceId,clientVersion, userAgent, apiBaseURL);
this.autoReconnect = autoReconnect;
this.msWaitForRetry = msWaitForRetry;
this.maxTriesRequest = maxTriesRequest;
autoReconnect: boolean,
msWaitForRetry: number,
maxTriesRequest: number
}) {
//assign defaults
Object.assign({ autoReconnect: true, msWaitForRetry: 1000, maxTriesRequest: 3 }, options);
super(options);
this.autoReconnect = options.autoReconnect;
this.msWaitForRetry = options.msWaitForRetry;
this.maxTriesRequest = options.maxTriesRequest;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ async function main() {
// process.env.LIFE360_USERAGENT || ""
// );

const myClient = new life360.Life360API(
process.env.LIFE360_USERNAME || "",
process.env.LIFE360_PASSWORD || "",
process.env.LIFE360_PHONENUMBER || "",
process.env.LIFE360_COUNTRYCODE || "",
process.env.LIFE360_DEVICEID || "",
process.env.LIFE360_CLIENTVERSION || "",
process.env.LIFE360_USERAGENT || ""
);
const myClient = new life360.Life360API({
username: process.env.LIFE360_USERNAME || "",
password: process.env.LIFE360_PASSWORD || "",
phonenumber: process.env.LIFE360_PHONENUMBER || "",
countryCode: process.env.LIFE360_COUNTRYCODE || "",
deviceId: process.env.LIFE360_DEVICEID || "",
clientVersion: process.env.LIFE360_CLIENTVERSION || "",
userAgent: process.env.LIFE360_USERAGENT || ""
});

// Login with supplied Life360 credentials (see conf.json)
console.log("- Logging in ...");
Expand Down

0 comments on commit 2e21665

Please sign in to comment.