Display user, logout

This commit is contained in:
Meutel 2017-09-08 18:13:22 +02:00
parent a873c0df36
commit 2909754be4
3 changed files with 54 additions and 17 deletions

View File

@ -45,6 +45,8 @@ const (
URLIndex = "/"
// URLLogin url of login page (OAuth 2)
URLLogin = "/login"
// URLLogout url of logout page
URLLogout = "/logout"
// URLCallback url of OAuth callback
URLCallback = "/callback"
// URLBooks url of books page
@ -160,14 +162,20 @@ type SeriesFull struct {
// Model is basic page model
type Model struct {
Title string
Page string
Version string
Title string
Page string
Version string
Username string
}
// NewModel constuctor for Model
func NewModel(title, page string) *Model {
return &Model{title, page, Version}
func (app *Bouquins) NewModel(title, page string, req *http.Request) *Model {
return &Model{
Title: title,
Page: page,
Version: Version,
Username: app.Username(req),
}
}
// IndexModel is the model for index page
@ -177,13 +185,13 @@ type IndexModel struct {
}
// NewIndexModel constructor IndexModel
func NewIndexModel(title string, count int64) *IndexModel {
return &IndexModel{*NewModel(title, "index"), count}
func (app *Bouquins) NewIndexModel(title string, count int64, req *http.Request) *IndexModel {
return &IndexModel{*app.NewModel(title, "index", req), count}
}
// NewSearchModel constuctor for search page
func NewSearchModel() *Model {
return NewModel("Recherche", "search")
func (app *Bouquins) NewSearchModel(req *http.Request) *Model {
return app.NewModel("Recherche", "search", req)
}
// ResultsModel is a generic model for list pages
@ -277,6 +285,12 @@ func TemplatesFunc(prod bool) *template.Template {
})
}
// RedirectHome redirects to home page
func RedirectHome(res http.ResponseWriter, req *http.Request) error {
http.Redirect(res, req, "/", http.StatusTemporaryRedirect)
return nil
}
// generates a 16 characters long random string
func securedRandString() string {
b := make([]byte, 16)
@ -292,6 +306,15 @@ func (app *Bouquins) Session(req *http.Request) *sessions.Session {
return session
}
// logged in username
func (app *Bouquins) Username(req *http.Request) string {
username := app.Session(req).Values[sessionUser]
if username != nil {
return username.(string)
}
return ""
}
// sets value in session
func (app *Bouquins) SessionSet(name string, value string, res http.ResponseWriter, req *http.Request) {
session := app.Session(req)
@ -414,7 +437,7 @@ func (app *Bouquins) bookPage(idParam string, res http.ResponseWriter, req *http
if err != nil {
return err
}
return app.render(res, tplBooks, &BookModel{*NewModel(book.Title, "book"), book})
return app.render(res, tplBooks, &BookModel{*app.NewModel(book.Title, "book", req), book})
}
func (app *Bouquins) authorPage(idParam string, res http.ResponseWriter, req *http.Request) error {
id, err := strconv.Atoi(idParam)
@ -425,7 +448,7 @@ func (app *Bouquins) authorPage(idParam string, res http.ResponseWriter, req *ht
if err != nil {
return err
}
return app.render(res, tplAuthors, &AuthorModel{*NewModel(author.Name, "author"), author})
return app.render(res, tplAuthors, &AuthorModel{*app.NewModel(author.Name, "author", req), author})
}
func (app *Bouquins) seriePage(idParam string, res http.ResponseWriter, req *http.Request) error {
id, err := strconv.Atoi(idParam)
@ -436,7 +459,7 @@ func (app *Bouquins) seriePage(idParam string, res http.ResponseWriter, req *htt
if err != nil {
return err
}
return app.render(res, tplSeries, &SeriesModel{*NewModel(series.Name, "series"), series})
return app.render(res, tplSeries, &SeriesModel{*app.NewModel(series.Name, "series", req), series})
}
// ROUTES //
@ -458,12 +481,12 @@ func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) erro
// SearchPage displays search form and results
func (app *Bouquins) SearchPage(res http.ResponseWriter, req *http.Request) error {
return app.render(res, tplSearch, NewSearchModel())
return app.render(res, tplSearch, app.NewSearchModel(req))
}
// AboutPage displays about page
func (app *Bouquins) AboutPage(res http.ResponseWriter, req *http.Request) error {
return app.render(res, tplAbout, NewModel("A propos", "about"))
return app.render(res, tplAbout, app.NewModel("A propos", "about", req))
}
// LoginPage redirects to OAuth login page (github)
@ -476,12 +499,19 @@ func (app *Bouquins) LoginPage(res http.ResponseWriter, req *http.Request) error
return nil
}
// LogoutPage logout connected user
func (app *Bouquins) LogoutPage(res http.ResponseWriter, req *http.Request) error {
app.SessionSet(sessionUser, "", res, req)
return RedirectHome(res, req)
}
// CallbackPage handle OAuth 2 callback
func (app *Bouquins) CallbackPage(res http.ResponseWriter, req *http.Request) error {
savedState := app.Session(req).Values[sessionOAuthState]
if savedState == "" {
return fmt.Errorf("missing saved oauth state")
}
app.SessionSet(sessionOAuthState, "", res, req)
state := req.FormValue("state")
if state != savedState {
return fmt.Errorf("invalid oauth state, expected '%s', got '%s'", "state", state)
@ -521,8 +551,7 @@ func (app *Bouquins) CallbackPage(res http.ResponseWriter, req *http.Request) er
if userEmail == "meutel+github@meutel.net" {
app.SessionSet(sessionUser, "Meutel", res, req)
log.Println("User logged in", userEmail)
http.Redirect(res, req, "/", http.StatusTemporaryRedirect)
return nil
return RedirectHome(res, req)
} else {
return fmt.Errorf("Unknown user")
}
@ -535,7 +564,7 @@ func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) error
if err != nil {
return err
}
model := NewIndexModel("", count)
model := app.NewIndexModel("", count, req)
if isJSON(req) {
return writeJSON(res, model)
}

View File

@ -113,6 +113,7 @@ func handleURL(url string, f func(res http.ResponseWriter, req *http.Request) er
func router(app *bouquins.Bouquins) {
handleURL(bouquins.URLIndex, app.IndexPage)
handleURL(bouquins.URLLogin, app.LoginPage)
handleURL(bouquins.URLLogout, app.LogoutPage)
handleURL(bouquins.URLCallback, app.CallbackPage)
handleURL(bouquins.URLBooks, app.BooksPage)
handleURL(bouquins.URLAuthors, app.AuthorsPage)

View File

@ -23,5 +23,12 @@
<input name="q" type="text" class="form-control" placeholder="Recherche">
</div>
</form>
<ul class="nav navbar-nav navbar-right">
{{ if .Username }}
<li><a href="/logout">{{ .Username }} <span title="Déconnexion" class="glyphicon glyphicon-log-out"></span></a></li>
{{ else }}
<li><a href="/login">Connexion <span class="glyphicon glyphicon-log-in"></span></a></li>
{{ end }}
</ul>
</div>
</nav>