Authenticate users with their Google account

Yt::Auth lets you easily authenticate users of your website by means of their Google-based email address.

With Yt::Auth, it is easy to limit access to your app to a few users without the need for them to create a username and password.

The source code is available on GitHub and the documentation on RubyDoc.

Build Status Coverage Status Dependency Status Code Climate Online docs Gem Version

The Yt::Auth class provides three public methods: url, email, and access_token.

Yt::Auth#url

With the url method, you can obtain a URL where to redirect users who need to authenticate with their Google account in order to use your application:

redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
Yt::Auth.new(redirect_uri: redirect_uri).url
 # => https://accounts.google.com/o/oauth2/auth?client_id=...&scope=email&redirect_uri=https%3A%2F%2Fexample.com%2Fauth&response_type=code

Yt::Auth#email

After users have authenticated with their Google account, they will be redirected to the redirect_uri you indicated, with an extra code query parameter, e.g. https://example.com/auth?code=1234

With the email method, you can obtain the verified email of the users:

redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
code = '1234' # REPLACE WITH REAL ONE
Yt::Auth.new(redirect_uri: redirect_uri, code: code).email
 # => "[email protected]"

Yt::Auth#access_token

Similarly, with the access_token method, you can obtain an access token of the users:

redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
code = '1234' # REPLACE WITH REAL ONE
Yt::Auth.new(redirect_uri: redirect_uri, code: code).access_token
 # => "ya29.GltbBLXt74GrwX8S_xr70aX"

Yt::HTTPError

Yt::HTTPError will be raised whenever something goes wrong during the authentication process. The message of the error will include the details:

redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
code = 'this-is-not-a-valid-code'
Yt::Auth.new(redirect_uri: redirect_uri, code: code).email
 # => Yt::HTTPError: Invalid authorization code.

How to contribute

Contribute to the code by forking the project, adding the missing code, writing the appropriate tests and submitting a pull request.

In order for a PR to be approved, all the tests need to pass and all the public methods need to be documented and listed in the guides. Remember:

  • to run all tests locally: bundle exec rspec
  • to generate the docs locally: bundle exec yard
  • to list undocumented methods: bundle exec yard stats --list-undoc