Restrict access to routes when not logged in (i.e., no active session)

This commit is contained in:
Alessio 2023-12-24 14:09:46 -06:00
parent a039b4b320
commit 3ba8e3ef70
3 changed files with 16 additions and 7 deletions

View File

@ -29,7 +29,10 @@ func (t MessageData) FocusedTweetID() scraper.TweetID {
func (app *Application) Messages(w http.ResponseWriter, r *http.Request) {
app.traceLog.Printf("'Messages' handler (path: %q)", r.URL.Path)
// TODO: what if no active user?
if app.ActiveUser.ID == 0 {
app.error_401(w)
return
}
chat_view := app.Profile.GetChatRoomsPreview(app.ActiveUser.ID)
if strings.Trim(r.URL.Path, "/") != "" {

View File

@ -61,6 +61,10 @@ func (app *Application) error_400_with_message(w http.ResponseWriter, msg string
http.Error(w, fmt.Sprintf("Bad Request\n\n%s", msg), 400)
}
func (app *Application) error_401(w http.ResponseWriter) {
http.Error(w, "Please log in or set an active session", 401)
}
func (app *Application) error_404(w http.ResponseWriter) {
http.Error(w, "Not Found", 404)
}

View File

@ -25,12 +25,14 @@
<span>Notifications</span>
</li>
</a>
<a class="unstyled-link" href="/messages">
<li class="quick-link">
<img class="svg-icon" src="/static/icons/messages.svg" />
<span>Messages</span>
</li>
</a>
{{if (not (eq (active_user).Handle "[nobody]"))}}
<a class="unstyled-link" href="/messages">
<li class="quick-link">
<img class="svg-icon" src="/static/icons/messages.svg" />
<span>Messages</span>
</li>
</a>
{{end}}
<a class="unstyled-link" href="/lists">
<li class="quick-link">
<img class="svg-icon" src="/static/icons/lists.svg" />