auth and checkLogin cmd

This commit is contained in:
GitBluub
2024-05-04 23:21:45 +02:00
parent 4a0b3157c4
commit 7ba0c2488f
3 changed files with 87 additions and 46 deletions
+16 -16
View File
@@ -5,24 +5,24 @@ import (
)
type Feed struct {
id string
name string
url string
faviconUrl string
tags []string
Id string `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
FaviconUrl string `json:"faviconUrl"`
Tags []string `json:"tags"`
}
type Entry struct {
id string
title string
content string
link string
date time.Time
Id string `json:"id"`
ArticleTitle string `json:"title"`
Content string `json:"content"`
Link string `json:"link"`
Date time.Time `json:"time"`
author *string // author not always specified
isRead bool
isBookmarked bool
isIgnored bool
isReadLater bool
feed Feed
Author *string `json:"author"` // author not always specified
IsRead bool `json:"isRead"`
IsBookmarked bool `json:"IsBookmarked"`
IsIgnored bool `json:"isIgnored"`
IsReadLater bool `json:"isReadLater"`
Feed Feed `json:"feed"`
}
+58 -22
View File
@@ -15,12 +15,12 @@ type statusMsg int
type errMsg struct{ error }
type missingJwtMsg struct{}
type noJwtMsg struct{}
type invalidJwtMsg struct{}
type httpErrorMsg error
func (e errMsg) Error() string { return e.error.Error() }
type getEntriesSuccessMsg []Entry
type httpErrorMsg error
const serverUrl = "http://localhost:1597"
type loginSuccessMsg struct{ string }
@@ -43,6 +43,35 @@ func getData(req *http.Request) ([]byte, error) {
}
func checkJwt(jwt *string) tea.Cmd {
return func() tea.Msg {
if jwt == nil || *jwt == "" {
return noJwtMsg{}
}
url := fmt.Sprintf("%s/me", serverUrl)
req, _ := http.NewRequest(http.MethodPost, url, nil)
req.Header.Add("Content-type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", *jwt))
data, err := getData(req)
if err != nil {
return invalidJwtMsg{}
}
var resp struct {
Id string `json:"id"`
Name string `json:"name"`
}
err = json.Unmarshal(data, &resp)
if err != nil {
return invalidJwtMsg{}
}
return nil
}
}
func login(username string, password string) tea.Cmd {
return func() tea.Msg {
url := fmt.Sprintf("%s/login", serverUrl)
@@ -107,42 +136,49 @@ func register(username string, password string, email string) tea.Cmd {
}
}
type getEntriesSuccessMsg []Entry
func getEntries(jwt *string) tea.Cmd {
return func() tea.Msg {
url := fmt.Sprintf("%s/entries", serverUrl)
req, err := http.NewRequest(http.MethodGet, url, nil)
req, _ := http.NewRequest(http.MethodGet, url, nil)
if jwt == nil {
return missingJwtMsg{}
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", *jwt))
req.Header.Add("Content-type", "application/json")
data, err := getData(req)
if err != nil {
return httpErrorMsg(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return httpErrorMsg(err)
}
defer resp.Body.Close() // nolint: errcheck
data, err := io.ReadAll(resp.Body)
if err != nil {
return httpErrorMsg(err)
}
var entries []Entry
err = json.Unmarshal(data, &entries)
if err != nil {
return httpErrorMsg(err)
}
return getEntriesSuccessMsg(entries)
}
}
type getFeedsSuccessMsg []Feed
type getFeedsErrMsg error
func getFeeds(jwt *string) tea.Cmd {
return func() tea.Msg {
url := fmt.Sprintf("%s/feeds", serverUrl)
req, _ := http.NewRequest(http.MethodGet, url, nil)
if jwt == nil {
return missingJwtMsg{}
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", *jwt))
data, err := getData(req)
if err != nil {
return httpErrorMsg(err)
}
var feeds []Feed
err = json.Unmarshal(data, &feeds)
if err != nil {
return httpErrorMsg(err)
}
return getFeedsSuccessMsg(feeds)
}
}
+13 -8
View File
@@ -15,11 +15,11 @@ import (
)
func (e Entry) FilterValue() string {
return e.title
return e.ArticleTitle
}
func (e Entry) Title() string {
return e.title
return e.ArticleTitle
}
func (e Entry) Description() string {
@@ -57,16 +57,16 @@ func (m *Model) initList(width int, height int) {
m.list = list.New([]list.Item{}, list.NewDefaultDelegate(), width, height)
m.list.Title = "Posts"
m.list.SetFilteringEnabled(false)
var f = Feed{id: "1", tags: []string{"Devops", "Kubernetes"}, name: "zwindler", url: "zwindler.blog", faviconUrl: "zwindler.blog.favicon"}
var f = Feed{Id: "1", Tags: []string{"Devops", "Kubernetes"}, Name: "zwindler", Url: "zwindler.blog", FaviconUrl: "zwindler.blog.favicon"}
m.list.SetItems([]list.Item{
Entry{id: "1", title: "yay", content: "ouin ouin ouin", link: "awd", date: time.Now(), isRead: false, isIgnored: false, isReadLater: false, isBookmarked: false, feed: f},
Entry{id: "2", title: "grrrrr", content: "ouin ouin ouin", link: "awd", date: time.Now(), isRead: false, isIgnored: false, isReadLater: false, isBookmarked: false, feed: f},
Entry{id: "3", title: "my life is pain", content: "ouin ouin ouin", link: "awd", date: time.Now(), isRead: false, isIgnored: false, isReadLater: false, isBookmarked: false, feed: f},
Entry{Id: "1", ArticleTitle: "yay", Content: "ouin ouin ouin", Link: "awd", Date: time.Now(), IsRead: false, IsIgnored: false, IsReadLater: false, IsBookmarked: false, Feed: f},
Entry{Id: "2", ArticleTitle: "grrrrr", Content: "ouin ouin ouin", Link: "awd", Date: time.Now(), IsRead: false, IsIgnored: false, IsReadLater: false, IsBookmarked: false, Feed: f},
Entry{Id: "3", ArticleTitle: "my life is pain", Content: "ouin ouin ouin", Link: "awd", Date: time.Now(), IsRead: false, IsIgnored: false, IsReadLater: false, IsBookmarked: false, Feed: f},
})
}
func (m Model) Init() tea.Cmd {
return tea.Batch(checkServer, m.auth.loginForm.Init(), m.auth.registerForm.Init())
return tea.Batch(checkServer, m.auth.loginForm.Init(), m.auth.registerForm.Init(), checkJwt(m.auth.jwt))
}
@@ -172,6 +172,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.initList(msg.Width, msg.Height)
case invalidJwtMsg:
m.auth.jwt = new(string)
m.page = LOGIN
return m, nil
case loginSuccessMsg:
*m.auth.jwt = msg.string
m.page = FEEDS
@@ -254,7 +259,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func main() {
tea.LogToFile("yay.log", "")
tea.LogToFile("vex.log", "")
m := New()
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {