Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [TS migration] Migrate 'SessionUtils.js' lib to TypeScript #27430

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Changes from 1 commit
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 !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (loggedInDuringSession !== undefined) {
if (loggedInDuringSession) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed @fabioh8010

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