summaryrefslogtreecommitdiff
path: root/client/login_prompt.go
blob: ec5b36bc521d47b8ffe9c6b6e549ff68fadf5a5a (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main

import (
	"citrons.xyz/talk/tui"
	"os"
)

type loginPrompt struct {
	input tui.TextInput
	uid string
	username string
	customPrompt string
	waiting bool
	callback func(ok bool)
}

func newLoginPrompt(completeName string) *loginPrompt {
	var prompt loginPrompt
	username := completeName
	if username == "" {
		username = os.Getenv("USER")
	}
	prompt.input.SetText(username)
	return &prompt
}

func (p *loginPrompt) Input() *tui.TextInput {
	if !p.waiting {
		return &p.input
	} else {
		return &tui.TextInput {}
	}
}

func (p *loginPrompt) Send(text string) {
	if p.uid == "" {
		p.username = text
		globalApp.authAnon(text, func(success bool, uid string) {
			if success {
				globalApp.removePrompt(p)
				globalApp.cmdWindow.warn(
					"you have created a temporary account. " +
					"set a password to keep it.",
				)
			} else if uid == "" {
				p.username = ""
			} else {
				p.uid = uid
				p.input.SetText("")
				p.input.Private = true
			}
		})
	} else {
		p.input.SetText("")
		p.waiting = true
		globalApp.authPassword(p.uid, text, func(success bool) {
			p.waiting = false
			if !globalApp.authenticated {
				p.uid = ""
				p.input.SetText(p.username)
				p.input.Private = false
				return
			}
			globalApp.removePrompt(p)
			if p.callback != nil {
				p.callback(success)
			}
		})
	}
}

func (p *loginPrompt) ShowStatusLine() {
	tui.Text("[", nil)
	tui.Text("login", &tui.Style {
		Bg: tui.White, Fg: tui.Blue, Bold: true,
	})
	tui.Text("] ", nil)
	if p.customPrompt == "" {
		if p.uid == "" {
			tui.Text("username", nil)
		} else {
			tui.Text("password", nil)
		}
	} else {
		tui.Text(p.customPrompt, nil)
	}
	tui.Text(":", nil)
}

type passwordChangePrompt struct {
	input tui.TextInput
	password string
}

func (p *passwordChangePrompt) Input() *tui.TextInput {
	p.input.Private = true
	return &p.input
}

func (p *passwordChangePrompt) Send(text string) {
	if text != p.password {
		globalApp.cmdWindow.err("passwords do not match")
	} else {
		globalApp.setPassword(text, func(ok bool) {
			if ok {
				globalApp.cmdWindow.info("password changed")
			}
		})
	}
	globalApp.removePrompt(p)
}

func (p *passwordChangePrompt) ShowStatusLine() {
	tui.Text("[", nil)
	tui.Text("login", &tui.Style {
		Bg: tui.White, Fg: tui.Blue, Bold: true,
	})
	tui.Text("] ", nil)
	tui.Text("confirm new password:", nil)
}