this repo has no description
1package state
2
3import (
4 "log"
5 "net/http"
6
7 "github.com/go-chi/chi/v5"
8 "github.com/icyphox/bild/appview/auth"
9 "github.com/icyphox/bild/appview/db"
10)
11
12type State struct {
13 Db *db.DB
14 Auth *auth.Auth
15}
16
17func Make() (*State, error) {
18 db, err := db.Make("appview.db")
19 if err != nil {
20 return nil, err
21 }
22
23 auth, err := auth.Make()
24 if err != nil {
25 return nil, err
26 }
27
28 return &State{db, auth}, nil
29}
30
31func (s *State) Login(w http.ResponseWriter, r *http.Request) {
32 ctx := r.Context()
33
34 switch r.Method {
35 case http.MethodGet:
36 log.Println("unimplemented")
37 return
38 case http.MethodPost:
39 username := r.FormValue("username")
40 appPassword := r.FormValue("password")
41
42 atSession, err := s.Auth.CreateInitialSession(ctx, username, appPassword)
43 if err != nil {
44 log.Printf("creating initial session: %s", err)
45 return
46 }
47
48 err = s.Auth.StoreSession(r, w, atSession)
49 if err != nil {
50 log.Printf("storing session: %s", err)
51 return
52 }
53
54 log.Printf("successfully saved session for %s (%s)", atSession.Handle, atSession.Did)
55 http.Redirect(w, r, "/", http.StatusSeeOther)
56 return
57 }
58}
59
60func (s *State) Router() http.Handler {
61 r := chi.NewRouter()
62
63 r.Post("/login", s.Login)
64
65 return r
66}