-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
68 lines (57 loc) 路 1.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/rivo/tview"
g "github.com/darylhjd/mangadesk/globals"
p "github.com/darylhjd/mangadesk/pages"
)
// Start the program.
func main() {
// Load user configuration.
if err := g.LoadUserConfiguration(); err != nil {
fmt.Println("Unable to read configuration file. Is it set correctly?")
fmt.Println("If in doubt, delete the configuration file to start over!\n\nDetails:")
fmt.Println(err.Error())
os.Exit(1)
}
// Create new pages holder.
pages := tview.NewPages()
// Set required input captures that are valid for the whole app.
p.SetUniversalHandlers(pages)
// Check whether the user is remembered. If they are, then load credentials into the client and refresh token.
if err := CheckAuth(); err != nil {
// Prompt login if unable to read stored credentials.
p.ShowLoginPage(pages)
} else {
// If able to log in using stored refresh token, go to logged main page.
p.ShowMainPage(pages)
}
// Run the app. SetRoot also calls SetFocus on the primitive.
if err := g.App.SetRoot(pages, true).Run(); err != nil {
panic(err)
}
}
// CheckAuth : Check if the user's credentials have been stored before.
// If they are, then read it, and attempt to refresh the token.
// Will return error if any steps fail (no stored credentials, authentication failed).
func CheckAuth() error {
// Try to read stored credential file.
content, err := ioutil.ReadFile(filepath.Join(g.UsrDir, g.CredFileName))
if err != nil { // If error, then file does not exist.
return err
}
fmt.Println("Welcome back!")
fmt.Println("Restoring session...")
g.Dex.RefreshToken = string(content) // We set the stored refresh token.
// Do a refresh of the token to keep it up to date.
if err = g.Dex.RefreshSessionToken(); err != nil {
fmt.Println("Session expired. Please login again.")
time.Sleep(time.Millisecond * 750)
return err
}
return nil
}