Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock v11 for Web サンプル #1

Merged
merged 1 commit into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/auth0js_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Auth0jsController < ApplicationController
def show
end
end
2 changes: 2 additions & 0 deletions app/helpers/auth0js_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Auth0jsHelper
end
2 changes: 2 additions & 0 deletions app/javascript/src/application.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import './home'
import './dashboard'
import './auth0js'

31 changes: 31 additions & 0 deletions app/javascript/src/auth0js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Auth0Lock from 'auth0-lock';

document.addEventListener('DOMContentLoaded', () => {
const element = document.getElementById('js-auth0-lock');
if (element) {
const dataset = element.dataset;
const lock = new Auth0Lock(dataset.auth0ClientId, dataset.auth0Domain);

const loginButton = document.getElementById('js-login-button');

// ログインモーダルを表示
// カスタマイズする方法はドキュメント参照 https://auth0.com/docs/libraries/lock/v11
// divへの埋め込み、スマホ向けのパスワードなしなどいろいろできる
loginButton.addEventListener('click', () => {
lock.show();
});

lock.on('authenticated', (authResult) => {
lock.getUserInfo(authResult.accessToken, (error, profile) => {
if (error) {
alert(error);
return;
}

loginButton.style.display = 'none';
document.getElementById('js-nickname-area').textContent = profile.nickname;
console.log(profile);
});
});
}
});
9 changes: 9 additions & 0 deletions app/views/auth0js/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Auth0js#show</h1>
<p>Find me in app/views/auth0js/show.html.erb</p>

<div id="js-auth0-lock"
data-auth0-client-id="<%= Rails.application.secrets.auth0_client_id %>"
data-auth0-domain="<%= Rails.application.secrets.auth0_domain %>">
<button id="js-login-button">Login with Lock for Web</button>
<p>Welcome <span id="js-nickname-area">Guest</span></p>
</div>
2 changes: 2 additions & 0 deletions app/views/home/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
<h1>RoR Auth0 Sample</h1>
<p>Step 1 - Login.</p>
<a class="btn btn-success btn-lg" href="/auth/auth0">Login</a>
<p>Or Lock v11 for Web</p>
<%= link_to 'Login with Lock v11 for Web', auth0js_path %>
</section>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
get 'dashboard' => 'dashboard#show'
get '/auth/oauth2/callback' => 'auth0#callback'
get '/auth/failure' => 'auth0#failure'
get '/auth0js' => 'auth0js#show'
root 'home#show'
end
9 changes: 9 additions & 0 deletions test/controllers/auth0js_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class Auth0jsControllerTest < ActionDispatch::IntegrationTest
test "should get show" do
get auth0js_show_url
assert_response :success
end

end