-
Notifications
You must be signed in to change notification settings - Fork 0
Deviseで登録したユーザーをAuth0に自動移行するサンプル #6
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
Conversation
c85d1bf to
68e45d3
Compare
68e45d3 to
804db9b
Compare
| if user&.active_for_authentication? | ||
|
|
||
| # 認証を通ったらこのデータベースが持っているユーザー情報を返す | ||
| render json: user.auth0_data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Database Action Script の Get User から呼び出すAPI
与えられたメールアドレスが登録済みか調べて返す
このAPIを呼び出すコードは Database Action Script で書くので、自由にできるし、なんなら直接データベースに接続してしまっても良い
| user&.active_for_authentication? | ||
|
|
||
| # 認証を通ったらこのデータベースが持っているユーザー情報を返す | ||
| render json: user.auth0_data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Database Action Script の Login から呼び出すAPI
与えられたメールアドレスとパスワードの組み合わせが正しいかを調べて、返す
| if provider == 'auth0' | ||
| uid = session[:userinfo]['uid'] # ex) "auth0|5a84413df5c8213cb27be6cd" or "auth0|DeviseUser:1" | ||
| stripped_uid = uid.sub("#{provider}|", '') | ||
| DeviseUser.find_by_auth0_uid(stripped_uid) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もし Auth0 に移行してから、もともとのDeviseUserのどれに対応するんだっけ?ってなったら、uidから特定できる、という実験
|
|
||
| # Auth0 への Automatic Migration 用の user_id を作る | ||
| def auth0_uid | ||
| "DeviseUser:#{id}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
別に id を渡しちゃってもよかったんだけど、他で登録されたユーザーとDeviseUserから移行したユーザーを明確に区別したかったのでこのように
メタデータ渡してもよいのかも?
https://auth0.com/docs/metadata
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Auth0ログイン時のフォールバックを利用した、自動移行を試す
何が嬉しいか?
事前準備
https://auth0.com/docs/users/migrations/automatic
再現手順
仕組み
https://auth0.com/docs/users/migrations/automatic
に書いてある。
Auth0でログインしようとした際に、未登録のメールアドレスの場合、事前に設定したLoginスクリプト(Node.js)が呼び出され、その中で旧来のデータベースに対して問い合わせを行う。
問い合わせ方法はNode.jsでかければなんでもよい。今回は問い合わせ用のAPIを作った。
Auth0で登録しようとした際も同様で、まずGet Userでチェックするようになる。
Database Action Scripts
Auth0の管理画面上でも設定・デバッグが可能だが、実際にはAuth0 Management APIを用いて設定するようにし、構成をコード化しておくべき。(#7)
Login
GetUser