1
1
package staticbackend
2
2
3
3
import (
4
- "context"
5
4
"fmt"
6
5
"log"
7
6
"math/rand"
@@ -19,7 +18,6 @@ import (
19
18
"github.com/stripe/stripe-go/v71/customer"
20
19
"github.com/stripe/stripe-go/v71/sub"
21
20
22
- "go.mongodb.org/mongo-driver/bson"
23
21
"go.mongodb.org/mongo-driver/bson/primitive"
24
22
)
25
23
@@ -32,20 +30,32 @@ var (
32
30
type accounts struct {}
33
31
34
32
func (a * accounts ) create (w http.ResponseWriter , r * http.Request ) {
35
- email := strings .ToLower (r .URL .Query ().Get ("email" ))
33
+ var email string
34
+ fromCLI := true
35
+
36
+ // the CLI do a GET for the account initialization, we can then
37
+ // base the rest of the flow on the fact that the web UI POST data
38
+ if r .Method == http .MethodPost {
39
+ fromCLI = false
40
+
41
+ r .ParseForm ()
42
+
43
+ email = strings .ToLower (r .Form .Get ("email" ))
44
+ } else {
45
+ email = strings .ToLower (r .URL .Query ().Get ("email" ))
46
+ }
36
47
// TODO: cheap email validation
37
48
if len (email ) < 4 || strings .Index (email , "@" ) == - 1 || strings .Index (email , "." ) == - 1 {
38
49
http .Error (w , "invalid email" , http .StatusBadRequest )
39
50
return
40
51
}
41
52
42
53
db := client .Database ("sbsys" )
43
- ctx := context .Background ()
44
- count , err := db .Collection ("accounts" ).CountDocuments (ctx , bson.M {"email" : email })
54
+ exists , err := internal .EmailExists (db , email )
45
55
if err != nil {
46
56
http .Error (w , err .Error (), http .StatusInternalServerError )
47
57
return
48
- } else if count > 0 {
58
+ } else if exists {
49
59
http .Error (w , "Please use a different/valid email." , http .StatusInternalServerError )
50
60
return
51
61
}
@@ -101,11 +111,11 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
101
111
retry := 10
102
112
dbName := randStringRunes (12 )
103
113
for {
104
- count , err = db . Collection ( "bases" ). CountDocuments ( ctx , bson. M { "name" : dbName } )
114
+ exists , err = internal . DatabaseExists ( db , dbName )
105
115
if err != nil {
106
116
http .Error (w , err .Error (), http .StatusInternalServerError )
107
117
return
108
- } else if count > 0 {
118
+ } else if exists {
109
119
retry --
110
120
dbName = randStringRunes (12 )
111
121
continue
@@ -120,7 +130,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
120
130
Whitelist : []string {"localhost" },
121
131
}
122
132
123
- if _ , err := db . Collection ( "bases" ). InsertOne ( ctx , base ); err != nil {
133
+ if err := internal . CreateBase ( db , base ); err != nil {
124
134
http .Error (w , err .Error (), http .StatusInternalServerError )
125
135
return
126
136
}
@@ -194,7 +204,17 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
194
204
return
195
205
}
196
206
197
- respond (w , http .StatusOK , signUpURL )
207
+ if fromCLI {
208
+ respond (w , http .StatusOK , signUpURL )
209
+ return
210
+ }
211
+
212
+ if strings .HasPrefix (signUpURL , "https" ) {
213
+ http .Redirect (w , r , signUpURL , http .StatusSeeOther )
214
+ return
215
+ }
216
+
217
+ render (w , r , "login.html" , nil , & Flash {Type : "sucess" , Message : "We've emailed you all the information you need to get started." })
198
218
}
199
219
200
220
func (a * accounts ) auth (w http.ResponseWriter , r * http.Request ) {
0 commit comments