All Projects → dwyl → elixir-auth-google-demo

dwyl / elixir-auth-google-demo

Licence: GPL-2.0 license
⭐️ A basic example of using Google Auth in a Phoenix App.

Programming Languages

elixir
2628 projects
HTML
75241 projects
Procfile
174 projects

Projects that are alternatives of or similar to elixir-auth-google-demo

Phoenix Ecto Append Only Log Example
📝 A step-by-step example/tutorial showing how to build a Phoenix (Elixir) App where all data is immutable (append only). Precursor to Blockchain, IPFS or Solid!
Stars: ✭ 58 (+286.67%)
Mutual labels:  phoenix, learn
Lifelong Learning
✅ ✅ ✅ A massive repo filled with notes on everything from coding to philosophy to psychology to marketing to product
Stars: ✭ 297 (+1880%)
Mutual labels:  phoenix, learn
Phoenix Ecto Encryption Example
🔐 A detailed example for how to encrypt data in a Phoenix (Elixir) App before inserting into a database using Ecto Types
Stars: ✭ 166 (+1006.67%)
Mutual labels:  phoenix, learn
Phoenix Chat Example
💬 A Step-by-Step Beginners Tutorial for Building, Testing & Deploying a Chat app in Phoenix 1.5.5 🚀
Stars: ✭ 452 (+2913.33%)
Mutual labels:  phoenix, learn
Phoenix Todo List Tutorial
✅ Complete beginners tutorial building a todo list from scratch in Phoenix 1.5.3 (latest)
Stars: ✭ 65 (+333.33%)
Mutual labels:  phoenix, learn
Phoenix Liveview Counter Tutorial
🤯 beginners tutorial building a real time counter in Phoenix 1.5.5 + LiveView 0.14.7 ⚡️
Stars: ✭ 115 (+666.67%)
Mutual labels:  phoenix, learn
Ueberauth example
Example Phoenix application using Überauth for authentication
Stars: ✭ 180 (+1100%)
Mutual labels:  phoenix, example-project
The-Overly-Complicated-Random-Number-Generator
An Overly Complex Random Number Generator, created to demystify how containers work.
Stars: ✭ 25 (+66.67%)
Mutual labels:  example-project
phoenix html simplified helpers
Some helpers for phoenix html( truncate, time_ago_in_words, number_with_delimiter, url_for, current_page? )
Stars: ✭ 29 (+93.33%)
Mutual labels:  phoenix
elixir ravelry
Elixir API using Neo4j database for ElixirConf 2017 talk
Stars: ✭ 21 (+40%)
Mutual labels:  phoenix
radiopush
Create communities and share songs with Spotify users (https://radiopush.app)
Stars: ✭ 89 (+493.33%)
Mutual labels:  phoenix
phoenix meta tags
Phoenix library helps generating meta tags for website.
Stars: ✭ 25 (+66.67%)
Mutual labels:  phoenix
play-scala-tls-example
A Play application using HTTPS and WS with optional client authentication
Stars: ✭ 44 (+193.33%)
Mutual labels:  example-project
Go-Clean-Architecture-REST-API
Golang Clean Architecture REST API example
Stars: ✭ 376 (+2406.67%)
Mutual labels:  example-project
dotfiles
💾 dotfiles for macOS - includes zsh, hyper key, Karabiner, and Phoenix configurations. Installation via dotbot.
Stars: ✭ 41 (+173.33%)
Mutual labels:  phoenix
learn-ramda
🐏 Learn ramda, the interactive way
Stars: ✭ 84 (+460%)
Mutual labels:  learn
big-frontend-knowlage
前端入门到进阶图文教程,超详细的Web前端学习笔记。从零开始学前端,做一名精致优雅的前端工程师。公众号「人生代码」作者。
Stars: ✭ 31 (+106.67%)
Mutual labels:  learn
mfpb
My Frugal PostBin
Stars: ✭ 26 (+73.33%)
Mutual labels:  phoenix
yii2-realtime-chat-example
Yii2 simple realtime chat with websocket.io
Stars: ✭ 17 (+13.33%)
Mutual labels:  example-project
phoenix.webui.framework
基于WebDriver的WebUI自动化测试框架
Stars: ✭ 118 (+686.67%)
Mutual labels:  phoenix

elixir-auth-google demo

A basic example of using Google Auth in a Phoenix App.

Build Status codecov.io Hex.pm contributions welcome

Try it: https://elixir-auth-google-demo.herokuapp.com

Why? 🤷

As developers we are always learning new things. When we learn, we love having detailed docs and examples that explain exactly how to get up-and-running. We write examples because we want them ourselves, if you find them useful, please the repo to let us know.

What? 💭

This project is intended as a barebones demonstration of using elixir-auth-google to add support for "Sign-in with Google" to any Phoenix Application.

Who? 👥

This demos is intended for people of all Elixir/Phoenix skill levels. Anyone who wants the "Sign-in with Google" functionality without the extra steps to configure a whole auth framework.

Following all the steps in this example should take around 10 minutes. However if you get stuck, please don't suffer in silence! Get help by opening an issue: https://github.com/dwyl/elixir-auth-google/issues

How? 💻

This example follows the step-by-instructions in the docs github.com/dwyl/elixir-auth-google

0. Create a New Phoenix App

Create a new project if you don't already have one:

If you're adding elixir_auth_google to an existing app, you can skip this step.
Just make sure your app is in a known working state before proceeding
.

mix phx.new app --no-ecto --no-webpack

We don't need a database or static asset compilation for this demo. But we know they work fine because we are using this package in our App in production.

If prompted to install dependencies Fetch and install dependencies? [Yn] Type y and hit the [Enter] key to install.

You should see something like this:

* running mix deps.get
* running cd assets && npm install && node node_modules/webpack/bin/webpack.js
* running mix deps.compile

Make sure that everything works before proceeding:

mix test

You should see:

Generated app app
...

Finished in 0.02 seconds
3 tests, 0 failures

The default tests pass and you know phoenix is compiling.

Run the web application:

mix phx.server

and visit the endpoint in your web browser: http://localhost:4000/ phoenix-default-home

1. Add the elixir_auth_google package to mix.exs 📦

Open your mix.exs file and add the following line to your deps list:

def deps do
  [
    {:elixir_auth_google, "~> 1.3.0"}
  ]
end

Run the mix deps.get command to download.

2. Create the Google APIs Application OAuth2 Credentials

Create your Google App and download the API keys by follow the instructions in: /create-google-app-guide.md

By the end of this step you should have these two environment variables defined:

GOOGLE_CLIENT_ID=631770888008-6n0oruvsm16kbkqg6u76p5cv5kfkcekt.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=MHxv6-RGF5nheXnxh1b0LNDq

⚠️ Don't worry, these keys aren't valid. They are just here for illustration purposes.

3. Create 2 New Files

We need to create two files in order to handle the requests to the Google Auth API and display data to people using our app.

3.1 Create a GoogleAuthController in your Project

In order to process and display the data returned by the Google OAuth2 API, we need to create a new controller.

Create a new file called lib/app_web/controllers/google_auth_controller.ex

and add the following code:

defmodule AppWeb.GoogleAuthController do
  use AppWeb, :controller

  @doc """
  `index/2` handles the callback from Google Auth API redirect.
  """
  def index(conn, %{"code" => code}) do
    {:ok, token} = ElixirAuthGoogle.get_token(code, conn)
    {:ok, profile} = ElixirAuthGoogle.get_user_profile(token.access_token)
    conn
    |> put_view(AppWeb.PageView)
    |> render(:welcome, profile: profile)
  end
end

This code does 3 things:

  • Create a one-time auth token based on the response code sent by Google after the person authenticates.
  • Request the person's profile data from Google based on the access_token
  • Render a :welcome view displaying some profile data to confirm that login with Google was successful.

Note: we are placing the welcome.html.eex template in the template/page directory to save having to create any more directories and view files. You are free to organise your code however you prefer.

3.2 Create welcome template 📝

Create a new file with the following path: lib/app_web/templates/page/welcome.html.eex

And type (or paste) the following code in it:

<section class="phx-hero">
  <h1> Welcome <%= @profile.given_name %>!
  <img width="32px" src="<%= @profile.picture %>" />
  </h1>
  <p> You are <strong>signed in</strong>
    with your <strong>Google Account</strong> <br />
    <strong style="color:teal;"><%= @profile.email %></strong>
  <p/>
</section>

The Google Auth API get_profile request returns profile data in the following format:

%{
  email: "[email protected]",
  email_verified: true,
  family_name: "Correia",
  given_name: "Nelson",
  locale: "en",
  name: "Nelson Correia",
  picture: "https://lh3.googleusercontent.com/a-/AAuE7mApnYb260YC1JY7",
  sub: "940732358705212133793"
}

You can use this data however you see fit. (obviously treat it with respect, only store what you need and keep it secure)

4. Add the /auth/google/callback to router.ex

Open your lib/app_web/router.ex file and locate the section that looks like scope "/", AppWeb do

Add the following line:

get "/auth/google/callback", GoogleAuthController, :index

That will direct the API request response to the GoogleAuthController :index function we defined above.

5. Update PageController.index

In order to display the "Sign-in with Google" button in the UI, we need to generate the URL for the button in the relevant controller, and pass it to the template.

Open the lib/app_web/controllers/page_controller.ex file and update the index function:

From:

def index(conn, _params) do
  render(conn, "index.html")
end

To:

def index(conn, _params) do
  oauth_google_url = ElixirAuthGoogle.generate_oauth_url(conn)
  render(conn, "index.html",[oauth_google_url: oauth_google_url])
end

5.1 Update the page/index.html.eex Template

Open the /lib/app_web/templates/page/index.html.eex file and type the following code:

<section class="phx-hero">
  <h1>Welcome to Awesome App!</h1>
  <p>To get started, login to your Google Account: <p>
  <a href={@oauth_google_url}>
    <img src="https://i.imgur.com/Kagbzkq.png" alt="Sign in with Google" />
  </a>
</section>

The home page of the app now has a big "Sign in with Google" button:

sign-in-button

Once the person completes their authentication with Google, they should see the following welcome message:

welcome

To start your Phoenix server:

  • Install dependencies with mix deps.get
  • Create and migrate your database with mix ecto.setup
  • Install Node.js dependencies with cd assets && npm install
  • Start Phoenix endpoint with mix phx.server
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].