@@ -37,16 +37,19 @@ func Login(loginURL string, username string, password string, headless bool) err
3737 ctx , cancel := createChromeContext (headless )
3838 defer cancel ()
3939
40+ log .Info ().Msg ("Trying to login to GOG.com." )
41+
4042 // Perform the login
41- finalURL , err := performLogin (ctx , loginURL , username , password )
43+ finalURL , err := performLogin (ctx , loginURL , username , password , headless )
4244 if err != nil {
4345 if headless {
44- log .Warn ().Err (err ).Msg ("Headless login failed, retrying with window mode" )
46+ log .Warn ().Err (err ).Msg ("Headless login failed, retrying with window mode." )
47+ fmt .Println ("Headless login failed, retrying with window mode." )
4548 // Retry with window mode if headless login fails
4649 ctx , cancel = createChromeContext (false )
4750 defer cancel ()
4851
49- finalURL , err = performLogin (ctx , loginURL , username , password )
52+ finalURL , err = performLogin (ctx , loginURL , username , password , false )
5053 if err != nil {
5154 return fmt .Errorf ("failed to login: %w" , err )
5255 }
@@ -86,7 +89,11 @@ func RefreshToken() (*db.Token, error) {
8689 return nil , fmt .Errorf ("failed to retrieve token record: %w" , err )
8790 }
8891
89- if ! isTokenValid (token ) {
92+ // Check if the token is valid and refresh it if necessary
93+ tokenStatus , err := isTokenValid (token )
94+ if err != nil {
95+ return nil , fmt .Errorf ("failed to check token validity: %w" , err )
96+ } else if ! tokenStatus {
9097 if err := refreshAccessToken (token ); err != nil {
9198 return nil , fmt .Errorf ("failed to refresh token: %w" , err )
9299 }
@@ -135,23 +142,24 @@ func refreshAccessToken(token *db.Token) error {
135142
136143// isTokenValid checks if the access token (stored in the database) is still valid.
137144// It takes a pointer to the token record and returns a boolean indicating whether the token is valid.
138- func isTokenValid (token * db.Token ) bool {
145+ func isTokenValid (token * db.Token ) ( bool , error ) {
139146
140147 if token == nil {
141- return false
148+ return false , fmt . Errorf ( "access token data does not exist in the database. Please login first" )
142149 }
143150
144- if token .AccessToken == "" || token .ExpiresAt == "" {
145- return false
151+ // Check if the token fields are empty (needs refresh)
152+ if token .AccessToken == "" || token .RefreshToken == "" || token .ExpiresAt == "" {
153+ return false , nil
146154 }
147155
148156 expiresAt , err := time .Parse (time .RFC3339 , token .ExpiresAt )
149157 if err != nil {
150- log .Error ().Err (err ).Msg ("Invalid expiration time format" )
151- return false
158+ log .Error ().Msgf ("Failed to parse expiration time: %s" , token .ExpiresAt )
159+ return false , err
160+ } else {
161+ return time .Now ().Before (expiresAt ), nil
152162 }
153-
154- return time .Now ().Before (expiresAt )
155163}
156164
157165// createChromeContext creates a new ChromeDP context with the specified option to run Chrome in headless mode or not.
@@ -188,12 +196,27 @@ func createChromeContext(headless bool) (context.Context, context.CancelFunc) {
188196}
189197
190198// performLogin performs the login process using the provided username and password and returns the final URL after successful login.
191- // It takes the ChromeDP context, login URL, username, and password as parameters and returns the final URL and an error if the login process fails.
192- func performLogin (ctx context.Context , loginURL string , username string , password string ) (string , error ) {
193- timeoutCtx , cancel := context .WithTimeout (ctx , 4 * time .Minute )
194- defer cancel ()
199+ // It takes the ChromeDP context, login URL, username, password, and a boolean indicating whether to use headless mode as parameters, and returns the final URL and an error if the login process fails.
200+ func performLogin (ctx context.Context , loginURL string , username string , password string ,
201+ headlessMode bool ) (string , error ) {
195202
203+ var timeoutCtx context.Context
204+ var cancel context.CancelFunc
196205 var finalURL string
206+
207+ if headlessMode {
208+ // Timeout for login in headless mode 30
209+ timeoutCtx , cancel = context .WithTimeout (ctx , 30 * time .Second )
210+ defer cancel ()
211+ } else {
212+ // Timeout for login in window mode 4 minutes
213+ timeoutCtx , cancel = context .WithTimeout (ctx , 4 * time .Minute )
214+ }
215+
216+ // Close the context when the function returns
217+ defer cancel ()
218+
219+ // Start the login process
197220 err := chromedp .Run (timeoutCtx ,
198221 chromedp .Navigate (loginURL ),
199222 chromedp .WaitVisible (`#login_username` , chromedp .ByID ),
0 commit comments