Giter Club home page Giter Club logo

oauth2-google's Introduction

Getting started with OAuth2 in Go

Authentication usually is very important part in any application. You can always implement your own authentication system, but it will require a lot of work, registration, forgot password form, etc. That's why OAuth2 was created, to allow user to log in using one of the many accounts user already has.

In this video we'll create a simple web page with Google login using oauth2 Go package.

Google Project, OAuth2 keys

First of all, let's create our Google OAuth2 keys.

  • Go to Google Cloud Platform
  • Create new project or use an existing one
  • Go to Credentials
  • Click "Create credentials"
  • Choose "OAuth client ID"
  • Add authorized redirect URL, in our case it will be localhost:8080/callback
  • Get client id and client secret
  • Save it in a safe place

How OAuth2 works with Google

  • Obtain OAuth 2.0 credentials from the Google API Console.
  • Obtain an access token from the Google Authorization Server.
  • Send the access token to an API.
  • Refresh the access token, if necessary.

Structure

We'll do everything in 1 main.go file, and register 3 URL handlers:

  • /
  • /login
  • /callback

Set Environment Variables

We save google client id and secret in env variables and only use os.Getenv in the code.

cp template.env test.env
# Set your client id / password. It is for IDE env Plugin. 
# you don't have to export environmentvariable manually. don't push it to git reop 
vi test.env

Test it

go run main.go

Conclusion

That's all what we need to do to integrate OAath2 with Google in Go. As you can see, it's only 70 lines of code.

Also, there is a nice package https://github.com/markbates/goth from Mark Bates which provides multi-provider authentications.

License

It is copy from https://medium.com/@pliutau/getting-started-with-oauth2-in-go-2c9fae55d187

oauth2-google's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.