These are chat archives for gin-gonic/gin

18th
Apr 2016
Depado
@Depado
Apr 18 2016 11:15
Hello everyone.
I'm still trying to figure out how to handle authentication with Gin, does anyone have an example ? (Not talking about API auth, I quite figured out how to do that)
Ousmane Traore
@otraore
Apr 18 2016 11:15
session auth?
Depado
@Depado
Apr 18 2016 11:16
Yes
Depado
@Depado
Apr 18 2016 11:19
I understand that it's supposed to point me to the right thing, but I still don't understand how to integrate with a database, handle login/logout of users and such. I was wondering if anyone got a working example. (Or even better, maybe a tutorial of some sort ?)
Ousmane Traore
@otraore
Apr 18 2016 11:20
Well I'm assuming you've written your db logic. Basically after logging in just send a new session with that user's id
And on logout remove it
Depado
@Depado
Apr 18 2016 11:21
I see, I'll try to give that a shot then.
Thanks for the tips @otraore
Ousmane Traore
@otraore
Apr 18 2016 11:21
If you want I can write up an example for you
I can also do real database hits if you tell me what db you're using. (mongo, postgres, mysql, etc)
Depado
@Depado
Apr 18 2016 11:22
I'd love that ^^ I'm thinking of writing a full tutorial once I figure things out
Ousmane Traore
@otraore
Apr 18 2016 11:22
Sure
Does the db matter?
Depado
@Depado
Apr 18 2016 11:23
I don't think so, I'll probably use gorm
Ousmane Traore
@otraore
Apr 18 2016 11:23
If not, I'll use mongo (I'm already doing auth with gin + mgo)
Depado
@Depado
Apr 18 2016 11:23
And for tests I usually just hit an SQLite db
Ousmane Traore
@otraore
Apr 18 2016 11:23
Ok so sql
Thomas Boerger
@tboerger
Apr 18 2016 11:23
i think for an example the type of doesnt really matter
Depado
@Depado
Apr 18 2016 11:23
The DB logic isn't what I'm stuck on actually ^^
But yeah I'll really appreciate an example :)
Ousmane Traore
@otraore
Apr 18 2016 11:24
Ok I'll fake db logic then to make it easier on me
something like if username == "hello" && password == "its me"
Since you're right @tboerger shouldn't really matter
Depado
@Depado
Apr 18 2016 11:27
Yes something like that :D
Ousmane Traore
@otraore
Apr 18 2016 11:55
Tested it out and it works
uploading it as a gist
I'm not sure why but for some reason anything I put to github the tabs look like they're doubled
Depado
@Depado
Apr 18 2016 12:23
Amazing, thanks @otraore :D
Ousmane Traore
@otraore
Apr 18 2016 12:24
My pleasure @Depado, If you need anything else just shoot!
Rıza Sabuncu
@riza
Apr 18 2016 13:00
hei @otraore
i looked your login code
how to redirect a page with session after login?
Ousmane Traore
@otraore
Apr 18 2016 13:05
c.Redirect(code, location)
Rıza Sabuncu
@riza
Apr 18 2016 13:05
thankss :)
Ousmane Traore
@otraore
Apr 18 2016 13:06
No problem
Rıza Sabuncu
@riza
Apr 18 2016 13:06
and how to restrict admin page access
Ousmane Traore
@otraore
Apr 18 2016 13:06
You'll need to set up an Auth middleware
Rıza Sabuncu
@riza
Apr 18 2016 13:06
hmm
Ousmane Traore
@otraore
Apr 18 2016 13:07
Do you want an example?
Ousmane Traore
@otraore
Apr 18 2016 13:07
Yes
I can write an example for you to start off of if you want?
Rıza Sabuncu
@riza
Apr 18 2016 13:08
ow rly
thanks :)
if you write a example im glad
sorry im english very bad:D
Ousmane Traore
@otraore
Apr 18 2016 13:09
It's alright :d
Give me a second I'll write it after I'm done eating.
Rıza Sabuncu
@riza
Apr 18 2016 13:13
session := sessions.Default(c)
user := session.Get("user")
its worked :D
im logout but do not clear session
Ousmane Traore
@otraore
Apr 18 2016 13:56
@riza Logout didn't work because I forgot to save the session
Rıza Sabuncu
@riza
Apr 18 2016 13:56
:D
Ousmane Traore
@otraore
Apr 18 2016 13:57
New logout function:
This message was deleted
func logout(c *gin.Context) {
    session := sessions.Default(c)
    user := session.Get("user")
    if user == nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid session token"})
    } else {
        log.Println(user)
        session.Delete("user")
        session.Save()
        c.JSON(http.StatusOK, gin.H{"message": "Successfully logged out"})
    }
}
session.Save() after session.Delete("user")
Also I have to go now so I'll get back to you on the Auth Middleware.
Rıza Sabuncu
@riza
Apr 18 2016 13:58
okey thanks a lot @otraore
im fixed that, working successfully :D
Ousmane Traore
@otraore
Apr 18 2016 13:58
No problem.
Ousmane Traore
@otraore
Apr 18 2016 15:34
@riza I updated https://gist.github.com/otraore/4b3120aa70e1c1aa33ba78e886bb54f3 with the Auth middleware
Rıza Sabuncu
@riza
Apr 18 2016 15:35
@otraore thank you:)
Ousmane Traore
@otraore
Apr 18 2016 15:35
You're welcome @riza