jhvst / vertigo

Blog engine in Go (golang)
https://toldjuuso.github.io/vertigo
MIT License
263 stars 24 forks source link

login/register links while signed-in #32

Closed ghost closed 9 years ago

ghost commented 9 years ago

okay, another issue which I've notice is that when you are logged in you are still able to see the login/register links on the index page. Is there a way to change this so that the links are only displayed when the user isn't signed in.

jhvst commented 9 years ago

Currently I'm not aware of way to add this, expect for writing a middleware and modifying every render call in routes...

Look forward to #27, it will most likely added in there.

jayrox commented 9 years ago

you can fix this easily.

in the template add something like:

{{ if loggedin .Session }}
    <a href="/user">User</a>
    <a href="/user/logout">Log out</a>
{{ else }}
    <a href="/user/login">Log in</a>
    {{ if allowregistrations . }}<a href="/user/register">Register</a>{{ end }}
{{ end }}

and in main.go add a "loggedin" template helper:

"loggedin": func(session sessions.Session) bool {
    return sessionIsAlive(session)
},

will also need a struct to hold and pass the session to rendered output.

i created a file called "page.go" to store the following code:

package main

import "github.com/martini-contrib/sessions"

type Page struct {
    Session sessions.Session
    Data    interface{}
    Err     string
}

then to use it you'll need to modify the function that renders, like this: for example the posts.go homepage method:

// Homepage route fetches all posts from database and renders them according to "home.tmpl".
// Normally you'd use this function as your "/" route.
func Homepage(res render.Render, db *gorm.DB, s sessions.Session) {
    if Settings.Firstrun {
        res.HTML(200, "installation/wizard", nil)
        return
    }
    var post Post
    posts, err := post.GetAll(db)
    if err != nil {
        log.Println(err)
        res.JSON(500, map[string]interface{}{"error": "Internal server error"})
        return
    }

    res.HTML(200, "home", Page{Session: s, Data: posts})
}

another modification requires using the .Data variable: in post/display.tmpl:

<article>
    <small><time>{{date .Data.Created}}</time></small>
    <h1>{{.Data.Title}}</h1>
    {{unescape .Data.Content}}
</article>
jayrox commented 9 years ago

i know that actually ends up being a ton of updates and changes but it adds a ton of power.

jhvst commented 9 years ago

Thanks @jayrox. The method you described is the same to what I've resorted to use in other apps. Though, I hope this could achieved in some easier way.

I'm going to replace martini with negroni or gin soon, which might give new ways to resolve this problem. In general, the barebones HTTP middleware used by negroni and gin should be a lot more versatile. I'll try to find the time to check up on this during the next week, during which I'll also be merging the database-as-drivers branch. After that getting rid of martini should be pretty straightforward.

Also, @jayrox, any comments on the database-as-drivers branch are welcome. Should you find anything which bothers your mind, let me know about it on #25. I'll probably end up creating an organization for the database plugins, which will then be imported as git submodules on this repository.

jayrox commented 9 years ago

i've been working on replacing martini and have it basically working except the tests.

jhvst commented 9 years ago

@jayrox I added you as a collaborator. I will soon merge the database-drivers branch to the master. I would then like you to fork it and apply the changes regarding martini into a new branch. I could check up on any issues you might have with the tests. The tests itself do not have anything martini specific, so if the server works as a normal HTTP server, everything should be about fine.

I will reference the commits to this issue once I push any code.

ghost commented 9 years ago

@9uuso I didn't want to start another issue for this, but shouldn't vertigo have a way in which themes can simply be implemented such as Ghost or Wordpress? This is possibly around the same bout, shouldn't we add a default theme for the blog and then a separate for the admin type section.

jayrox commented 9 years ago

ideally with these little helpers themes will be very doable.

if you look at my fork (the version i use to host my personal copy) you can see some of the helpers i've added.

my version uses polymer which pretty much shows you can do anything you want theme wise. if there is any interest we can certainly look into merging some, any or all of these changes to this repo.

ghost commented 9 years ago

@jayrox your example code above is initially the thing that should be implemented before anything else, is it already committed onto your forked source?

jayrox commented 9 years ago

Yes On May 19, 2015 9:35 PM, "My-khael Pierce" notifications@github.com wrote:

@jayrox https://github.com/jayrox your example code above is initially the thing that should be implemented before anything else, is it already committed onto your forked source?

— Reply to this email directly or view it on GitHub https://github.com/9uuso/vertigo/issues/32#issuecomment-103715322.

jhvst commented 9 years ago

I didn't want to start another issue for this, but shouldn't vertigo have a way in which themes can simply be implemented such as Ghost or Wordpress?

Yes, this is a feature I've thought about earlier.

That being said, journey did integrate Ghost themes into their engine (which is interesting and appealing feature) which Vertigo could benefit as well. However, I'm afraid it would introduce too much restrictions on what you can and what you can't do with templates. In addition, the way the Ghost templates are parsed looks rather complicated to me.

If the functionality would be imported as a nice package, which would work nicely with html/template, then it might be a different thing.

This is possibly around the same bout, shouldn't we add a default theme for the blog and then a separate for the admin type section.

The blog and admin section do not need separate themes.

However, themes should be able to override default templates, provide helper functions in Go and override or provide their own CSS and JS. Doing this will require some modifications at least to the settings, so its not all too straightforward. Although, implementing this feature now would not be a gigantic effort either, so I'll have a look at it as soon as I find the time and willingness.

@jayrox I'll check out your repo and I find any code to merge, I'll setup a new branch.

jhvst commented 9 years ago

This will be "fixed" with a single User CP link which leads to login page (/user) if you are logged in. If you are not logged in, you should see a register link where you can register.

I've been also working on new frontend design, which looks like this a the moment:

screenshot 2015-10-04 02 10 39

The social media links will be removed from the base theme, since managing the links would need HTML editing. Page name and description (in this case Juuso Haavisto and Blog of a computer whisperer) will be fetched from settings, so they can be changed without knowledge of HTML.

I'll push the changes once I get compatibility problems with schemas resolved. Currently gorm has been removed from the codebase, and sqlx has been added. SQLite works currently, but MySQL and PostgreSQL have some issues.

jhvst commented 9 years ago

I decided to keep the RSS logo in the base theme.

PostgreSQL integration is only one bug away.