Saturday, June 4, 2016

OAuth2 with Golang

In my previous post I showed how to have an authentication handler. In this post I will continue with that source and implement user login using OAuth2. I will use Google as the provider.
Gomniauth is a better solution as GoLang OAuth2 package. First we need to install Gomniauth package.
In this demo we will use Google as the authentication provider. Using Google Developer Console you can create a client id and client secrets. And also you have to give host URL and re directional URL. Then will setup Gomniauth in main.go file.
Next will add login.html file.
Add endpoint for login.html in main function.
In login.html Google login link is given as "/auth/login/google". We need to write a login handler which can identify the provider and redirect request accordingly. Because in future we can add more providers like Facebook, Github and etc.
Next we need to write callback handler. In callback, we can retrieve user's basic data using Gomniauth functions. Once we have response data, we Base64 encode full name and save it into a cookie called auth cookie. Then we can retrieve those details later from the cookie and show it in our web pages.
Update templateHandler function like below.
Update home.html file to show name retrieved from Google login.

No comments:

Post a Comment