GithubHelp home page GithubHelp logo

isabella232 / authy-devise-demo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from twilio/authy-devise-demo

0.0 0.0 0.0 111 KB

License: MIT License

Shell 0.02% JavaScript 1.34% Ruby 29.80% CSS 51.00% HTML 17.84%

authy-devise-demo's Introduction

Authy Devise Demo

This is a demo of using Devise and Authy together with the authy-devise gem to add two factor authentication to a Rails application.

Running this demo

This demo was built with Ruby 2.5.1, but should run with any Ruby version that is supported by Rails/Devise.

To run this application download or clone it from GitHub, change into the directory and install the dependencies:

git clone https://github.com/twilio/authy-devise-demo.git
cd authy-devise-demo
bundle install

Create and migrate the database:

rails db:create db:migrate

Get your Authy application API key from the Twilio console and set it in your environment variables:

Through CLI:

export AUTHY_API_KEY=YOUR_API_KEY

Or in .env:

cp .env{.example,}

Place API key in .env file generated from above command.

Run the Rails application:

rails server

Visit localhost:3000 and sign up as a new user.

Building this demo yourself

  1. Create a new Rails application

    rails new authy-devise-demo
    cd authy-devise-demo
  2. Generate a controller

    rails generate controller welcome index signed_in
  3. Add a root path and signed in path to your config/routes.rb

    Rails.application.routes.draw do
      get "signed_in", to: "welcome#signed_in"
      root :to => 'welcome#index'
    end
  4. Update the root and signed in views

    # app/views/welcome/index.html.erb
    <h1>Welcome to the sample app</h1>
    <p><%= link_to "Sign up", new_user_registration_path %></p>
    <p><%= link_to "Sign in", new_user_session_path %></p>
    # app/views/welcome/signed_in.html.erb
    <h1>Welcome to the sample app</h1>
    <p>You are signed in as <%= current_user.email %></p>
  5. Add the devise and devise-authy gems to your Gemfile and install

    gem 'devise', '~> 4.5'
    gem 'devise-authy', '~> 1.9'
    bundle install
  6. Install devise

    rails generate devise:install
  7. Add flash messages to the app/views/layouts/application.html.erb and update the default URL options in config/environments/development.rb

    <p class="notice"><%= notice %></p>
    <p class="alert"><%= alert %></p>
    config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  8. Generate a user model with Devise and migrate the database

    rails generate devise User
    rails db:migrate
  9. Edit app/controllers/welcome_controller.rb and add:

    class WelcomeController < ApplicationController
      before_action :authenticate_user!, only: :signed_in
    
      def index
        redirect_to signed_in_path if user_signed_in?
      end
    
      def signed_in
      end
    end
  10. Install authy-devise

    rails generate devise_authy:install
  11. Open config/initializers/authy.rb and add your Authy API key (generate one in the Twilio Console)

    Authy.api_key = "YOUR_API_KEY"
    Authy.api_uri = "https://api.authy.com/"
  12. Add authy-devise to the User model and run the resulting migration

    rails generate devise_authy User
    rails db:migrate
  13. Run the server and visit http://localhost:3000/users/sign_up to create a user

    rails server
  14. When signed in, visit http://localhost:3000/users/enable_authy to enable 2FA

  15. Sign out and sign back in again and you will be required to enter your 2FA token

authy-devise-demo's People

Contributors

dependabot[bot] avatar dlinch avatar philnash avatar snyk-bot avatar

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.