save session key

This commit is contained in:
sentriz
2019-04-18 11:22:04 +01:00
parent e8424ca571
commit c6273c8c12
3 changed files with 37 additions and 24 deletions

View File

@@ -51,9 +51,10 @@ type Cover struct {
// User represents the users table
type User struct {
Base
Name string `gorm:"not null;unique_index"`
Password string
IsAdmin bool
Name string `gorm:"not null;unique_index"`
Password string
LastFMSession string
IsAdmin bool
}
// Setting represents the settings table

View File

@@ -93,14 +93,16 @@ func (c *Controller) ServeLinkLastFMCallback(w http.ResponseWriter, r *http.Requ
secret.Value,
token,
)
session := r.Context().Value("session").(*sessions.Session)
if err != nil {
session := r.Context().Value("session").(*sessions.Session)
session.AddFlash(err.Error())
session.Save(r, w)
http.Redirect(w, r, "/admin/home", 302)
return
}
fmt.Println("THE SESSION KEY", sessionKey)
user, _ := session.Values["user"].(*db.User)
user.LastFMSession = sessionKey
c.DB.Save(&user)
http.Redirect(w, r, "/admin/home", 302)
}

View File

@@ -16,30 +16,40 @@
<u>last.fm</u>
</div>
<div class="right">
<a href="/admin/update_lastfm_api_key">update last.fm api key</a><br/>
<a href="https://www.last.fm/api/auth/?api_key={{ .CurrentLastFMAPIKey }}&cb={{ .RequestRoot }}/admin/link_lastfm_callback">link account</a><br/>
{{ if .User.IsAdmin }}
<a href="/admin/update_lastfm_api_key">update api key</a><br/>
{{ end }}
{{ if .CurrentLastFMAPIKey }}
{{ if .User.LastFMSession }}
<span class="light">current status</span> linked
{{ else }}
<span class="light">current status</span> unlinked <a href="https://www.last.fm/api/auth/?api_key={{ .CurrentLastFMAPIKey }}&cb={{ .RequestRoot }}/admin/link_lastfm_callback">link</a><br/>
{{ end }}
{{ else if not .User.IsAdmin }}
<span class="light">api key not set. please ask your admin to set it</span>
{{ end }}
</div>
</div>
<div class="padded box mono">
{{ if .User.IsAdmin }}
{{/* admin panel to manage all users */}}
<div class="box-title">
<u>users</u>
</div>
<div class="right">
{{ range $user := .AllUsers }}
{{ $user.Name }} <span class="light">created</span> <u>{{ $user.CreatedAt.Format "Jan 02, 2006" }}</u> <a href="/admin/change_password?user={{ $user.Name }}">change password</a><br/>
{{ end }}
<a href="/admin/create_user" class="button">create new</a>
</div>
{{/* admin panel to manage all users */}}
<div class="box-title">
<u>users</u>
</div>
<div class="right">
{{ range $user := .AllUsers }}
{{ $user.Name }} <span class="light">created</span> <u>{{ $user.CreatedAt.Format "Jan 02, 2006" }}</u> <a href="/admin/change_password?user={{ $user.Name }}">change password</a><br/>
{{ end }}
<a href="/admin/create_user" class="button">create new</a>
</div>
{{ else }}
{{/* user panel to manage themselves */}}
<div class="box-title">
<u>your account</u>
</div>
<div class="right">
<a href="/admin/change_own_password" class="button">change password</a>
</div>
{{/* user panel to manage themselves */}}
<div class="box-title">
<u>your account</u>
</div>
<div class="right">
<a href="/admin/change_own_password" class="button">change password</a>
</div>
{{ end }}
</div>
{{ end }}