@@ -49,6 +49,7 @@ import {
4949import { ref } from "vue" ;
5050import { useProfileStore } from "../stores/profile" ;
5151import { analytic } from "./mixpanel" ;
52+ import { debug , error , log } from "../common/helper/log" ;
5253
5354GlobalOptions . set ( {
5455 host : process . env . SUBTURTLE_API_URL || "" ,
@@ -61,6 +62,7 @@ export {
6162} from "@modular-rest/client" ;
6263
6364export const isLogin = ref ( false ) ;
65+
6466function updateIsLogin ( ) {
6567 const loginInfo =
6668 authentication . isLogin && authentication . user ?. type . toLowerCase ( ) == "user" ;
@@ -79,8 +81,8 @@ function updateIsLogin() {
7981
8082 return true ;
8183 } )
82- . catch ( ( error ) => {
83- console . error ( "Error bootstrapping profile store:" , error ) ;
84+ . catch ( ( errorDetail ) => {
85+ error ( "Error bootstrapping profile store:" , errorDetail ) ;
8486 return false ;
8587 } ) ;
8688 }
@@ -111,11 +113,20 @@ export async function loginWithLastSession() {
111113
112114 const user = await authentication . loginWithToken (
113115 res . token as string ,
114- true
116+ // token will be stored on background service, so we don't need to store it here
117+ false
115118 ) ;
116- return user ;
119+
120+ debug ( "retrieved user from last login: " , user ) ;
121+
122+ if ( user . type == "anonymous" ) {
123+ authentication . logout ( ) ;
124+ return false ;
125+ } else {
126+ return updateIsLogin ( ) ;
127+ }
128+
117129 } )
118- . then ( ( _user ) => updateIsLogin ( ) )
119130 . then ( async ( _isRegisteredUser ) => {
120131 // updateIsLogin's truthy result means "registered user with a real
121132 // account". Anonymous users return false here even though they hold a
@@ -126,19 +137,20 @@ export async function loginWithLastSession() {
126137 // session, clearing chrome.storage.sync and broadcasting null to every
127138 // tab — and the next translate from any content script then 412s
128139 // because its Authorization header is empty.
129- if ( ! authentication . isLogin ) {
140+ if ( ! _isRegisteredUser && ! authentication . isLogin ) {
130141 await logout ( ) ;
131142 return false ;
132143 }
133144 return true ;
134145 } )
135-
136146 . finally ( ( ) => {
137147 if ( ! authentication . isLogin ) {
148+ debug ( "Login with last session failed, trying anonymous login" ) ;
149+
138150 authentication
139151 . loginAsAnonymous ( )
140152 . then ( async ( ) => {
141- console . log (
153+ debug (
142154 "Subturtle Anonymous login succeded" ,
143155 authentication . isLogin
144156 ) ;
@@ -153,14 +165,14 @@ export async function loginWithLastSession() {
153165 try {
154166 await sendMessage ( new StoreUserTokenMessage ( token ) ) ;
155167 } catch ( err ) {
156- console . warn (
168+ error (
157169 "Subturtle: persisting anonymous token to background failed" ,
158170 err
159171 ) ;
160172 }
161- if ( typeof localStorage !== "undefined" ) {
162- localStorage . setItem ( "token" , token ) ;
163- }
173+ // if (typeof localStorage !== "undefined") {
174+ // localStorage.setItem("token", token);
175+ // }
164176 }
165177 updateIsLogin ( ) ;
166178 } )
0 commit comments