1- import toast from "components/toast" ;
2- import { addIntentHandler } from "handlers/intent" ;
31import config from "./config" ;
4- import customTab from "./customTab" ;
52
63/**
74 * @typedef {object } User
@@ -29,6 +26,7 @@ let loggedInUser = null;
2926let cacheTimeout = null ;
3027
3128const CACHE_USER_KEY = "cached-logged-in-user" ;
29+ const LOGIN_RESUME_TIMEOUT_MS = 60_000 ;
3230
3331const loginEvents = {
3432 listeners : new Set ( ) ,
@@ -50,7 +48,6 @@ class AuthService {
5048 #loginTimeout = null ;
5149
5250 constructor ( ) {
53- addIntentHandler ( this . onIntentReceiver . bind ( this ) ) ;
5451 loginEvents . addListener ( ( ) => {
5552 clearTimeout ( this . #loginTimeout) ;
5653 for ( const callback of this . #loginCallbacks) {
@@ -66,29 +63,10 @@ class AuthService {
6663 }
6764
6865 this . #loginCallbacks. clear ( ) ;
69- } , 1000 ) ;
66+ } , LOGIN_RESUME_TIMEOUT_MS ) ;
7067 } ) ;
7168 }
7269
73- async onIntentReceiver ( event ) {
74- try {
75- if ( event ?. module === "user" && event ?. action === "login" ) {
76- if ( event ?. value ) {
77- this . #exec( "saveToken" , [ event . value ] ) ;
78- toast ( "Logged in successfully" ) ;
79-
80- setTimeout ( ( ) => {
81- loginEvents . emit ( ) ;
82- } , 500 ) ;
83- }
84- }
85- return null ;
86- } catch ( error ) {
87- console . error ( "Failed to parse intent token." , error ) ;
88- return null ;
89- }
90- }
91-
9270 /**
9371 * Helper to wrap cordova.exec in a Promise
9472 */
@@ -159,12 +137,22 @@ class AuthService {
159137
160138 async login ( ) {
161139 return new Promise ( ( resolve , reject ) => {
162- customTab ( `${ config . BASE_URL } /login?redirect=app` ) . catch ( ( err ) => {
163- console . error ( "Custom tab error" , err ) ;
164- reject ( "Failed to open browser" ) ;
165- } ) ;
166-
167- this . #loginCallbacks. add ( { resolve, reject } ) ;
140+ const callback = { resolve, reject } ;
141+ this . #loginCallbacks. add ( callback ) ;
142+ this . #exec( "login" , [
143+ {
144+ baseUrl : config . BASE_URL ,
145+ appVersionCode : window . BuildInfo ?. versionCode || 0 ,
146+ } ,
147+ ] )
148+ . then ( ( ) => {
149+ loginEvents . emit ( ) ;
150+ } )
151+ . catch ( ( err ) => {
152+ console . error ( "Native login error" , err ) ;
153+ this . #loginCallbacks. delete ( callback ) ;
154+ reject ( "Failed to login" ) ;
155+ } ) ;
168156 } ) ;
169157 }
170158}
0 commit comments