Skip to content

Commit

Permalink
Merge pull request #27430 from kubabutkiewicz/ts-migration/SessionUti…
Browse files Browse the repository at this point in the history
…ls-lib

[No QA] [TS migration] Migrate 'SessionUtils.js' lib to TypeScript
  • Loading branch information
deetergp authored Sep 20, 2023
2 parents 5d71d32 + 6e6127f commit 55e6010
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/libs/SessionUtils.js → src/libs/SessionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import ONYXKEYS from '../ONYXKEYS';

/**
* Determine if the transitioning user is logging in as a new user.
*
* @param {String} transitionURL
* @param {String} sessionEmail
* @returns {Boolean}
*/
function isLoggingInAsNewUser(transitionURL, sessionEmail) {
function isLoggingInAsNewUser(transitionURL: string, sessionEmail: string): boolean {
// The OldDot mobile app does not URL encode the parameters, but OldDot web
// does. We don't want to deploy OldDot mobile again, so as a work around we
// compare the session email to both the decoded and raw email from the transition link.
Expand All @@ -27,22 +21,22 @@ function isLoggingInAsNewUser(transitionURL, sessionEmail) {
// Capture the un-encoded text in the email param
const emailParamRegex = /[?&]email=([^&]*)/g;
const matches = emailParamRegex.exec(transitionURL);
const linkedEmail = lodashGet(matches, 1, null);
const linkedEmail = matches?.[1] ?? null;
return linkedEmail !== sessionEmail;
}

let loggedInDuringSession;
let loggedInDuringSession: boolean | undefined;

// To tell if the user logged in during this session we will check the value of session.authToken once when the app's JS inits. When the user logs out
// we can reset this flag so that it can be updated again.
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (session) => {
if (!_.isUndefined(loggedInDuringSession)) {
if (loggedInDuringSession) {
return;
}

if (session && session.authToken) {
if (session?.authToken) {
loggedInDuringSession = false;
} else {
loggedInDuringSession = true;
Expand All @@ -54,9 +48,6 @@ function resetDidUserLogInDuringSession() {
loggedInDuringSession = undefined;
}

/**
* @returns {boolean}
*/
function didUserLogInDuringSession() {
return Boolean(loggedInDuringSession);
}
Expand Down

0 comments on commit 55e6010

Please sign in to comment.