Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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